Skip to content

logging: inherit output stream from existing handler#1191

Merged
casperdcl merged 1 commit intotqdm:develfrom
syzzer:fix-logging-redirection
Aug 14, 2021
Merged

logging: inherit output stream from existing handler#1191
casperdcl merged 1 commit intotqdm:develfrom
syzzer:fix-logging-redirection

Conversation

@syzzer
Copy link
Copy Markdown
Contributor

@syzzer syzzer commented Jun 21, 2021

When using the logging redirection, logs will currently always be printed
to stdout, while the logging module default is to print to stderr.

Fix this by trying to inherit the stream from the existing handler, like
the code already does for the formatter.

Consider the following example:

import logging
import time

from tqdm.contrib.logging import tqdm_logging_redirect

log = logging.getLogger()
log.warning("start")

with tqdm_logging_redirect(range(int(4))) as pbar:
    for i in pbar:
        time.sleep(0.1)
        log.warning(f"Step {i}")

log.warning("done")

Running this while redirecting stdout ($ python3 log.py > /dev/null)
without this patch will print:

$ venv/bin/python log.py > /dev/null
start
100%|████████████████████████████████████████████| 4/4 [00:00<00:00,  9.87it/s]
done

After this patch:

$ venv/bin/python log.py > /dev/null
start
Step 0
Step 1
Step 2
Step 3
100%|████████████████████████████████████████████| 4/4 [00:00<00:00,  9.83it/s]
done

Signed-off-by: Steffan Karger steffan.karger@fox-it.com

When using the logging redirection, logs will currently always be printed
to stdout, while the logging module default is to print to stderr.

Fix this by trying to inherit the stream from the existing handler, like
the code already does for the formatter.

Consider the following example:

    import logging
    import time

    from tqdm.contrib.logging import tqdm_logging_redirect

    log = logging.getLogger()
    log.warning("start")

    with tqdm_logging_redirect(range(int(4))) as pbar:
        for i in pbar:
            time.sleep(0.1)
            log.warning(f"Step {i}")

    log.warning("done")

Running this while redirecting stdout (`$ python3 log.py > /dev/null`)
without this patch will print:

    $ venv/bin/python log.py > /dev/null
    start
    100%|████████████████████████████████████████████| 4/4 [00:00<00:00,  9.87it/s]
    done

After this patch:

    $ venv/bin/python log.py > /dev/null
    start
    Step 0
    Step 1
    Step 2
    Step 3
    100%|████████████████████████████████████████████| 4/4 [00:00<00:00,  9.83it/s]
    done

Signed-off-by: Steffan Karger <steffan.karger@fox-it.com>
@syzzer syzzer requested a review from casperdcl as a code owner June 21, 2021 11:47
@casperdcl casperdcl added this to the Non-breaking milestone Jun 21, 2021
@casperdcl casperdcl added p3-enhancement 🔥 Much new such feature submodule ⊂ Periphery/subclasses to-merge ↰ Imminent labels Jun 21, 2021
@casperdcl casperdcl changed the base branch from master to devel August 14, 2021 20:16
@casperdcl casperdcl merged commit f99bcb4 into tqdm:devel Aug 14, 2021
@casperdcl casperdcl mentioned this pull request Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p3-enhancement 🔥 Much new such feature submodule ⊂ Periphery/subclasses to-merge ↰ Imminent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants