Python 2.7: 日志类线程旋转文件处理器缓冲区溢出?错误32

2 投票
2 回答
4172 浏览
提问于 2025-04-18 08:46

我在记录日志时遇到了一个错误,特别是在日志文件需要轮换的时候:

Traceback (most recent call last):
  File "C:\Python27\Lib\logging\handlers.py", line 72, in emit
    self.doRollover()
  File "C:\Python27\Lib\logging\handlers.py", line 174, in doRollover
    self.rotate(self.baseFilename, dfn)
  File "C:\Python27\Lib\logging\handlers.py", line 113, in rotate
    os.rename(source, dest)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process
Logged from file logger_example.py, line 37

PSMON的结果 (完整图片)

PSMON结果

我看到在Python 3.2中有一个叫做QueuedHandler的东西,可以用来处理多线程的日志记录,但我觉得这不是我遇到的问题。我尝试在多线程的时候关闭日志记录,结果还是不行。我甚至根据Python 3中包含的handler.py实现了一个队列版本 (像这样),但还是收到了同样的错误。显然我做错了什么,但我找不到原因,因为多个类都在写入同一个文件。

logger_example.py:

class log_this(object):
    def __init__(self,module_name, MainFrame):
        super(log_this, self).__init__()
        self.MainFrame = MainFrame
        self.module_name = module_name
        LOG_FILENAME = os.path.join('logs','test.log')
        LOG_LEVEL = logging.DEBUG
        logging.getLogger(self.module_name)
        logging.basicConfig(level=LOG_LEVEL,
                            format='\n--%(asctime)s %(funcName)s %(name)-12s %(levelname)-8s %(message)s',
                            datefmt='%m-%d %H:%M:%S',
                            filename=LOG_FILENAME,
                            filemode='w')
        #console = logging.StreamHandler()
        self.handler = logging.handlers.RotatingFileHandler(
              LOG_FILENAME, maxBytes=1000000, backupCount=5)
        #console.setLevel(logging.INFO)
        self.count = 0


    def log_info(self,name,msg):
        print('In: log_info')
        try:
            log_name = logging.getLogger(".".join([self.module_name,name]))
            log_name.addHandler(self.handler)
            if self.MainFrame.threading is False:
                log_name.info("\n%s" % (msg))
        except Exception,e:
            print(traceback.format_exc())
            print(e)
            exit()
        return

    def log_debug(self,func_name,msg,debug_info):
        try:
            log_name = logging.getLogger(".".join([self.module_name,func_name]))
            log_name.addHandler(self.handler)
            #called_frame_trace = "\n    ".join(traceback.format_stack()).replace("\n\n","\n")
            outer_frames = inspect.getouterframes(inspect.currentframe().f_back.f_back)
            call_fn = outer_frames[3][1]
            call_ln = outer_frames[3][2]
            call_func = outer_frames[3][3]
            caller = outer_frames[3][4]
            # a string with args and kwargs from log_debug
            args_kwargs_str = "\n"+str(debug_info).replace(", '","\n, '")
            if self.MainFrame.threading is False:
                results = log_name.debug("%s\nARGS_KWARGS_STR:%s\ncall_fn: %s\ncall_ln: %i\ncall_func: %s\ncaller: %s\nException:" % (
                                            msg,
                                            args_kwargs_str,
                                            call_fn,
                                            call_ln,
                                            call_func,
                                            caller
                                            ),exc_info=1)
        except Exception, e:
            print(traceback.format_exc())
            print(e)
            exit()
        return

这是在多个类和不同目录/模块中使用的示例:

from Logs.logger_example import log_this
class Ssh(object):

    def __init__(self, MainFrame):
        super(Ssh, self).__init__()
        self.MainFrame = MainFrame
        self.logger = log_this(__name__,self.MainFrame)
    def infoLogger(self,msg=None):
        this_function_name = sys._getframe().f_back.f_code.co_name
        self.logger.log_info(this_function_name,str(msg)+" "+this_function_name)
        return
    def debugLogger(self,msg=None,*args,**kwargs):
        debug_info = {'ARGS':args, 'KWARGS':kwargs}
        this_function_name = sys._getframe().f_back.f_code.co_name
        self.logger.log_debug(this_function_name,str(msg)+this_function_name,debug_info)
        return
    #-------------------------------------------------
    # Example Usage:
    # It used like this in other classes as well
    #-------------------------------------------------
    def someMethod(self):
        self.infoLogger('some INFO message:%s' % (some_var))
        self.debugLogger('Some DEBUG message:',var1,var2,var3=something)

示例输出:

--06-05 12:35:34 log_debug display_image.EventsHandlers.MainFrameEventHandler.csvFileBrowse DEBUG    Inside:  csvFileBrowse
ARGS_KWARGS_STR:
{'ARGS': ()
, 'KWARGS': {}}
call_fn: C:\Python27\lib\site-packages\wx-3.0-msw\wx\_core.py
call_ln: 8660
call_func: MainLoop
caller: ['        wx.PyApp.MainLoop(self)\n']
Exception:
None

--06-05 12:35:34 log_info display_image.EventsHandlers.MainFrameEventHandler.csvFileBrowse INFO     
Inside:  csvFileBrowse

--06-05 12:35:41 log_debug display_image.sky_scraper.fetchPage DEBUG    retailer_code,jNumberfetchPage
ARGS_KWARGS_STR:
{'ARGS': ('1', u'J176344')
, 'KWARGS': {}}
call_fn: C:\Users\User\Desktop\SGIS\SGIS_6-2-14\SGIS-wxpython-master\display_image\EventsHandlers\MainFrameEventHandler.py
call_ln: 956
call_func: onScanNumberText
caller: ['            results = self.updateMainFrameCurrentItemInfo(retailer_code)\n']
Exception:
None

如果有任何建议,我将非常感激。

2 个回答

1

我在开发一个flask应用的时候也遇到了同样的错误。为了解决这个问题,我把环境变量从 "FLASK_DEBUG=1" 改成了 "FLASK_DEBUG=0"。 这样做的原因是,开启调试模式会导致线程错误。 我是在阅读 这篇博客 后找到的解决办法。

2

错误32的意思就是:有其他程序正在使用这个文件,所以你无法通过日志来重命名它。你需要检查一下,确保只有一个程序在打开这个文件,看看它是不是在编辑器里打开,或者有没有被杀毒软件扫描等等。

撰写回答