码迷,mamicode.com
首页 > 其他好文 > 详细

logging模块

时间:2018-01-17 20:17:03      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:pos   一个   idt   use   add   cut   日期   进程   creat   

一、简述

  很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误,警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为debug,info,warning,error和critical 5个级别,下面我们就来看看这个日志模块logging怎么用

二、简单用法

1、简单用法

说明:日志级别有五个,分别是:debug,info,warning,error和critical,其中debug级别最低,critical级别最高,级别越低,打印的日志等级越多。

import logging
logging.debug("test debug")
logging.info("test info")
logging.warning("user [seven] attempted wrong password more than 3 times")
logging.error("test error")
logging.critical("server id down")

#输出
ERROR:root:test error
WARNING:root:user [seven] attempted wrong password more than 3 times
CRITICAL:root:server id down

注:从上面可以看出,一个模块默认的日志级别是warning

2、日志级别

看一下这几个日志级别分别代表什么意思,如表:

LevelWhen it’s used
DEBUG Detailed information, typically of interest only when diagnosing problems.
INFO Confirmation that things are working as expected.
WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERROR Due to a more serious problem, the software has not been able to perform some function.
CRITICAL A serious error, indicating that the program itself may be unable to continue running.

3、日志写入文件

logging.basicConfig(filename="app.log",level=logging.INFO) #定义文件名和日志输出级别
logging.debug("test debug")
logging.info("test info")
logging.warning("user [seven] attempted wrong password more than 3 times")
logging.error("test error")
logging.critical("server id down")

#日志内容
INFO:root:test info
WARNING:root:user [seven] attempted wrong password more than 3 times
ERROR:root:test error
CRITICAL:root:server id down

注: 这句中的level=loggin.INFO意思是,把日志纪录级别设置为INFO,也就是说,只有比日志是INFO或比INFO级别更高的日志才会被纪录到文件里,所以debug日志没有记录,如果想记录,则级别设置成debug也就是level=loggin.DEBUG

4、加入日期格式

说明:感觉上面的日志格式忘记加上时间啦,日志不知道时间怎么行呢,下面就来加上

logging.basicConfig(filename="app.log",
                    level=logging.INFO,
                    format=%(asctime)s %(levelname)s %(message)s,
                    datefmt="%m/%d/%Y %H:%M:%S %p") #需要加上format和datefmt
logging.debug("test debug")
logging.info("test info")
logging.warning("user [seven] attempted wrong password more than 3 times")
logging.error("test error")
logging.critical("server id down")

#日志输出
01/17/2018 17:49:05 PM INFO test info
01/17/2018 17:49:05 PM WARNING user [seven] attempted wrong password more than 3 times
01/17/2018 17:49:05 PM ERROR test error
01/17/2018 17:49:05 PM CRITICAL server id down

5、 format的日志格式

%(name)s

Logger的名字

%(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

用户输出的消息

 

logging模块

标签:pos   一个   idt   use   add   cut   日期   进程   creat   

原文地址:https://www.cnblogs.com/lin-777/p/8304234.html

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