2

簡単に :

base.py:

import logging
from logging.handlers import TimedRotatingFileHandler
import os

slogFile = os.path.join(os.getcwd(), 'LOGS', 'App_Debug.log')
if True != os.path.isdir(os.path.join(os.getcwd(), 'LOGS')):
    os.mkdir(os.path.join(os.getcwd(), 'LOGS'))
logging.basicConfig(filename=slogFile, level=logging.DEBUG)
logging.basicConfig(format='%(asctime)s %(message)s')
logger = logging.getLogger("myApp")   
fmt = logging.Formatter(fmt='%(asctime)s %(message)s')
size=1024*1024*1 #1mb file size
logger = logging.getLogger("myApp")   
fmt = logging.Formatter(fmt='%(asctime)s %(message)s')

hdlr = logging.handlers.RotatingFileHandler(filename = slogFile ,mode='w', maxBytes=size, backupCount=5, encoding=None, delay=0)
hdlr.setFormatter(fmt)
logger.addHandler(hdlr)</em>

app_main1.py:

import base

base.logger.debug('xxx')

app_main2.py:

import base

base.logger.debug('xxx')

注: 私のアプリケーションでは、ログを同じファイルに記録する別のモジュールを使用しています。

このエラーが発生しています:

Traceback (most recent call last):

 File "C:\Python27\lib\logging\handlers.py", line 78, in emit

   self.doRollover()

 File "C:\Python27\lib\logging\handlers.py", line 141, in doRollover

  os.rename(self.baseFilename, dfn)

WindowsError: [Error 32] The process cannot access the file because it is being used by another process

ファイルapp_main1.py、59行目から記録

これについて説明していただけますか?

最大(1MB)のサイズに達したときにログファイルをバックアップしたい。

4

3 に答える 3