The analysis of server logs to Python. Hangs script when logrotate, how to fight?

Use home-made script to analyze some logs. Script in a loop continuously reads the log at working logrotate (old log goes into the archive and creates a new, blank file) - freezes. How to deal with it?
Log read here
def monitor(file):,2)
 while True:
 line = file.readline()
 if not line:
 yield line

loglines = monitor(logfile)
for line in loglines
March 12th 20 at 08:39
3 answers
March 12th 20 at 08:41
In your case you need not to keep the log files continuously open. Opened, believed in the buffer available line or the n-th number of them, closed. Pause, repeat cycle.

The problem is that after rotation of the handle to the open file continues to point to the old file.
Else in the config file logrotate needs to restart the script using postrotate/endscript . Description and examples in help. For example, I restart php-fpm - Federico.Crona commented on March 12th 20 at 08:44
March 12th 20 at 08:43
The most obvious solution , to copy it in the tempo of the place, or if the logs are not large to read not the lines but the whole file
Neither is perfect, it reads it line by line and processes real-time - Elyse_Monahan commented on March 12th 20 at 08:46
March 12th 20 at 08:45
You should use default package logging and TimedRotatingFileHandler is already correctly implemented and directly with log rotation.

example config:
 "version": 1,
 "disable_existing_loggers": False,
 "formatters": {
 "rotate": {"format": "%(levelname)s %(asctime)s %(pathname)s %(lineno)d %(message)s"}
 "handlers": {
 "file_rotate": {
 "level": "DEBUG",
 "class": "logging.handlers.TimedRotatingFileHandler",
 "formatter": "rotate",
 "filename": "/var/log/my_logger.log",
 "when": "midnight",
 "loggers": {
 "my_logger": {
 "level": "DEBUG", 
 "handlers": ["file_rotate"]

the logs we write this:
import logging

my_logger = logging.getLogger("my_logger")
my_logger.debug('debug message')'info message')
my_logger.error('error message')
my_logger.critical('critical message')

Find more questions by tags PythonLinux