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

Write queued messages to sink after explicit logger.flush(requeue: Optional[bool] = False) #1209

Open
reneleonhardt opened this issue Sep 21, 2024 · 1 comment
Labels
question Further information is requested

Comments

@reneleonhardt
Copy link

reneleonhardt commented Sep 21, 2024

My use case: I would like to print multiple tqdm progress bars and log messages at the "same" time in an asyncio app.
Now all writes to stdout/stderr are interleaved while tqdm is refreshing the progress bars multiple times per second.

I just saw the logger.add(enqueue=True) option, could this be enhanced in _queued_writer() to wait much longer with writing/flushing to the sink until a command like logger.flush() would be explicitly called?
In this case, after all progress bars have been closed (and maybe even removed with leave=False), when tqdm stops updating them.
Then every message would be logged just normally long after they have been recorded, so original timestamps would be written when the event has been queued minutes ago.
flush(requeue=True) would re-enable queuing, otherwise the logger would automatically configure(enqueue=False) on itself to stop queuing from now on for future log messages.

After debugging I found a workaround for me to retain the default message format and color / escape codes, sorry for the noise, thank you for this amazing library!

import io

from loguru import logger

logger.configure(handlers=[dict(sink=(stringio := io.StringIO()), colorize=True)])
logger.info("First")
print("Second")
print(stringio.getvalue())
# Second
# 2024-09-21 00:00:00.000 | INFO     | __main__:<module>:6 - First
@reneleonhardt reneleonhardt changed the title Write queued messages to sink after explicit logger.flush(requeue=Optional[bool]) Write queued messages to sink after explicit logger.flush(requeue: Optional[bool] = False) Sep 21, 2024
@Delgan
Copy link
Owner

Delgan commented Sep 22, 2024

Sorry, I didn't fully grasp your problem but maybe you can benefit from logger.complete(). It's designed to process all pending messages in the queue (equivalent to your "flush" idea?).

@Delgan Delgan added the question Further information is requested label Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants