lxgeek
lxgeek

Reputation: 1814

How to clear the log file and write it again?

I create the log file by this way:

    global logger
    logger = logging.getLogger("plus_dig_cname")
    logger.setLevel(logging.DEBUG)

    fh = logging.FileHandler( fdoc_log + "/plus_dig_cname.log" )

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)

    logger.addHandler(fh)

and when the size of plus_dig_cname.log is bigger than 300MB,I handle it by a shell script, the main process is:

 mv $LOG_DIR/$1 $LOG_DIR/$1.bk
 [ $? -ne 0 ] && return 1
 touch $LOG_DIR/$1
 [ $? -ne 0 ] && return 1
 chmod 666 $LOG_DIR/$1
 [ $? -ne 0 ] && return 1

just mv it and touch a new one.
The problem is the logger can't wirte anything in the file of plus_dig_cname.log. The logging can't work.
Maybe it can be solved it by:

with open( "plus_dig_cname.log", "w" ):
               pass

this way can get a new log file by Python. But i want to get the new log file by Bash.

So, why the logging can't work after "mv touch chmod"?

Thank you

Upvotes: 7

Views: 6658

Answers (2)

jfs
jfs

Reputation: 414685

The file is opened when you create FileHandler() or on the first write to it. If you move the file; it's gone.

You could use logging.handlers.RotatingFileHandler as @sberry suggested to rotate the file using Python.

If you rotate log files externally e.g., using logrotate then you could notify your script that the file is gone e.g., by restarting it or sending a signal in the postrotate step.

Your script could reopen the file automatically if you use something like inotify to detect the change. Compare how tail -f and tail -F work.

Upvotes: 1

sberry
sberry

Reputation: 132098

Use a RotatingFileHandler to have this handled for you.

For example:

MAX_SIZE = 300 * 1024 * 1024
LOG_PATH = fdoc_log + "/plus_dig_cname.log"
fh = logging.handlers.RotatingFileHandler(LOG_PATH, maxBytes=MAX_SIZE, backupCount=5)

This will create 5 backups of 300MB each.

Upvotes: 6

Related Questions