【问题标题】:Python 2.7: Logging Class Threaded RotatingFileHandler Buffer Overflow? Error 32Python 2.7:记录类线程 RotatingFileHandler 缓冲区溢出?错误 32
【发布时间】:2014-06-05 18:07:39
【问题描述】:

当日志记录轮换日志文件时,我收到以下错误:

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 结果(full picture):

我在 python 3.2 中看到有一个用于线程日志记录的 QueuedHandler 的实现,但我不认为这就是这里发生的事情,因为我试图在线程处理时关闭日志记录。我什至基于 python3 (like this) 中包含的 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

任何建议将不胜感激。

【问题讨论】:

    标签: python logging


    【解决方案1】:

    错误 32 就是它所说的 - 其他东西打开了文件,因此无法通过日志记录重命名。检查您是否只有一个处理程序打开该文件,它没有在编辑器中打开或被防病毒工具扫描等。

    【讨论】:

    • 由于我的类的编写方式,我打开了多个句柄。感谢import logging 的出色模块/文档。
    【解决方案2】:

    我在开发烧瓶应用程序时遇到了同样的错误。为了解决这个问题,我不得不将环境变量从 “FLASK_DEBUG=1”到 “FLASK_DEBUG=0”。 原因是打开调试会导致线程错误。 看了this blog后得到了解决方案

    【讨论】:

      猜你喜欢
      • 2017-05-12
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 2011-12-19
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多