Mayil-AI / loguru

MIT License
0 stars 0 forks source link

Rotation Does not create a new file and Logs on the previous Days log file. (hashtag1093) #1

Open vikramsubramanian opened 3 months ago

vikramsubramanian commented 3 months ago

I have a Django rest service that runs continuously configured somewhere in the middleware. I'm facing two challenges:

  1. The rotation doesn't seem to create the new file name as set in the middleware where instead it extends it with the date when the service first started running. Something like this Icrat-service_2024-01-23.2024-01-24_21-42-12_825363

2.The logs are written in the file with the wrong date in the shared screen shot the date modified you can see the logs were written in the log file of 29-02 instead of a new log file that should be created. ![image](

Here is the code.

class LoggingMiddleware(MiddlewareMixin):
    def __init__(self, get_response):
        self.get_response = get_response                

        hashtag Create the log directory if it doesn't exist
        logDir = "C:\\ProgramData\\MyService\\logs"
        os.makedirs(logDir, exist_ok=True)

        hashtagConfigure the logger        
        logFile = os.path.join(logDir, f"service_{datetime.date.today()}.log")
        logger.remove()

        def getLatestLogFile():           
          baseName = f"service_{datetime.datetime.now()}.log"                  
          return os.path.join(logDir, baseName)

        logger.add(logFile, rotation = "00:00",compression=lambda filepath: os.rename(filepath,getLatestLogFile())) 
       hashtagdatetime.time(hour=0, minute=0,tzinfo = datetime.timezone(datetime.timedelta(hours=utcOffsetFormatted)))) Tried adding 
       hashtagrotate time manually still didn't work
       hashtagI also tried with and without compression still doesn't seem to work.

        logger.add(
            sys.stderr,
            format = "{time:MMMM D, YYYY > HH:mm:ss.SSSZ} | {level} | {message} | {extra}",
            level = "TRACE",           
            catch = False,
            backtrace = False,
            enqueue = True,
            diagnose = False
        )

)

mayil-ai[bot] commented 3 months ago

Possible Solution

Code snippets to check

loguru → _file_sink.py This snippet contains the logic for file termination and rotation, which is relevant to the issue of log rotation not creating new files correctly. https://github.com/Mayil-AI/loguru/blob/871de74acb2382fb3982d3fb8e224e4a82de9073/loguru/_file_sink.py#L262-L307 This snippet includes the rotation logic based on time, which is relevant to the issue of logs being written with the wrong date. https://github.com/Mayil-AI/loguru/blob/871de74acb2382fb3982d3fb8e224e4a82de9073/loguru/_file_sink.py#L87-L154 This snippet shows the initialization of the file sink, which includes rotation, retention, and compression parameters that are relevant to the issue. https://github.com/Mayil-AI/loguru/blob/871de74acb2382fb3982d3fb8e224e4a82de9073/loguru/_file_sink.py#L31-L260 This snippet contains the function to generate a renamed path for log files, which might be relevant to the issue of incorrect file naming during rotation. https://github.com/Mayil-AI/loguru/blob/871de74acb2382fb3982d3fb8e224e4a82de9073/loguru/_file_sink.py#L1-L84 This snippet defines how rotation functions are created based on the rotation parameter, which is relevant to the issue of log rotation. https://github.com/Mayil-AI/loguru/blob/871de74acb2382fb3982d3fb8e224e4a82de9073/loguru/_file_sink.py#L41-L429
loguru → _logger.py This snippet describes how to add a handler to the logger, which includes parameters for rotation, retention, and compression that are relevant to the issue. https://github.com/Mayil-AI/loguru/blob/871de74acb2382fb3982d3fb8e224e4a82de9073/loguru/_logger.py#L253-L2094