Logging module
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为:5个级别 debug() info() warning() error() critical()对于级别: notset 0 debug() 10 info() 20 warning() 30 error() 40 critical() 50 critical=fatal 只有设置大于当前日志等级或等于的操作才会被记录!!!
The simplest syntax:
import logging #直接输出 logging.warning("user[HeiKe] attempted wrong password more than 3 times") #定义一个warning日志输出 logging.critical("server is down") #定义一个critical日志输出 #运行结果: WARNING:root:user[HeiKe] attempted wrong password more than 3 times CRITICAL:root:server is down
If you want to write the log in the file:
import logginglogging.basicConfig(filename="test_log.log",level=logging.INFO)logging.debug("this is a debug log!!!")logging.info("this is a info !!!")logging.warning("this is a warning!!!")写到文件里边的内容如下: INFO:root:this is a info !!! WARNING:root:this is a warning!!!其中下面这句中的filename="test_log.log" 是设置log文件、level=loggin.INFO意思是,把日志纪录级别设置为INFO,也就是说,只有比日志是INFO或比INFO级别更高的日志才会被纪录到文件里,在这个例子, 第一条日志是不会被纪录的,如果希望纪录debug的日志,那把日志级别改成DEBUG就行了。
Log plus time:
import logginglogging.basicConfig(filename="test_log.log",level=logging.INFO,format="%(asctime)s %(message)s",datefmt="%Y-%m-%d %H:%M:%S %p") #%p 代表上午或者下午logging.debug("this is a debug log!!!")logging.info("this is a info !!!")logging.warning("this is a warning!!!")结果写入文件内容: 2018-01-23 17:41:39 PM this is a info !!! 2018-01-23 17:41:39 PM this is a warning!!!
Log 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 用户输出的消息
If you want to print log in the screen and file log at the same time, you need to know a little bit more complex knowledge.
python logging模块由几个分类组成: logger提供了应用程序可以直接使用的接口;对外暴露接口; handler将(logger创建的)日志记录发送到合适的目的输出; filter提供了过滤日志来决定输出哪条日志记录; formatter决定日志记录的最终输出格式。
Logger
每个程序在输出信息之前都要获得一个Logger。Logger通常对应了程序的模块名,比如聊天工具的图形界面模块可以这样获得它的Logger:LOG=logging.getLogger(”chat.gui”)而核心模块可以这样:LOG=logging.getLogger(”chat.kernel”)Logger.setLevel(lel):指定最低的日志级别,低于lel的级别将被忽略。debug是最低的内置级别,critical为最高Logger.addFilter(filt)、Logger.removeFilter(filt):添加或删除指定的filterLogger.addHandler(hdlr)、Logger.removeHandler(hdlr):增加或删除指定的handlerLogger.debug()、Logger.info()、Logger.warning()、Logger.error()、Logger.critical():可以设置的日志级别
Handler
The handler object is responsible for sending relevant information to the specified destination. Python's log system can be used in a variety of handler. Some handler can output information to the console, some logger can output the information to a file, and some handler can send information to the network. If you feel that it is not enough, you can write your own handler. Multiple Handler handler.setlevel (LEL) can be added through the AddHandler () method: Specifies the level of information being processed, and information below the LEL level is ignored Handler.setformatter () : Choose a format for this Handler handler.addfilter (filt), Handler.removefilter (filt): Add or remove a filter object each logger can append multiple Handler. Next, let's introduce some common handler:1) logging. Streamhandler uses this handler to output information to any file object, such as Sys.stdout or Sys.stderr. Its constructor is: Streamhandler ([STRM]) where the STRM parameter is a file object. Default is SYS.STDERR2) logging. Filehandler and Streamhandler are similar for outputting log information to a file. But Filehandler will open this file for you. Its constructor is: Filehandler (Filename[,mode]) filename is the file name and you must specify a file name. Mode is how the file is opened. See the use of the Python built-in function open (). The default is ' a ', which is added to the end of the file. 3) Logging.handlers.RotatingFileHandler This handler is similar to the filehandler above, but it can manage the file size. When the file reaches a certain size, it automatically renames the current log file and then creates a new log file with the same name to continue the output. For example, the log file is Chat.log. When the chat.log reaches the specified size, Rotatingfilehandler automatically renames the file to Chat.log.1. However, if chat.log.1 already exists, it will first put Chat.loG.1 renamed to Chat.log.2 ... Finally, re-create the Chat.log and continue to output the log information. Its constructors are: Rotatingfilehandler (filename[, mode[, maxbytes[, Backupcount]]) where filename and mode two parameters are the same as Filehandler 。 The maxbytes is used to specify the maximum file size for the log file. If MaxBytes is 0, it means that the log file can be infinitely large, and the renaming process described above does not occur. The backupcount is used to specify the number of reserved backup files. For example, if you specify 2, when the renaming process described above occurs, the original chat.log.2 is not renamed, but is deleted. 4) Logging.handlers.TimedRotatingFileHandler This handler is similar to Rotatingfilehandler, however, it does not determine the size of the file to decide when to recreate the log file, but instead of a certain time interval Creates a new log file automatically. The process of renaming is similar to Rotatingfilehandler, but the new file is not an appended number but the current time. Its constructor is: Timedrotatingfilehandler (filename [, when [, interval [, Backupcount]]]) where the filename parameter and the Backupcount parameter and R Otatingfilehandler have the same meaning. Interval is the time interval. The When parameter is a string. A unit that represents a time interval, not case-sensitive. It has the following values: s seconds M minutes H hours D days W per week (Interval==0 on behalf of Monday) midnight every morning
A simple example of a log output to a terminal and a file:
import logging#创建一个logger实例对象logger = logging.getLogger("own_definition")logger.setLevel(logging.DEBUG) #定义全局日志级别#创建一个输出到屏幕的对象screen = logging.StreamHandler()screen.setLevel(logging.INFO) #定义工作域日志级别#创建一个输出到文件的对象file = logging.FileHandler("testtest.log")file.setLevel(logging.CRITICAL) #定义工作域日志级别#创建一个日志输出格式log_output_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")#把日志输出格式 赋予给 屏幕和文件 对象screen.setFormatter(log_output_format)file.setFormatter(log_output_format)#把 屏幕和文件 两个handle 赋予给 loggerlogger.addHandler(screen)logger.addHandler(file)#使用logger手动生成日志信息logger.debug("debug is log!!")logger.info("info is log!!")logger.warn("warn is log!!")logger.error("error is log!!")logger.critical("critical is log!!")
After setting the global and working domain log levels several times, the experiment results are concluded:
1、当全局日志低于工作域日志,工作域日志优先级高;2、当全局日志高于工作域日志,全局日志优先级高;3、当工作域日志没有设置的时候,继承全局日志;4、当全局日志没有定义的时候,默认全局日志级别为WARNING。
Log file Automatic cutting:
import loggingfrom logging import handlers#创建一个logger实例对象logger = logging.getLogger(__name__)#定义自动生成的对象log_file = "testtesttest.log"#自动生成一:通过设置日志文件大小 自动备份创建log文件 如:log log.1 log.2#auto_split_file = handlers.RotatingFileHandler(filename=log_file,maxBytes=10,backupCount=2)#自动生成二:通过设置时间间隔 自动创建log文件 如:testtesttest.log testtesttest.log.2018-01-24_14-49-47 testtesttest.log.2018-01-24_14-49-48auto_time_split_file = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=1,backupCount=2)#创建一个日志输出格式log_output_format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")#把日志输出格式 赋予给 自动生成日志的 对象auto_time_split_file.setFormatter(log_output_format)#把 自动生成 handle 赋予给 loggerlogger.addHandler(auto_time_split_file)#使用logger手动生成日志信息logger.debug("debug is log!!")logger.info("info is log!!")logger.warn("warn is log!!")logger.error("error is log!!")logger.critical("critical is log!!")
Python logging module