如何防止日志记录对象被覆盖

时间:2016-11-05 07:52:37

标签: python logging

我正在使用第三方库,但似乎lib导入日志记录并修改了混乱我的控制台和文件记录的配置(copy.deepcopy),我正在寻找一种方法防止我的日志被修改。

我尝试复制日志记录对象并在完成后重新分配,但cant can't pickle _cffi_backend.CTypeDescr objects不允许我克隆原始对象 - 说 logging_obj = copy.deepcopy(logging) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 190, in deepcopy y = _reconstruct(x, rv, 1, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 334, in _reconstruct state = deepcopy(state, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy y = copier(x, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 257, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 190, in deepcopy y = _reconstruct(x, rv, 1, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 334, in _reconstruct state = deepcopy(state, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy y = copier(x, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 257, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 190, in deepcopy y = _reconstruct(x, rv, 1, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 334, in _reconstruct state = deepcopy(state, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 163, in deepcopy y = copier(x, memo) File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 257, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) TypeError: can't pickle _cffi_backend.CTypeDescr objects

_handlerList

所以我试图检查它的内容(但只转换为str并进行比较),我发现handler_list = copy.deepcopy(logging.__dict__['_handlerList']) obj = {} for k in logging.__dict__: obj[k] = str(logging.__dict__[k]) # some 3rd party lib calls self.logger.info("containers: {}".format(containers)) logging.__dict__['_handlerList'] = handler_list # print out different obj, will print nothing after assign handler_list back (above line) for k in logging.__dict__: if not str(logging.__dict__[k]) == obj[k]: print k print "=="*100 print logging.__dict__[k] print "--"*50 print obj[k] print "=="*100 是被修改过的,但是在我重新分配之后仍然无法工作:

{{1}}

希望有人可以帮助我,我甚至不知道我是否正确行事(可能不是......但我不知道怎样也无法找到),第3派对lib实际上是为cli用法而构建的,但我试图导入并将其视为lib。

我总是很难理解python的日志记录,如果有人也可以建议一些日志文章,它会非常棒。

非常感谢。

1 个答案:

答案 0 :(得分:0)

经过一些调试后,事实证明我的记录器被神秘地禁用了,在第3次lib调用之前,我的sheet.PageSetup.Zoom = False sheet.PageSetup.FitToPagesTall = False sheet.PageSetup.FitToPagesWide = 1

logger.__dict__

通话结束后,已禁用变为 True

{
    'name': 'log/controller.log',
    'parent': < logging.RootLogger object at 0x10cb3d190 > ,
    'handlers': [ < logging.handlers.RotatingFileHandler object at 0x10de5d4d0 > , < logging.StreamHandler object at 0x10de5da90 > ],
    'level': 20,
    'custom': < function custom at 0x10de6a668 > ,
    'disabled': 0,
    'manager': < logging.Manager object at 0x10cb3d250 > ,
    'propagate': False,
    'result': < function result at 0x10de6a6e0 > ,
    'filters': []
}

设置后({ 'name': 'log/controller.log', 'parent': < logging.RootLogger object at 0x10cb3d190 > , 'handlers': [ < logging.handlers.RotatingFileHandler object at 0x10de5d4d0 > , < logging.StreamHandler object at 0x10de5da90 > ], 'level': 20, 'custom': < function custom at 0x10de6a668 > , 'disabled': True, 'manager': < logging.Manager object at 0x10cb3d250 > , 'propagate': False, 'result': < function result at 0x10de6a6e0 > , 'filters': [] } ),我的记录器开始打印并再次登录文件。

希望这有助于遇到同样的问题。