gpt4 book ai didi

Python 2.7 : Logging Class Threaded RotatingFileHandler Buffer Overflow? 错误 32

转载 作者:太空宇宙 更新时间:2023-11-04 01:16:47 25 4
gpt4 key购买 nike

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

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

PSMON results

我看到在 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

如有任何建议,我们将不胜感激。

最佳答案

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

关于Python 2.7 : Logging Class Threaded RotatingFileHandler Buffer Overflow? 错误 32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24067158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com