Log module logging
Logging module can be based on custom log information, when the program is running, the log is printed in the terminal and logging to the file. Here's a look at the logs supported by logging five levels
Debug () debugging level, typically used to log details of program runs
Info () event level, typically used to record the program's running process
Warnning () warning level, typically used to log a scenario where a program has a potential error
Error level, generally used to log program errors, but does not affect overall operation
Critical critical Error level, this error has affected the overall operation
- Simple usage, print logs to Terminal
#!/usr/bin/env python# -*- coding: UTF-8 -*-#pyversion:python3.5#owner:fuzjimport logginglogging.debug("User %s is loging" % 'jeck')logging.info("User %s attempted wrong password" % 'fuzj')logging.warning("user %s attempted wrong password more than 3 times" % 'mary')logging.error("select db is timeout")logging.critical("server is down")
Output Result:
WARNING:root:user mary attempted wrong password more than 3 timesERROR:root:select db is timeoutCRITICAL:root:server is down
Find out why there is no debug and info logs? Originally, by default, logging prints logs to the screen with a log level of warning. Debug and info are below the warnning level, so do not print. We can adjust the terminal output level, for beauty, custom log format
#!/usr/bin/env python# -*- coding: UTF-8 -*-#pyversion:python3.5#owner:fuzjimport logginglogging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S')logging.debug("User %s is loging" % 'jeck')logging.info("User %s attempted wrong password" % 'fuzj')logging.warning("user %s attempted wrong password more than 3 times" % 'mary')logging.error("select db is timeout")logging.critical("server is down")
Output results
Thu, 09 Jun 2016 11:42:08 日志.py[line:13] DEBUG User jeck is logingThu, 09 Jun 2016 11:42:08 日志.py[line:14] INFO User fuzj attempted wrong passwordThu, 09 Jun 2016 11:42:08 日志.py[line:15] WARNING user mary attempted wrong password more than 3 timesThu, 09 Jun 2016 11:42:08 日志.py[line:16] ERROR select db is timeoutThu, 09 Jun 2016 11:42:08 日志.py[line:17] CRITICAL server is down
The output of such a result looks passable. Now the brain complements the Logging.basicconfig function used above.
Now with the new requirements, I can't just print the log to the terminal Yes, I want to log what to do?
Very simple, use the above logging.basicconfig can be done
#!/usr/bin/env python# -*- coding: UTF-8 -*-#pyversion:python3.5#owner:fuzjimport logging#在logging.basciConfig中指定文件和写入方式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="test.log",filemode='a')logging.debug("User %s is loging" % 'jeck')logging.info("User %s attempted wrong password" % 'fuzj')logging.warning("user %s attempted wrong password more than 3 times" % 'mary')logging.error("select db is timeout")logging.critical("server is down")
Output effect:
This does not have terminal output, found in the sibling directory generated a test.log file, open to see, the original log has been written in, and the above terminal output the same
Thu, 09 Jun 2016 11:57:41 日志.py[line:15] DEBUG User jeck is logingThu, 09 Jun 2016 11:57:41 日志.py[line:16] INFO User fuzj attempted wrong passwordThu, 09 Jun 2016 11:57:41 日志.py[line:17] WARNING user mary attempted wrong password more than 3 timesThu, 09 Jun 2016 11:57:41 日志.py[line:18] ERROR select db is timeoutThu, 09 Jun 2016 11:57:41 日志.py[line:19] CRITICAL server is down
Same output screen and logging
Now there are new requirements, I can not just log to the file, some log information I still want to directly on the screen output, but also does not affect the logging file, this process is more complex, first prepare the basic knowledge
- Four components of the logging
- Loggers provides interfaces that applications can use directly
- Handlers send logs to the appropriate destination
- Filters provides a way to filter log information
- Formatters specifying the Log display format
See the code below
#!/usr/bin/env python#-*-coding:utf-8-*-#pyversion:p ython3.5#owner:fuzjimport Logginglogger = Logging.getlogger ("test.conf") #创建一个logger, default to root loggerlogger.setlevel (logging. DEBUG) #设置全局log级别为debug. Note Global precedence is highest hterm = logging. Streamhandler () #创建一个终端输出的handler, set the level to Errorhterm.setlevel (logging. ERROR) hfile = logging. Filehandler ("Access.log") #创建一个文件记录日志的handler, set the level to Infohfile.setlevel (logging.info) formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') #创建一个全局的日志格式hterm. Setformatter (Formatter) #将日志格式应用到终 End Handlerhfile.setformatter (Formatter) #将日志格式应用到文件handlerlogger. AddHandler (hterm) # Add Terminal handler to Loggerlogger.addhandler (hfile) #将文件handler添加到logger # define the log msg, note that this is logger, not logging Logger.debug (" User%s is loging "% ' Jeck ') logger.info (" User%s attempted wrong password "% ' Fuzj ') logger.warning (" User%s attempted Wron G Password more than 3 times "% ' Mary ') logger.error (" Select DB is Timeout ") logger.critical (" Server was down ")
Results:
In the terminal output:
2016-06-09 12:22:25,283 - test.conf - ERROR - select db is timeout2016-06-09 12:22:25,283 - test.conf - CRITICAL - server is down
Write in log file:
2016-06-09 12:22:25,283 - test.conf - INFO - User fuzj attempted wrong password2016-06-09 12:22:25,283 - test.conf - WARNING - user mary attempted wrong password more than 3 times2016-06-09 12:22:25,283 - test.conf - ERROR - select db is timeout2016-06-09 12:22:25,283 - test.conf - CRITICAL - server is down
Isn't it big on? Don't get excited, it's just a simple implementation. The logging module functions much more than that. The following is an in-depth analysis of the four components of logging
To start with the dry goods, please see the following script:
#!/usr/bin/env python#-*-coding:utf-8-*-#pyversion:p ython3.5#owner:fuzjimport logging# Create Loggerlogger = Logging.getlogger () #创建默认loggerlogger1 = Logging.getlogger ("Testlog") #创建一个名为testlog1的logger实例logger1logger2 = Lo Gging.getlogger ("Testlog") #创建一个名为testlog1的logger实例logger2logger3 = Logging.getlogger ("Testlog.child") # Create a Testlog child instance logger3# set the logger log level logger1.setlevel (logging. DEBUG) #将logger1日志级别设置为debuglogger2. SetLevel (Logging.info) #将logger1日志级别设置为infologger3. SetLevel (logging. ERROR) #将logger1日志级别设置为warning # Create Handlerhterm = logging. Streamhandler () #输出到终端hfile = logging. Filehandler ("Test.log") #输出到文件 # defines the log format formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') #将日志格式应用到handlerhterm. Setformatter (Formatter) Hfile.setformatter (Formatter) #给logger添加handlerlogger. AddHandler (hterm) Logger.addhandler (hfile) Logger1.addhandler (hterm) Logger1.addhandler (hfile) Logger2.addhandler (hterm) Logger2.addhandler (hfile) Logger3.addhandler (Hterm)Logger3.addhandler (hfile) #记录日志信息logger. Debug (' Logger debug message ') Logger.info (' Logger info message ') Logger.warning (' Logger warning message ') logger.error (' Logger error message ') logger.critical (' Logger critical Message ') Logger1.debug (' logger1 debug Message ') Logger1.info (' logger1 info message ') logger1.warning (' Logger1 warning Message ') logger1.error (' logger1 error message ') logger1.critical (' logger1 critical message ') Logger2.debug (' Logger2 Debug Message ') Logger2.info (' logger2 info message ') logger2.warning (' logger2 warning message ') logger2.error (' Logger2 Error message ') logger2.critical (' logger2 critical message ') Logger3.debug (' logger3 debug Message ') Logger3.info (' LOGGER3 Info message ') logger3.warning (' Logger3 warning message ') logger3.error (' logger3 error message ') Logger3.critical (' logger3 critical message ')
Result: File and terminal output:
2016-06-09 14:55:46,136-root-warning-logger WARNING message2016-06-09 14:55:46,136-root-error-logger ERROR mes sage2016-06-09 14:55:46,136-root-critical-logger CRITICAL message2016-06-09 14:55:46,136-testlog-info-logger1 Info message2016-06-09 14:55:46,136-testlog-info-logger1 Info message2016-06-09 14:55:46,137-testlog-warning-l Ogger1 warning message2016-06-09 14:55:46,137-testlog-warning-logger1 warning message2016-06-09 14:55:46,137-testl Og-error-logger1 error message2016-06-09 14:55:46,137-testlog-error-logger1 error message2016-06-09 14:55:46,137 -Testlog-critical-logger1 CRITICAL message2016-06-09 14:55:46,137-testlog-critical-logger1 CRITICAL message201 6-06-09 14:55:46,137-testlog-info-logger2 Info message2016-06-09 14:55:46,137-testlog-info-logger2 Info messag e2016-06-09 14:55:46,137-testlog-warning-logger2 WARNING message2016-06-09 14:55:46,137-testlog-warning-logger 2 Warning message2016-06-09 14:55:46,137-testlog-error-logger2 error message2016-06-09 14:55:46,137-testlog-error-logger2 error MESSAG e2016-06-09 14:55:46,137-testlog-critical-logger2 CRITICAL message2016-06-09 14:55:46,137-testlog-critical-log Ger2 critical message2016-06-09 14:55:46,137-testlog.child-error-logger3 ERROR message2016-06-09 14:55:46,137-test Log.child-error-logger3 error message2016-06-09 14:55:46,137-testlog.child-error-logger3 error message2016-06-09 14:55:46,137-testlog.child-critical-logger3 CRITICAL message2016-06-09 14:55:46,137-testlog.child-critical-lo Gger3 Critical message2016-06-09 14:55:46,137-testlog.child-critical-logger3 critical message
Logger a tree hierarchy, before outputting the information to obtain a Logger,logger = Logging.getlogger () returns a default logger, which is root logger, and applies the default log level, Handler and formatter settings.
Found from the output,
1) Root logger because there is no loglevel defined, so the default is warning, so the output three days record
2) Logger1 Testlog.logger definition of loglevel is debug, how to output information is worm info start? Originally logger1 and logger2 corresponding to the same logger instance, Logger2 redefined loglevel, so logger1 and logger2 corresponding instance loglevel level becomes info, careful here error
3) from the log bar count, how logger1 and Logger2 repeated output of 2 times, Logger2 repeated output three times, the original and logger structure, he is a tree structure, logger = Logging.getlogger () the creation of the root Logger, and the Logger1 logger2 we continue to create is a child instance of the root Logger instance, and Logger3 is a child of Logger1 and Logger2
logger2 = logging.getLogger("testlog") logger3 = logging.getLogger("testlog.child")
See, Logger2 corresponds to Testlog, and Logger3 corresponds to Testlog.child. In this way, a logger not only needs his own handler to handle the message, but also sends the message to the handler of his father logger. , and sub-logger if you do not define the log level, Handler, filter, and other settings, will inherit the parent logger the settings, so the parent logger priority by the properties of the child logger processing, or according to their own processing, so there are many repeated log phenomenon
Define rules for handler processing
If you define a rule
filter=logging.Filter('testlog.child') #符合testlog.child的才能记录日志到文件hterm.addFilter(filter) #将filer应用到hterm的handle
Execution Result:
Terminal output:
2016-06-09 15:42:30,501 - testlog.child - ERROR - logger3 error message2016-06-09 15:42:30,501 - testlog.child - ERROR - logger3 error message2016-06-09 15:42:30,501 - testlog.child - ERROR - logger3 error message2016-06-09 15:42:30,501 - testlog.child - CRITICAL - logger3 critical message2016-06-09 15:42:30,501 - testlog.child - CRITICAL - logger3 critical message2016-06-09 15:42:30,501 - testlog.child - CRITICAL - logger3 critical message
The log output does not change
Here is to logger add filer, also can give handler add, but the influence of wine more than a logger. He's going to affect all the logger that use this handler.
The log format used to define the output. Use the formatted content to refer to the Logging.basicconfig () function
#定义日志格式formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')#将日志格式应用到handlerhterm.setFormatter(formatter)hfile.setFormatter(formatter)
Python Log Module-logging