Ordinary print() function in Python is buffered (there is flush
option, but it alleviates just part of problems). Logging module
knows about and deals with the problem.
https://stackabuse.com/bytes/flush-the-output-of-the-print-function-in-python/
https://stackoverflow.com/q/230751/164233
https://stackoverflow.com/q/55619733/164233
(logging.INFO changed during the development of the code to
logging.DEBUG as needed).
Signed-off-by: Matěj Cepl <mcepl@cepl.eu>
---
import_issues.py | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/import_issues.py b/import_issues.py
index 4b1722e..666644e 100755
--- a/import_issues.py
+++ b/import_issues.py
@@ -123,6 +123,7 @@
import argparse
import csv
+import logging
import json
import os
import re
@@ -137,6 +138,9 @@ from typing import Dict, List, Optional
ID_RE = re.compile(r'^[0-9]+$')
+logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
+ level=logging.DEBUG)
+log = logging.getLogger()
email_count = 0
issue_count = 0
@@ -173,7 +177,7 @@ def do_mail(
):
global email_count
email_count += 1
- print(f"---- #{email_count}")
+ log.info(f"---- #{email_count}")
date = format_datetime(datetime.now(timezone.utc))
msg_id = make_msgid()
@@ -491,7 +495,7 @@ def run(
for issue_json in issue_jsons:
issue_json['notes'].sort(key=lambda x: x['created_at'])
- print("-------- CREATING TICKETS")
+ log.info("-------- CREATING TICKETS")
issue_id_map: Dict[int, int] = {}
@@ -560,7 +564,7 @@ def run(
issue_id_map[gitlab_issue_id] = srht_issue_id
- print("-------- CREATING COMMENTS")
+ log.info("-------- CREATING COMMENTS")
for issue_json in issue_jsons:
for note_json in issue_json['notes']:
@@ -607,7 +611,7 @@ def run(
is_confidential=(note_json['confidential'] is True),
)
- print("-------- CLOSING CLOSED ISSUES")
+ log.info("-------- CLOSING CLOSED ISSUES")
for issue_json in issue_jsons:
if issue_json['state'] == 'closed':
@@ -807,7 +811,7 @@ def main():
assert smtp_user, f"No SMTP user given."
assert smtp_password, f"No SMTP password given."
- print(f"Connecting to {smtp_host}:{smtp_port}, user {smtp_user!r}.")
+ log.info(f"Connecting to {smtp_host}:{smtp_port}, user {smtp_user!r}.")
if smtp_ssl:
smtp = smtplib.SMTP_SSL(host=smtp_host, port=smtp_port)
--
2.44.0
>>>> On Sun, 5 May 2024 08:52:15 +0200
>>>> Matěj Cepl <mcepl@cepl.eu> wrote:
> Ordinary print() function in Python is buffered (there is flush
> option, but it alleviates just part of problems). Logging module
> knows about and deals with the problem.
>
> https://stackabuse.com/bytes/flush-the-output-of-the-print-function-in-python/
> https://stackoverflow.com/q/230751/164233
> https://stackoverflow.com/q/55619733/164233
>
> (logging.INFO changed during the development of the code to
> logging.DEBUG as needed).
>
> Signed-off-by: Matěj Cepl <mcepl@cepl.eu>
> ---
> import_issues.py | 14 +++++++++-----
> 1 file changed, 9 insertions(+), 5 deletions(-)
Applied, thank you! I made some further changes to logging since I
was getting some bad behaviour with "... |& less"; the "---- #N" lines
were being printed in batches, out of order with the printed emails,
since logging was going to stderr unbuffered and stdout was still
buffered. Changed everything to go to stdout for simplicity.
- Bryan