【1.78】logging模块--logging

import logging

# 重要的模块

logging.debug("debug infomation")
logging.info("info infomation")
logging.warning("warning infomation")
logging.error("error infomation")
logging.critical("critical infomation")

#执行 上面的代码就有一下的打印

# WARNING:root:warning infomation
# ERROR:root:error infomation
# CRITICAL:root:critical infomation
logging 基本   用于便捷记录日志且线程安全的模块
等级级别值:
CRITICAL = 50 FATAL = CRITICAL ERROR = 40 WARNING = 30 WARN = WARNING INFO = 20 DEBUG = 10 NOTSET = 0
定义log 的配置:

import
logging # 重要的模块 #不定义也可以打印 如第一个 #定义基本配置 #注意每一个配置都要有逗号来隔开,最后一个配置后可以保留逗号 logging.basicConfig( filename="test_logging", #不定义直接打印在屏幕上,定义后输出日志到文件test_logging filemode="w", #默认是 a 模式 就是追加 format="%(asctime)s-%(name)s-%(levelname)s: %(message)s", #定义单条日志的输出格式 #level=logging.INFO, 可以这样写 也可以直接写等级值 20 datefmt='%Y-%m-%d %H:%M:%S %p', #定义时间的格式 2019-04-24 22:55:54 PM 默认格式:2019-04-24 22:55:05 level=20 #只有【当前写等级】大于【日志等级】时,日志文件才被记录。 ) logging.debug("debug infomation") logging.info("info infomation") logging.warning("warning infomation") logging.error("error infomation") logging.critical("critical infomation") #定义该条日志的等级 级别值 logging.log(30,'log') #执行 上面的代码就有一下的打印 # 2019-04-24 22:56:45 PM-root-INFO: info infomation # 2019-04-24 22:56:45 PM-root-WARNING: warning infomation # 2019-04-24 22:56:45 PM-root-ERROR: error infomation # 2019-04-24 22:56:45 PM-root-CRITICAL: critical infomation # 2019-04-24 22:56:45 PM-root-WARNING: log

 123: 为logging模块指定全局配置,针对所有logger有效,控制打印到文件中 

为logging模块指定全局配置,针对所有logger有效,控制打印到文件中


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



#格式
%(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.basicConfig()

 

posted @ 2016-04-24 22:22  科学小怪癖  阅读(88)  评论(0)    收藏  举报