question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating multiple log files at log rotation time '00:00'

See original GitHub issue

Hi guys!

This is my log_sys.py code:

from loguru import logger

logger.remove()

formato = '{time:YYYY-MM-DD HH:mm:ss.SSSSS!UTC}   {level:15}  {message}  [{file} : {line}]'

logger.add('logs_loguru/loguru_UTC_time_WARNING_{time:YYYY-MM-DD!UTC}.log', rotation='00:00', format=formato, level='WARNING', catch=True)
logger.add('logs_loguru/loguru_UTC_time_DEBUG_{time:YYYY-MM-DD!UTC}.log', rotation='00:00', format=formato, level='DEBUG', catch=True)

It is a web app and is running in a Cloudlinux server. At the time the app was started, two log files were created, as expected: loguru_UTC_time_WARNING_2022-08-23.log loguru_UTC_time_DEBUG_2022-08-23.log

Problem started after a few hours (when the server clock was at 00:00 at the server timezone?). Instead to rotate the files, creating loguru_UTC_time_WARNING_2022-08-24.log and loguru_UTC_time_DEBUG_2022-08-24.log, it started to create many log files, with different file name and overwriting the content of some of them, as you can see in the following image:

loguru File time shown in the image is my local time rather than server time.

I tried to add enqueue=True as logger.add('logs_loguru/loguru_UTC_time_DEBUG_{time:YYYY-MM-DD!UTC}.log', rotation='00:00', format=formato, level='DEBUG', catch=True, enqueue=True) but that leads my web app to freezing/crashing, that seems to be a separated issue.

I could not find a way to set the rotation to UTC time. Can that be the issue?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sukhoi47commented, Sep 21, 2022

In recent weeks I am using this code, that is working fine so far.

My server has internal clock set to UTC +02:00. So, by rotating at 02:00 it will be rotating at UTC +00:00 time.

from loguru import logger
from datetime import time, timezone

logger.remove()

formato = '{time:YYYY-MM-DD HH:mm:ss.SSSSS!UTC}   {level:8}  {message}  [{file} : {line}]'

# server internal clock is set to UTC +02:00
logger.add('logs_loguru/loguru_UTC_time_WARNING_{time:YYYY-MM-DD!UTC}.log', rotation='02:00', format=formato, level='WARNING')
logger.add('logs_loguru/loguru_UTC_time_DEBUG_{time:YYYY-MM-DD!UTC}.log', rotation='02:00', format=formato, level='DEBUG')
0reactions
Delgancommented, Aug 26, 2022

Hey @sukhoi47, thanks for the investigations and the reported edge case.

I think you can try using a datetime.time() object with tzinfo set as UTC for rotation:

logger.add(..., rotation=datetime.time(0, 0, 0, tzinfo=datetime.timezone.utc))

May bad it’s not working, I need to address this bug.

Currently, I think you have to implement a custom rotation function or convert your UTC time to local time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Logrotate to Manage Log Files in Linux
Create a system-independent log rotation schedule for your application. ... rotate 4 : log files are rotated four times before old files are...
Read more >
Manually rotating log files on Linux - Network World.com
Under normal circumstances, there is no need to manually rotate log files. Your Linux system should already be set up to rotate some...
Read more >
logging in python with log rotation and compression of rotated ...
Blog post - https://nagasudhir.blogspot.com/2022/11/ logging -in-python.htmlIn this video, we will understand how perform logging in python ...
Read more >
How to Manage Log Files Using Logrotate - Datadog
Create or copy log files to manage rotation · Adjust your log rotation schedule · Compress files to save disk space · Run...
Read more >
cluster + rotating file + large log file = crash · Issue #117 - GitHub
the file rotation takes time, during which log records can come in. In a single process that is handled on the RotatingFileStream via...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found