Python logging(代码实现)

定义我们的日志模块

我们可以指定我们的模块打印到相应的日志文件,统一调用我们的mylog.getLogger(name=’root’, logtype=None, filename=None)
logtype是我们定义的日志类型,同一类型的日志打到同一文件中,当然这些类型都是我们可以定制的。

#mylog.py
import logging
import logging.handlers


class MyLogging(object):
    """docstring for MyLogging"""

    def __init__(self, name, logtype=None, filename=None):
        super(MyLogging, self).__init__()

        logfiles = {"test1":"/tmp/test1.log",
                    "test2":"/tmp/test2.log",
                    "default":"/tmp/default.log"}

        self.name = name        
        if logtype is None:
            logtype = "default"
        self.filename = logfiles[logtype]
        self.formatter = "%(asctime)s %(pathname)s(%(lineno)d): %(levelname)s %(message)s"
        self.loglevel = logging.DEBUG



    def getLogger(self):
        handler = logging.handlers.RotatingFileHandler(self.filename,maxBytes=20*1024*1024,backupCount=10) 
        formatter = logging.Formatter(self.formatter)
        handler.setFormatter(formatter)
        logger = logging.getLogger(self.name)
        logger.addHandler(handler)
        logger.setLevel(self.loglevel)
        return logger


loggers = {}

def getLogger(name='root', logtype=None, filename=None):

    if name not in loggers:
        loggers[name] = MyLogging(name, logtype, filename).getLogger()
    return loggers[name]

我们的测试模块

我们在模块中的调用方式如下,多一个参数,就是指明我们的日志类型,也即是指明我们将这个模块的日志打到那个文件。

#test1.py
import mylog as logging

LOGGER = logging.getLogger(__name__,"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(__name__,"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即可
在这里我们并9没有指定日志类型,也就是说没有指明我们要把日志打到那个文件,当然就会到我们定义好的默认文件中:

#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 09:55:10,466 test.py(8): INFO test info message:test1.TestLog1()
    2016-01-04 09:55:10,466 test.py(10): INFO test info message: test1.TestLog2()

  • test1.log
    2016-01-04 09:55:10,466 /home/stephen/openstack/demo/log/log/test1.py(11): INFO test1 info mesage will wirte in test1.log
    2016-01-04 09:55:10,466 /home/stephen/openstack/demo/log/log/test1.py(12): WARNING test1 warning mesage will wirte in test1.log
    2016-01-04 09:55:10,466 /home/stephen/openstack/demo/log/log/test1.py(13): DEBUG test1 debug mesage will wirte in test1.log

  • test2.log
    2016-01-04 09:55:10,466 /home/stephen/openstack/demo/log/log/test2.py(11): INFO test2 info mesage will wirte in test2.log
    2016-01-04 09:55:10,467 /home/stephen/openstack/demo/log/log/test2.py(12): WARNING test2 warning mesage will wirte in test2.log
    2016-01-04 09:55:10,467 /home/stephen/openstack/demo/log/log/test2.py(13): 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、付费专栏及课程。

余额充值