定义我们的日志模块
定义统一入口,读取配置文件
#mylog.py
import logging
import logging.config
def getLogger(name='root'):
CONF_LOG = "/home/stephen/openstack/demo/log/log/logging.conf"
logging.config.fileConfig(CONF_LOG)
return logging.getLogger(name)
日志配置文件
我们配置了三种类型的日志,root, test1, test2,我们在使用是只需指明我们所使用的类型。
#logging.conf
[loggers]
keys=root,test1,test2
[handlers]
keys=rotatingFileHandler,test1Handler,test2Handler
[formatters]
keys=simpleFmt
[logger_root]
level=DEBUG
handlers=rotatingFileHandler
[logger_test1]
level=DEBUG
handlers=test1Handler
qualname=test1
propagate=0
[logger_test2]
level=DEBUG
handlers=test2Handler
qualname=test2
propagate=0
[handler_rotatingFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFmt
args=("/tmp/default.log", "a", 20*1024*1024, 10)
[handler_test1Handler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFmt
args=("/tmp/test1.log", "a", 20*1024*1024, 10)
[handler_test2Handler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFmt
args=("/tmp/test2.log", "a", 20*1024*1024, 10)
[formatter_simpleFmt]
format=%(asctime)s %(pathname)s(%(lineno)d): %(levelname)s %(message)s
我们的测试模块
我们在模块中的调用方式如下,就是指明我们的日志名称,也即是指明我们将这个模块的日志打到那个文件。
#test1.py
import mylog as logging
LOGGER = logging.getLogger("test1")
class TestLog1(object):
"""docstring for TestLog1"""
def __init__(self, arg=None):
super(TestLog1, self).__init__()
self.arg = arg
LOGGER.info("test1 info mesage will wirte in test1.log")
LOGGER.warning("test1 warning mesage will wirte in test1.log")
LOGGER.debug("test1 debug mesage will wirte in test1.log")
#test2.py
import mylog as logging
LOGGER = logging.getLogger("test2")
class TestLog2(object):
"""docstring for TestLog2"""
def __init__(self, arg=None):
super(TestLog2, self).__init__()
self.arg = arg
LOGGER.info("test2 info mesage will wirte in test2.log")
LOGGER.warning("test2 warning mesage will wirte in test2.log")
LOGGER.debug("test2 debug mesage will wirte in test2.log")
测试入口
测试的入口,我们执行python test.py即可
在这里我们并没有指定日志类型,也就是说没有指明我们要把日志打到那个文件,当然就会到我们定义好的默认文件root日志中:
#test.py
import mylog as logging
import test1
import test2
LOG = logging.getLogger(__name__)
def main():
LOG.info("test info message:test1.TestLog1()")
test1.TestLog1()
LOG.info("test info message: test1.TestLog2()")
test2.TestLog2()
if __name__ == '__main__':
main()
测试结果
default.log
2016-01-04 10:43:02,052 test.py(10): INFO test info message:test1.TestLog1()
2016-01-04 10:43:02,052 test.py(12): INFO test info message: test1.TestLog2()test1.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test1.py(12): INFO test1 info mesage will wirte in test1.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test1.py(13): WARNING test1 warning mesage will wirte in test1.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test1.py(14): DEBUG test1 debug mesage will wirte in test1.logtest2.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test2.py(12): INFO test2 info mesage will wirte in test2.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test2.py(13): WARNING test2 warning mesage will wirte in test2.log
2016-01-04 10:43:02,052 /home/stephen/openstack/demo/log/log/test2.py(14): DEBUG test2 debug mesage will wirte in test2.log详请参考:http://python.usyiyi.cn/python_278/library/logging.config.html
- 官方教程:http://python.usyiyi.cn/python_278/howto/logging.html#logging-basic-tutorial