Python logging(配置实现)

本文介绍了如何在Python中配置和使用logging模块,通过配置文件设置了root、test1和test2三种类型的日志,详细展示了如何在不同模块中调用日志并指定输出文件,以及测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义我们的日志模块

定义统一入口,读取配置文件

#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.log

  • test2.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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值