import logging

 logging.basicConfig(filename='log.log',
format='%(asctime)s - %(name)s - %(levelname)s - %(module)s; %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',
level=logging.INFO,)
'''
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
'''
logging.critical('c')
logging.fatal('f')
logging.error('e')
logging.warning('w')
logging.info('i')
logging.debug('d')
logging.log(logging.INFO,'')

结果:

2017-02-21 07:59:55 AM - root - CRITICAL - s1; c
2017-02-21 07:59:55 AM - root - CRITICAL - s1; f
2017-02-21 07:59:55 AM - root - ERROR - s1; e
2017-02-21 07:59:55 AM - root - WARNING - s1; w
2017-02-21 07:59:55 AM - root - INFO - s1; i
2017-02-21 07:59:55 AM - root - INFO - s1; 333 多文件写入日志
 import logging

 # 创建文件
file_1_1 = logging.FileHandler('l1-1.log','a')
# 创建格式
fmt = logging.Formatter(fmt='%(asctime)s - %(name)s -%(levelname)s - %(module)s:%(message)s')
#文件应用格式
file_1_1.setFormatter(fmt)
file_1_2 = logging.FileHandler('l1-2.log','a')
fmt = logging.Formatter()
file_1_2.setFormatter(fmt) # 定义日志
#定义阈值
logger1 = logging.Logger('s1',level=logging.ERROR)
logger1.addHandler(file_1_1)
logger1.addHandler(file_1_2) #写日志 logger1.critical('')