Issue
My log files in the past few month is looking like this:
2023-04-05.log
2023-04-06.log
2023-04-07.log.2023-05-13
2023-04-07.log.2023-05-14
2023-04-07.log.2023-05-15
2023-04-07.log.2023-05-16
2023-04-07.log.2023-05-17
2023-04-08.log
2023-04-09.log
2023-04-10.log
2023-04-11.log
2023-04-11.log.2023-04-11
2023-04-12.log
2023-04-12.log.2023-04-12
The logger code is as follows:
info_file_name = 'info-' + time.strftime('%Y-%m-%d', time.localtime(time.time())) + '.log'
info_handler = TimedRotatingFileHandler(filename='logs/info/' + info_file_name, when="midnight", backupCount=7,
encoding='utf-8')
info_handler.setFormatter(formatter)
info_handler.setLevel(logging.DEBUG)
I used logger for my flask project and the log file keep created in a weird way. At first, I thought it related to the machine time so it's duplicated. But some of the log files are created with the name of the day of last month and than append the current day. What should I do to make it creating log files as expected (no weird file)?
Solution
Now, I understand your query. It is working exactly as you specify.
- Create a log called (the date when the program was started).
- Everyday, rename that file to (the date the program was started) plus date suffix.
You pass a string of the date when the program was run into the rotating handler creation. Imagine you had passed in a logfile name like my-app-log
. In that case, you'd get my-app-log
each day, and the previous day would be rotated to my-app-log.previous-date
. That is just how TimedRotatingFileHandler, or any rotating logger works really. If you want a new file name, you probably need to create a new logger instance each day, rather then giving the handler a one-time static string of the current date when the handler creation is called.
Answered By - theherk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.