Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structlog does not work with tqdm #564

Closed
yauhen-sobaleu opened this issue Oct 20, 2023 · 4 comments
Closed

Structlog does not work with tqdm #564

yauhen-sobaleu opened this issue Oct 20, 2023 · 4 comments

Comments

@yauhen-sobaleu
Copy link

yauhen-sobaleu commented Oct 20, 2023

Hello! Is there a way to befriend structlog logging with tqdm?

import structlog
import tqdm
from tqdm.contrib.logging import logging_redirect_tqdm

import structlog
import tqdm

def get_logger() -> structlog.stdlib.BoundLogger:
    structlog.configure(
        processors=[
            # show log level
            structlog.stdlib.add_log_level,
            # add time to every log entry
            structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M.%S"),
            # render logs in pretty format (unstructured)
            structlog.dev.ConsoleRenderer(),
            
        ],
    )

    return structlog.get_logger()


logger = get_logger()

if __name__ == '__main__':
    with logging_redirect_tqdm(loggers=[logger]):
        with tqdm.tqdm(total=len(range(10))) as pbar:
            pbar.update(1)

AttributeError: 'BoundLoggerFilteringAtNotset' object has no attribute 'handlers'
@hynek
Copy link
Owner

hynek commented Oct 20, 2023

It’s been a while since I used tqdm, but doesn’t it have some kind of print/stdout redirect? You seem to be using their std lib logging integration that is only gonna work if you also use structlog’s. It’s possible it even works if you do nothing because the latest structlog versions use print() by default.

@yauhen-sobaleu
Copy link
Author

I am trying to solve the issue with Python print function not showing logs in real time (as opposed to prints done via logging module) using Argo Workflows: argoproj/argo-workflows#3169

The only place I invoke print is tqdm progress bar.

Thus I tried to pipe tqdm output to BoundLogger but I have this weird error.

@yauhen-sobaleu
Copy link
Author

It seems tqdm just cannot be used in cloud logging: tqdm/tqdm#1319.
Is it still true?

@yauhen-sobaleu
Copy link
Author

Ok I found exactly what I need! Setting TQDM_POSITION env variable to -1 does the job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants