E
E
Evgeny Elizarov2018-10-11 10:25:02
linux
Evgeny Elizarov, 2018-10-11 10:25:02

Analysis of server logs in Python. The script hangs during logrotate, how to deal with it?

I use a self-written script to analyze some logs. The script reads the log in a loop constantly and when logrotate is processed (the old log goes to the archive and a new, clean file appears) it freezes. How to deal with it?
I read the log like this

def monitor(file):
    file.seek(0,2)
    while True:
        line = file.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

loglines = monitor(logfile)
for line in loglines
    ....

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Shitskov, 2018-10-11
@Zarom

In your case, it is necessary not to keep the log files continuously open. Opened, counted available lines or their n-th number into the buffer, closed. Pause, repeat the cycle.
The problem is that after rotation, the open file descriptor continues to point to the old file.

R
Reversaidx, 2018-10-11
@Reversaidx

The most obvious solution is to copy it to a tempo place, or if the logs are not large, then read the entire file not line by line

A
Artyom Innokentiev, 2018-10-11
@artinnok

You should use the default package logging and TimedRotatingFileHandler - this is already correctly implemented and immediately with log rotation.
config example:

LOGGING = {
    "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"]
        }
    },
}

write logs like this:
import logging

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question