Loguru timed rotation keeps creating log files on every application startup

Loguru timed rotation keeps creating log files on every application startup

So, I tryed to use loguru as main logging utility in my django project, mainly because of cool and simple file logs rotations implementation. My logger configurations looks something like this:

from loguru import logger

logger.configure(
    handlers=[
        {
            "sink": "/logs/django_loguru_{time}.log",
            "rotation": "1 week",
            "retention": "1 month",
            "level": "INFO",
        },
    ],
)

It also configured during like that during development, when I frequently save files and hence django development server often restarts the whole application. That's how I noticed not expected behaviour, specifically on every file save and application restart loguru creates new empty log file instead of reusing the old one and as a result my containers /logs directory looks like this:

appuser@6c65f55e4366:/logs$ ls -lh
total 4.0K
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:40 django_loguru_2025-04-13_10-40-40_971825.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:40 django_loguru_2025-04-13_10-40-45_501983.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:40 django_loguru_2025-04-13_10-40-57_140253.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-07_077760.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-09_453907.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-23_088179.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-25_549341.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-27_829316.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-30_304538.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-32_662305.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-35_008569.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-37_451321.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-40_934342.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-43_372456.log
-rw-r--r-- 1 appuser appuser   0 Apr 13 10:50 django_loguru_2025-04-13_10-50-52_930993.log
-rw-r--r-- 1 appuser appuser 287 Apr 13 09:08 django_root.log

Doesn't look like "1 week" rotation if you ask me =( So the question, how can I stop loguru from spaming with this empty files and respect rotation settings i provided? Maybe there's mistake in my handler configuration?

Also I notices that standard logging module doesn't have the same problem as you can also see in /logs directory contents, where only one django_root.log file present. It was generated by standard logging with this dict configuration:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "d": {"format": "%(asctime)s - %(levelname)s - %(name)s - %(message)s"}
    },
    "handlers": {
        "file": {
            "formatter": "d",
            "level": "WARNING",
            "class": "logging.handlers.TimedRotatingFileHandler",
            "filename": "/logs/django_root.log",
            "when": "D",
            "interval": 7,
            "backupCount": 12,
            "utc": True,
        },
    },
    "root": {
        "handlers": ["file"],
        "level": "WARNING",
    },
}

As you can see, it was also configured with rotation, but even docs Read more that:

When computing the next rollover time for the first time (when the handler is created), the last modification time of an existing log file, or else the current time, is used to compute when the next rotation will occur.

Answer

After fully forming and writing a question i thought: "well, of course, standard logging doesn't have 'random' {time} macro in the middle of file name, so it can eassily access underlying file and check it's last modification date"

And indeed, after removing this {time} macro from my handler configuration, and leaving just /logs/django_loguru.log in sink field of my handler configuration - loguru started to pickup "old" log file even after application restart and rotate properly.

So, I think it can be concluded that when using log rotation by time with loguru, you should not use the {time} macro.

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles