Introduction:
Recently, I have tried to record some infomations occurred during progress running automatically. Because the main language is Python, so I choose the logging module to achieve it.
Firstly, I can record log when I use logging module. But when I tried to write log infomation two times or more, I found that the log file will be re-written with cleaning up all my infomation written last time. And then I have tested it by making two progress to write log infomating using logging module to one log file. This time I found that if the two progresses write infomation at the same time, Python Global Interpreter Lock(GIL) will prevent multiple progress form executing the I/O operations. So the fact is just one progress writted log infomation in log file.
But why the log file will be cleaned up? I searched my question in google and found the result. Because the logging module include class TimedRotatingFileHandler(BaseRotatingHandler), and we see it sources code, strengthen the reson code:
1 | def doRollover(self): |
As you see, it will remove the log file if it existed. So we try to create a class like TimedRotatingFileHandler, but support for Muti-Progress.
The coding thinking:
1 | graph TD |
When a progress request I/O operation, firstly judge it whether should Rollover? if this log file is the currenttime file you want to record it.
if is True return True, while False return False. And the doRollover receive the parameter to do rollover. When it rollover, it makes a symlink pointing to the current time file and then record it.
The main code:
1 | ... |
The whole source code:
1 | class MultiProcessSafeDailyRotatingFileHandler(BaseRotatingHandler): |
Finally
Let me tell you some usages of logging module: This module defines functions and classes which implement a flexible event logging system for applications and libraries. The key benefit of having the logging API provided by a standard library module is that all Python modules can participate in logging, so your application log can include your own messages integrated with messages from third-party modules.
Usage:
logger –> handler –> formatter –> output
Create logger
1
2logger = logging.getLogger('user')
logger.setLevel('DEBUG')Create handler and set leverl
1
2
3handler = logging.StreamHandler()
handler.setLevel('DEBUG')
`Create formatter
1
handler.setFormatter('%(levelname)s: %(message)s')
Add to logger
1
logger.addHandler(handler)
My simple:
1 | """ |