python学习 (三十五) logging

1  demo

import logging

# log level:
# DEBUG
# INFO
# Warning
# Error
# CRITICAL

logging.basicConfig(filename
= "test.log", level=logging.DEBUG) // 配置: 设置log的输出文件和级别 logging.error("error message") logging.warning("warning message") logging.info("info message")

2  格式化输出格式

import logging

# log level:
# DEBUG
# INFO
# Warning
# Error
# CRITICAL
logging.basicConfig(format='%(asctime)s: %(levelname)s:%(message)s',
                    datefmt='%m/%d/%Y %H:%M:%S %p', level=logging.DEBUG)

logging.error
logging.error("error message")
logging.warning("warning message")
logging.info("info message")

result:

10/28/2018 22:29:50 PM: ERROR:error message
10/28/2018 22:29:50 PM: WARNING:warning message
10/28/2018 22:29:50 PM: INFO:info message

 3  logging的

import logging

class loggerDemo():
    def testLog(self):
        logger = logging.getLogger("sample_log")
        logger.setLevel(logging.INFO)

        formatter = logging.Formatter('%(asctime)s: %(levelname)s:%(message)s',
                            datefmt='%m/%d/%Y %H:%M:%S %p')

        chandler = logging.StreamHandler()
        chandler.setLevel(logging.INFO)
        chandler.setFormatter(formatter)

        logger.addHandler(chandler)

        logger.info("debug message aaaaaaa")
        logger.error("debug message aaaaaaa")
        logger.critical("debug message aaaaaaa")

dmo = loggerDemo()
dmo.testLog()

result:

10/28/2018 22:40:40 PM: INFO:debug message aaaaaaa
10/28/2018 22:40:40 PM: ERROR:debug message aaaaaaa
10/28/2018 22:40:40 PM: CRITICAL:debug message aaaaaaa

 

posted @ 2018-10-28 22:14  刘大飞  阅读(147)  评论(0编辑  收藏  举报