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

python logging模块

时间:2017-12-12 15:12:04      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:定义   proc   str   日志   post   时间格式   程序   war   格式   

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/12/8 0008 14:30
# @Author  : ming

import logging

logging.basicConfig(level=logging.WARNING,  # 大于等于warning级别的才会被记录
                    format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s,
                    datefmt=%a, %d %b %Y %H:%M:%S,
                    filename=myapp.log,
                    filemode=w, )

logging.debug(This is debug message)
logging.info(This is info message)
logging.warning(This is warning message)
logging.error(This is error message)
logging.critical("This is critical message")
logging.log(logging.ERROR, This is error message)  # 也可以自己写到日志级别
logging.basicConfig函数各参数:
filename: 指定日志文件名
filemode: 和file函数意义相同,指定日志文件的打开模式,wa
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
 %(levelno)s: 打印日志级别的数值
 %(levelname)s: 打印日志级别名称
 %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
 %(filename)s: 打印当前执行程序名
 %(funcName)s: 打印日志的当前函数
 %(lineno)d: 打印日志的当前行号
 %(asctime)s: 打印日志的时间
 %(thread)d: 打印线程ID
 %(threadName)s: 打印线程名称
 %(process)d: 打印进程ID
 %(message)s: 打印日志信息
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
日志级别大小关系为:CRITICAL(50) > ERROR(40) > WARNING(30) > INFO(20) > DEBUG(10) > NOTSET(0),当然也可以自己定义日志级别。
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

 

运行结果
Tue, 12 Dec 2017 14:08:45 l1.py[line:16] WARNING This is warning message
Tue, 12 Dec 2017 14:08:45 l1.py[line:17] ERROR This is error message
Tue, 12 Dec 2017 14:08:45 l1.py[line:18] CRITICAL This is critical message
Tue, 12 Dec 2017 14:08:45 l1.py[line:19] ERROR This is error message

 

python logging模块

标签:定义   proc   str   日志   post   时间格式   程序   war   格式   

原文地址:http://www.cnblogs.com/ming5218/p/8027498.html

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