python基础学习1-日志信息

 
#!/usr/bin/env python
# -*- coding:utf-8 -*- 日志
import logging # 5个级别的日志 DEBUG INFO WARNING ERROR CRITICAL
"""logging.warning("1111111") #输出日志信息到屏幕
logging.critical("2222222")"""

logging.basicConfig(filename='example.log',level=logging.INFO,
                    format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p' )
#保存日志信息到文件# level表示比这个定义级别高的信息才写入文件
logging.debug('this message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

 

import logging

logger =logging.getLogger('TEST-LOG')# 获取logger对象设置日志标记,不设置默认是root
logger.setLevel(logging.DEBUG)#设置全局的日志级别

ch=logging.StreamHandler()# 设置打印到屏幕的对象
ch.setLevel(logging.DEBUG)#设置打印到屏幕对象的日志级别

fh=logging.FileHandler("access.log")#设置往文件输出的对象 设置文件名称
fh.setLevel(logging.WARNING)#设置输出到文件对象的日志级别
#Formatter 参数  还有 %(filename)s-输出日志的页面文件名称  %(lineno)d-输出日志的程序行号
#%(modeule)s -页面模块名称 ,%(process)s进程名称
formatter= logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(message)s')

ch.setFormatter(formatter)#设置输出屏幕对象的格式
fh.setFormatter(formatter)#设置输出文件对象的格式

logger.addHandler(ch)#注册屏幕输出对象
logger.addHandler(fh)#注册文件输出对象

#开始打印日志信息
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

 

posted @ 2016-08-23 18:05  whzym111  阅读(183)  评论(0编辑  收藏  举报