0

Python程序A:

LOG_PATH = fdoc_log + "/store_plus.log"
 FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
 logging.basicConfig(filename=LOG_PATH, filemode = 'w', level=logging.DEBUG, format=FORMAT)

bash 程序 B:

 mv store_plus.log store_plus.log.bk

程序 A 将在后台运行并且不会停止。当程序B 删除store_plus.log 文件时,程序A 也不能写入日志。如果我想让程序 A 重建 store_plus.log,如何解决?谢谢

PS:方式:

 f = open(LOG_PATH, "a")
 f.close()

它行不通。

4

2 回答 2

0

一个来自pymotw-logging的例子,全部归功于 Doug Hellmann。

import glob
import logging
import logging.handlers

LOG_FILENAME = '/tmp/logging_rotatingfile_example.out'

# Set up a specific logger with our desired output level

my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20, backupCount=5)

my_logger.addHandler(handler)

# Log some messages
for i in range(20):
   my_logger.debug('i = %d' % i)

# See what files are created
logfiles = glob.glob('%s*' % LOG_FILENAME)

for filename in logfiles:
   print filename
于 2013-01-22T09:44:24.887 回答
0

WatchedFileHandler 可以使用这种方式:

    logger = logging.getLogger('simple_example')
    logger.setLevel(logging.DEBUG)

    ch = logging.handlers.WatchedFileHandler('a_log')
    ch.setLevel(logging.DEBUG)

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

    ch.setFormatter(formatter)
    logger.addHandler(ch)
于 2013-01-22T10:08:29.367 回答