码迷,mamicode.com
首页 > 编程语言 > 详细

python之logging模块

时间:2016-03-22 19:28:15      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

参考:http://blog.csdn.net/zyz511919766/article/details/25136485

 

logging模块

日志有5个级别,分别为debug,info,warning,error,critical,对应的级别数字分别为10,20,30,40,50

将日志标准输出到屏幕上

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import logging

logging.debug("welcome to akon debug system")
logging.info("welcome to akon info system")
logging.warning("welcome to akon warning system")
logging.error("welcome to akon error system")
logging.critical("welcome to akon critical system")

输出结果:

WARNING:root:welcome to akon warning system
ERROR:root:welcome to akon error system
CRITICAL:root:welcome to akon critical system

由结果可见,默认情况下只会打印等于或高于warning级别的日志。

将日志以自定义的格式保存到文件中

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import logging
logging.basicConfig(level=logging.DEBUG,
                    format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s,
                    datefmt=%a, %d %b %Y %H:%M:%S,
                    filename=access.log,
                    filemode=a)

logging.debug(debug message)
logging.info(info message)
logging.warning(warning message)
logging.error(error message)
logging.critical(critical message)

查看access.log文件内容:

Tue, 22 Mar 2016 17:00:42 log.py[line:18] DEBUG debug message
Tue, 22 Mar 2016 17:00:42 log.py[line:19] INFO info message
Tue, 22 Mar 2016 17:00:42 log.py[line:20] WARNING warning message
Tue, 22 Mar 2016 17:00:42 log.py[line:21] ERROR error message
Tue, 22 Mar 2016 17:00:42 log.py[line:22] CRITICAL critical message

 

用logging.baseConfig()函数定义日志输出格式,参数如下:

filename:用指定的文件名创建FiledHandler,这样日志会被存储在指定的文件中。
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
format:指定handler使用的日志显示格式。 
datefmt:指定日期时间格式。 
level:设置rootlogger(后边会讲解具体概念)的日志级别 
stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

 

format参数中可能用到的格式化串:

%(name)s logger的名字,默认为root
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径名(可能没有)
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮点数表示
%(relativeCreated)d 输出日志信息时的,自Logger创建以来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d 线程ID(可能没有)
%(threadName)s 线程名(可能没有)
%(process)d 进程ID(可能没有)
%(message)s用户输出的消息

将日志同时输出到屏幕和保存到文件中

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import logging

# create logger
logger = logging.getLogger("mylog")
logger.setLevel(logging.DEBUG)
# create formatter创建日志输出格式
formatter = logging.Formatter(format("%(asctime)s %(name)s %(levelname)s %(message)s"),
                              datefmt="%a %d %b %Y,%H:%M:%S")
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create file handler and set level to debug
fh = logging.FileHandler("mytestlog.log")
fh.setLevel(logging.WARNING)

# add formatter to ch and fh把输出格式赋给ch和fh
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add ch and fh to logger
logger.addHandler(ch)
logger.addHandler(fh)

logger.debug("{} debug message".format("akon"))
logger.info("{} info message".format("akon"))
logger.warning("{} warning message".format("akon"))
logger.error("{} error message".format("akon"))
logger.critical("{} critical message".format("akon"))

屏幕输出为:

Tue 22 Mar 2016,18:18:36 mylog INFO akon info message
Tue 22 Mar 2016,18:18:36 mylog WARNING akon warning message
Tue 22 Mar 2016,18:18:36 mylog ERROR akon error message
Tue 22 Mar 2016,18:18:36 mylog CRITICAL akon critical message

文件保存为:

Tue 22 Mar 2016,18:18:36 mylog WARNING akon warning message
Tue 22 Mar 2016,18:18:36 mylog ERROR akon error message
Tue 22 Mar 2016,18:18:36 mylog CRITICAL akon critical message

 

python之logging模块

标签:

原文地址:http://www.cnblogs.com/akon0207/p/5307979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!