In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to learn Pathon log ogging module, the editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
Log logging module learning of python
Directory (?) [+]
Simply print the log to the screen
Configure the output format and mode of the log through the loggingbasicConfig function
Output the log to both the file and the screen
Logging log rollback
Configure logs through the loggingconfig module
Logging is thread safe
1. Simply print the log to the screen
Import logging
Logging.debug ('This
Is debug message')
Logging.info ('This
Is info message')
Logging.warning ('This
Is warning message')
Print on the screen:
WARNING:root:This is warning message
By default, logging prints the log to the screen, and the log level is WARNING
The log level size relationship is CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET. Of course, you can also define the log level yourself.
two。 Configure the output format and mode of the log through the logging.basicConfig function
Import logging
Logging.basicConfig (level=logging.DEBUG
Format='% (asctime) s
% (filename) s [line:% (lineno) d]% (levelname) s% (message) s'
Datefmt='%a
% d% b% Y% HRV% MVA% S'
Filename='myapp.log'
Filemode='w')
Logging.debug ('This
Is debug message')
Logging.info ('This
Is info message')
Logging.warning ('This
Is warning message')
The contents of the. / myapp.log file are:
Sun, 24 May 2009 21:48:54 demo2.py [line:11] DEBUG This is debug message
Sun, 24 May 2009 21:48:54 demo2.py [line:12] INFO This is info message
Sun, 24 May 2009 21:48:54 demo2.py [line:13] WARNING This is warning message
The parameters of the logging.basicConfig function:
Filename: specify the log file name
Filemode: has the same meaning as the file function, specifying the opening mode of the log file,'w' or'a'
Format: specify the format and content of the output. Format can output a lot of useful information, as shown in the example above:
% (levelno) s: print log-level numeric values
% (levelname) s: print log level name
% (pathname) s: print the path of the currently executing program, which is actually sys.argv [0]
% (filename) s: print the name of the currently executing program
% (funcName) s: current function for printing logs
% (lineno) d: print the current line number of the log
% (asctime) s: time the log was printed
% (thread) d: print thread ID
% (threadName) s: print thread name
% (process) d: print process ID
% (message) s: print log information
Datefmt: specify the time format, same as time.strftime ()
Level: sets the log level. Default is logging.WARNING.
Stream: specify the output stream of the log. You can specify the output to sys.stderr,sys.stdout or file. Default output to sys.stderr. When both stream and filename are specified, stream is ignored.
3. Output the log to both the file and the screen
Import logging
Logging.basicConfig (level=logging.DEBUG
Format='% (asctime) s
% (filename) s [line:% (lineno) d]% (levelname) s% (message) s'
Datefmt='%a
% d% b% Y% HRV% MVA% S'
Filename='myapp.log'
Filemode='w')
#
# define a StreamHandler, print log information at INFO level or higher to standard error, and add it to the current log processing object #
Console = logging.StreamHandler ()
Console.setLevel (logging.INFO)
Formatter = logging.Formatter ('% (name)-12s:
% (levelname)-8s% (message) s')
Console.setFormatter (formatter)
Logging.getLogger ('') .addHandler (console)
#
Logging.debug ('This
Is debug message')
Logging.info ('This
Is info message')
Logging.warning ('This
Is warning message')
Print on the screen:
Root: INFO This is info message
Root: WARNING This is warning message
The contents of the. / myapp.log file are:
Sun, 24 May 2009 21:48:54 demo2.py [line:11] DEBUG This is debug message
Sun, 24 May 2009 21:48:54 demo2.py [line:12] INFO This is info message
Sun, 24 May 2009 21:48:54 demo2.py [line:13] WARNING This is warning message
4.logging log rollback
Import logging
From logging.handlers import RotatingFileHandler
#
# define a RotatingFileHandler to back up up to 5 log files, each with a maximum of 10m
Rthandler = RotatingFileHandler ('myapp.log', maxBytes=10*1024*1024,backupCount=5)
Rthandler.setLevel (logging.INFO)
Formatter = logging.Formatter ('% (name)-12s:
% (levelname)-8s% (message) s')
Rthandler.setFormatter (formatter)
Logging.getLogger ('') .addHandler (Rthandler)
#
As you can see from the above example and this example, logging has a master object for log processing, and other processing methods are added through addHandler.
Several handle methods of logging are as follows:
Logging.StreamHandler: log output to a stream, which can be sys.stderr, sys.stdout, or file
Logging.FileHandler: log output to file
Log rollback method, using RotatingFileHandler and TimedRotatingFileHandler in actual use
Logging.handlers.BaseRotatingHandler
Logging.handlers.RotatingFileHandler
Logging.handlers.TimedRotatingFileHandler
Logging.handlers.SocketHandler: remote output log to TCP/IP sockets
Logging.handlers.DatagramHandler: remote output log to UDP sockets
Logging.handlers.SMTPHandler: remote output log to email address
Logging.handlers.SysLogHandler: log output to syslog
Logging.handlers.NTEventLogHandler: remote output log to Windows NT/2000/XP event log
Logging.handlers.MemoryHandler: defined buffer for log output to memory
Logging.handlers.HTTPHandler: remote output to the HTTP server via "GET" or "POST"
Because StreamHandler and FileHandler are commonly used log processing methods, they are directly included in the logging module, while other methods are included in the logging.handlers module.
For the use of the other processing methods mentioned above, see the python2.5 manual!
5. Configure logs through the logging.config module
# logger.conf
# # #
[loggers]
Keys=root,example01,example02
[logger_root]
Level=DEBUG
Handlers=hand01,hand02
[logger_example01]
Handlers=hand01,hand02
Qualname=example01
Propagate=0
[logger_example02]
Handlers=hand01,hand03
Qualname=example02
Propagate=0
# # #
[handlers]
Keys=hand01,hand02,hand03
[handler_hand01]
Class=StreamHandler
Level=INFO
Formatter=form02
Args= (sys.stderr,)
[handler_hand02]
Class=FileHandler
Level=DEBUG
Formatter=form01
Args= ('myapp.log', 'a')
[handler_hand03]
Class=handlers.RotatingFileHandler
Level=INFO
Formatter=form02
Args= ('myapp.log',' asides, 10 '1024' 1024, 5)
# # #
[formatters]
Keys=form01,form02
[formatter_form01]
Format=% (asctime) s% (filename) s [line:% (lineno) d]% (levelname) s% (message) s
Datefmt=%a, d b Y H:%M:%S
[formatter_form02]
Format=% (name)-12s:% (levelname)-8s% (message) s
Datefmt=
Example 3 above:
Import logging
Import logging.config
Logging.config.fileConfig ("logger.conf")
Logger = logging.getLogger ("example01")
Logger.debug ('This
Is debug message')
Logger.info ('This
Is info message')
Logger.warning ('This
Is warning message')
Example 4 above:
Import logging
Import logging.config
Logging.config.fileConfig ("logger.conf")
Logger = logging.getLogger ("example02")
Logger.debug ('This
Is debug message')
Logger.info ('This
Is info message')
Logger.warning ('This
Is warning message')
6.logging is thread-safe, which is how to learn the Pathon log ogging module. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.