~khumba/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH] fix: use proper logging instead of print()

Details
Message ID
<20240505065226.20698-2-mcepl@cepl.eu>
DKIM signature
pass
Download raw message
Patch: +9 -5
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
Details
Message ID
<20240506222206.6465e49f@khumba.net>
In-Reply-To
<20240505065226.20698-2-mcepl@cepl.eu> (view parent)
DKIM signature
pass
Download raw message
>>>> 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
Reply to thread Export thread (mbox)