简体   繁体   English

Python 2.7:记录类线程RotatingFileHandler缓冲区溢出? 错误32

[英]Python 2.7: Logging Class Threaded RotatingFileHandler Buffer Overflow? Error 32

I am receiving the following Error when logging goes to rotate the log files: 当日志记录旋转日志文件时,我收到以下错误:

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 results (full picture) : PSMON结果(全图)

PSMON结果

I saw in that in python 3.2 there is an implementation of a QueuedHandler for threaded logging, but I don't think that is what is going on here, as I have tried to turn logging off while threading. 我在python 3.2中看到,有一个用于线程日志记录的QueuedHandler的实现,但是我不认为这是正在发生的事情,因为我试图在线程化时关闭日志记录。 I even implemented a Queued Version based off of the handler.py included in python3 (like this) , but received the same error. 我什至基于python3中包含的handler.py实现了一个排队版本(像这样) ,但是收到了同样的错误。 I am obviously doing something incorrectly, but I am unable to figure it out. 我显然做错了什么,但我无法弄清楚。 As it is writing to the same file from multiple classes. 因为它正在从多个类写入同一文件。

logger_example.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

Example usage that is being used in multiple classes across several directories/modules: 在多个目录/模块的多个类中使用的示例用法:

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)

Example Output: 示例输出:

--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

Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Error 32 is what it says - something else has the file open, so it can't be renamed by logging. 错误32表示错误-其他文件已打开,因此无法通过日志记录重命名。 Check that you only have one handler opening that file, it's not open in an editor or being scanned by an antivirus tool, etc. 检查您只有一个处理程序打开该文件,没有在编辑器中打开文件或未被防病毒工具扫描等。

I got the same error while developing a flask application. 开发Flask应用程序时遇到了相同的错误。 To solve the problem, I had to change the environmental variable from "FLASK_DEBUG=1" to "FLASK_DEBUG=0". 为了解决该问题,我不得不将环境变量从“ FLASK_DEBUG = 1”更改为“ FLASK_DEBUG = 0”。 The reason being that turning debugging on leads to threading errors. 原因是打开调试会导致线程错误。 I got the solution after reading this blog 看完这篇博客我得到了解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM