在旧的Zope模块中,我在_ init _py中找到了一个函数add_file_logging()。
使用 logger2 我尝试记录一些INFOS,在add_file_logging()中将级别设置为DEBUG。但INFOS未记录到文件中。错误记录到文件中。但是在记录错误时,函数add_file_logging()必须是例外。但是日志级别必须是问题,它没有设置。
使用 logger1 我做了相同的 - 相同的代码 - 使用类本身的函数set_logger()它只是工作正常。
所以我想知道,为什么同样的方法在导入时不起作用。有人可以解释我的区别吗?
那将是非常好的;-)
myhelper.tools/src/myhelper/tools/worker.py
import os
import sys
import logging
import logging.handlers
from myhelper.tools import add_file_logging
logger1 = logging.getLogger('myLogger1')
logger2 = logging.getLogger('myLogger2')
class Worker(object):
def __init__(self):
self.to_delete = False
...
def set_logger (self, logger, logfile):
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(logfile)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
def run(self):
self.set_logger(logger1, os.path.dirname(__file__) + "logger1.log")
add_file_logging(logger2, os.path.dirname(__file__) + "logger2.log")
...
def main():
work = XmlWorker()
work.run()
myhelper.tools/src/myhelper/tools/的初始化的.py
init .py下划线会自动删除为 init.py ; - )
# -*- coding: utf-8 -*-
import logging
import logging.handlers
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
class AboveInfoFilter(logging.Filter):
def filter(self, record):
return record.levelno > 20
def add_file_logging (logger, logfile):
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(logfile)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
答案 0 :(得分:0)
我无法重现你的发现。这个脚本:
import logging
import os
import shutil
import sys
import tempfile
CONTENTS = '''
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
class AboveInfoFilter(logging.Filter):
def filter(self, record):
return record.levelno > 20
def add_file_logging (logger, logfile):
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(logfile)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
'''
def make_helper_module():
tempdir = tempfile.mkdtemp()
helper = os.path.join(tempdir, 'myhelper')
os.mkdir(helper)
fn = os.path.join(helper, '__init__.py')
with open(fn, 'w') as f:
pass # make empty file
tools = os.path.join(helper, 'tools')
os.mkdir(tools)
fn = os.path.join(tools, '__init__.py')
with open(fn, 'w') as f:
f.write(CONTENTS)
return tempdir
tempdir = make_helper_module()
sys.path.insert(0, tempdir)
from myhelper.tools import add_file_logging
logger1 = logging.getLogger('myLogger1')
logger2 = logging.getLogger('myLogger2')
class Worker(object):
def set_logger (self, logger, logfile):
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(logfile)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
def run(self):
self.set_logger(logger1, os.path.dirname(__file__) + "logger1.log")
add_file_logging(logger2, os.path.dirname(__file__) + "logger2.log")
def main():
worker = Worker()
worker.run()
logger1.info('Hello, ')
logger2.info('world!')
td = sys.path.pop(0)
shutil.rmtree(tempdir)
print('logger1.log contains:')
with open('logger1.log') as f:
print(f.read().strip())
print('logger2.log contains:')
with open('logger2.log') as f:
print(f.read().strip())
if __name__ == '__main__':
main()
运行时,生成
Mon, 03 Jun 2013 19:29:49 INFO Hello,
Mon, 03 Jun 2013 19:29:49 INFO world!
logger1.log contains:
Hello,
logger2.log contains:
world!
这似乎是合理的。