logging 模块学习

1、When to use logging

level when to use
DEBUG 细节信息,调试程序
INFO 证实程序如期运行
WARNING 出现异常,但程序仍运行
ERROR 严重问题,程序部分功能不能运行
CRITICAL 出错,程序不能运行

2、A simple example

默认是warning!

import logging
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything

consle_result:

WARNING:root:Watch out!

3、Logging to a file

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

file_result:

DEBUG:root:This message should go to the log file
INFO:root:So should this
WARNING:root:And this, too

上例中默认是输出文件为a+模式,可以自定义:

logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)

4、Logging variable data

import logging
logging.warning('%s before you %s', 'Look', 'leap!')

result:

WARNING:root:Look before you leap!

This is for backwards compatibility: the logging package pre-dates newer formatting options such as str.format()
and string.Template
. These newer formatting options are supported

5、格式

import logginglogging.basicConfig(format='%(levelname)s[%(asctime)s] :%(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)

6、一般使用方法

import logging
# create logger
logger = logging.getLogger('simple_example')#getLogger自定义值可替换root
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch=logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

7、同时打印到控制台和输出文件

import logging
import os
log = logging.getLogger()
formatter = logging.Formatter('[%(asctime)s] [%(name)s] %(levelname)s: %(message)s') 
stream_handler = logging.StreamHandler()
file_handler = logging.FileHandler(os.path.join(os.getcwd(), 'test.log'))

file_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)

log.addHandler(file_handler)
log.addHandler(stream_handler)
log.setLevel(logging.DEBUG)

8、使用配置文件

import logging
import logging.config
logging.config.fileConfig('logging.conf')

# create logger
logger = logging.getLogger('simpleExample')

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

logging.conf 文件格式:

[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

你可能感兴趣的:(logging 模块学习)