Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent log file truncation with python logging module?

Tags:

python

logging

I need to use python logging module to print debugging info to a file with statements like:

logging.debug(something)

The file is truncated (i am assuming - by the logging module) and the messages get deleted before I can see them - how can that be prevented?

Here is my logging config:

logging.basicConfig(
    level = logging.DEBUG,
    format = '%(asctime)s %(levelname)s %(message)s',
    filename = '/tmp/my-log.txt',
    filemode = 'w'
)

Thanks!

like image 602
Evgeny Avatar asked Oct 05 '09 18:10

Evgeny


1 Answers

logging

If you run the script repeatedly, the additional log messages are appended to the file. To create a new file each time, you can pass a filemode argument to basicConfig() with a value of 'w'. Rather than managing the file size yourself, though, it is simpler to use a RotatingFileHandler.

To prevent overwriting the file, you should not set filemode to 'w', or set it to 'a' (that is the default setting anyway).

I believe that you are simply overwriting the file.

like image 143
Esteban Küber Avatar answered Nov 14 '22 21:11

Esteban Küber