In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces what is the logging log module of python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
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
From: http://blog.csdn.NET/yatere/article/details/6655445
Original address: Python module Logging HOWTO official document
A brief introduction to Logging
Logging is a way to track and record events when the software is running. Software developers prompt the occurrence of certain events by calling the relevant methods of Logging in the code. Events can be described by description information, and of course the description information can also contain variables, because the description information may be different for each trigger of the event.
Second, a simple example
A simple example:
Import logginglogging.warning ('Watch outbound') # information will be printed to the console logging.info ('I told you so') # will not print any information, because the default level is higher than info
If you save the above code into the Python file, then run:
WARNING:root:Watch out!
The information for INFO does not appear on the console because the default level is WARNING higher than INFO. The log information contains log level and event description information, so don't worry too much about the root in the output, which will be described later. The actual output allows you to customize as much as you like, and the formatting will be described later.
Third, save the log information into the file
In the application, we often need to save the log information into the file. Please recreate a new file and do not attach it to the Python file above.
Import logginglogging.basicConfig (filename='example.log',level=logging.DEBUG) logging.debug ('This message should go to the log file') logging.info (' So should this') logging.warning ('And this, too')
Run him, and then generate a log file example.log, which you can open to see the specific log information:
DEBUG:root:This message should go to the log fileINFO:root:So should thisWARNING:root:And this, too
In this example, we saw how to set the log level, which is a threshold that controls the output of the log.
If you want to set the log level from the command line, you can do this:
-- log=INFO
And you can also get the parameters passed to-log, you can do this:
Getattr (logging, loglevel.upper ())
You can set the log level through the basicConfig () method-passing the level parameter. You may want to check the validity of the level parameter value, like this:
# assuming loglevel is bound to the string value obtained from the# command line argument. Convert to upper case to allow the user to# specify-- log=DEBUG or-- log=debugnumeric_level = getattr (logging, loglevel.upper (), None) if not isinstance (numeric_level, int): raise ValueError ('Invalid loglevel:% s'% loglevel) logging.basicConfig (level=numeric_level,...)
The basicConfig () method is called before debug (), info (), and so on. Because it is an one-time job: the first call is valid, and the subsequent calls are invalid empty operations.
If you run the above code multiple times, the log information will be appended to the original log information. If you want to overwrite the previous log information each time, you can configure the filemode parameter to'w':
Logging.basicConfig (filename='example.log', filemode='w', level=logging.DEBUG)
The new log information will overwrite the old one.
4. Multi-module Logging
If your project contains multiple modules, you can organize the logging as follows:
# myapp.pyimport loggingimport mylibdef main (): logging.basicConfig (filename='myapp.log', level=logging.INFO) logging.info ('Started') mylib.do_something () logging.info (' Finished') if _ _ name__ ='_ _ main__': main ()
-
# mylib.pyimport loggingdef do_something (): logging.info ('Doing something')
If you run myapp.py, you will see the following message in myqpp.log:
INFO:root:StartedINFO:root:Doing somethingINFO:root:Finished
That's what you want to see. You can organize them into your multiple modules by using mylib.py templates. Note: in this way, you can't tell where this information comes from except by looking at the event description. If you want to track the source of information, you need to refer to a more advanced tutorial-Logging Advanced
5. Logging variable information
To generate a log containing variables, you need to use a formatted string by passing the variable as a parameter to the description, for example:
Import logginglogging.warning ('% s before you% slots, 'Look', 'leapaches')
Generates:
WARNING:root:Look before you leap!
As you can see, variables are merged into the description information by using formatting characters. This approach is backward compatible: there are updated formatting options in the logging package, such as str.format () and string.Template. These new formatting methods are supported, but studying their use is beyond the scope of this article.
6. Modify the format of displaying log information
You can change the format in which log messages are displayed, like this:
Import logginglogging.basicConfig (format='% (levelname) logging.warning% (message) slots, level=logging.DEBUG) logging.debug ('This message should appear on the console') logging.info (' So should this') logging.warning ('And this, too')
Running the code prints out:
DEBUG:This message should appear on the consoleINFO:So should thisWARNING:And this, too
Have you noticed? The root that previously appeared in the log message is missing! You can generate almost everything by formatting, which you can refer to the LogRecord documentation. But for simple applications, you only need to know the log level, log information (log information containing variables), or add the occurrence time of the event. This will be covered in the next section.
Display the date and time in the log information
To display the date and time in the log message, you need to use the% (asctime) s format string. For example:
Import logginglogging.basicConfig (format='% (asctime) s% (message) s') logging.warning ('is when this event was logged.')
The run generates:
2010-12-12 11 is when this event was logged.
The default date-time display standard is ISO8601. If you want to customize your format, you can pass the parameter datefmt to the basicConfig () method as follows:
Import logginglogging.basicConfig (format='% (asctime) s% (message) slots, datefmt='%m/%d/%Y% I:%M:%S% p') logging.warning ('is when this event was logged.')
Generates:
12/12/2010 11:46:36 AM is when this event was logged.
The format of the datefmt parameter is similar to time.strftime ().
The above is the basic tutorial of Logging, if your needs are relatively simple, the usage described above should be able to meet your needs.
If you want to learn more about the Logging module, go to the Logging advanced tutorial.
The use of handlers in common use I. StreamHandler
The stream handler-- is contained in one of the three handler in the logging module.
You can output log information to sys.stdout, sys.stderr, or class file objects (more specifically, objects that support write () and flush () methods).
There is only one parameter:
Class logging.StreamHandler (stream=None)
The log information is output to the specified stream, or to sys.stderr by default if stream is empty.
II. FileHandler
One of the three handler included with the logging module. Inherited from StreamHandler. Output log information to a disk file.
Construction parameters:
Class logging.FileHandler (filename, mode='a', encoding=None, delay=False)
When append,delay is true by default, the file will not be opened until the emit method is executed. By default, log files can grow indefinitely.
III. NullHandler
Null operation one of the three handler that comes with the handler,logging module.
There are no parameters.
IV. WatchedFileHandler
Located in the logging.handlers module. Used to monitor the status of the file, and if the file is changed, close the current stream, reopen the file, and create a new stream. The file will change due to the use of newsyslog or logrotate. This handler is specially designed for Linux/unix systems, because under windows systems, the files being opened will not be changed.
The parameters are the same as FileHandler:
Class logging.handlers.WatchedFileHandler (filename, mode='a', encoding=None, delay=False) V. RotatingFileHandler
Circular log files are supported at logging.handlers.
Class logging.handlers.RotatingFileHandler (filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0)
The parameters maxBytes and backupCount allow log files to rollover when maxBytes is reached. When the file size reaches or exceeds maxBytes, a new log file is created. When either of the above parameters is 0, rollover will not occur. That is, there are no maxBytes restrictions on files. Backupcount is the number of backups, that is, the maximum number of backups you can have. Naming appends the base_name of the log with a suffix of .0-.n, such as example.log.1,example.log.1, … , example.log.10 . The log file currently in use is base_name.log.
VI. TimedRotatingFileHandler
Timed cyclic log handler, located in logging.handlers, supports scheduled generation of new log files.
Class logging.handlers.TimedRotatingFileHandler (filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)
The parameter when determines the type of time interval, and the parameter interval determines how much time interval. When='D',interval=2, for example, refers to a two-day interval, and backupCount determines how many log files can be kept. If you exceed the number, the old log files will be discarded.
The parameters of when determine the type of interval. The relationship between the two is as follows:
'S' | second' M' | minute'H' | hour'D' | days'W _ 0 _ minute _ color _ hour W6'| Monday to Sunday 'midnight' | early morning every day
The utc parameter represents the UTC time.
7. Other handler--SocketHandler, DatagramHandler, SysLogHandler, NtEventHandler, SMTPHandler, MemoryHandler, HTTPHandler
These handler are not commonly used, so please refer to the official documentation and other handlers for details.
Here is a simple example to demonstrate the use of handler:
Example 1-method of not using configuration files (StreamHandler): import logging# set up logging to file-see previous section for more detailslogging.basicConfig (level=logging.DEBUG, format='% (asctime) s% (name)-12s% (levelname)-8s% (message) slots, datefmt='%m-%d% Hpurs% Manners, filename='/temp/myapp.log' Filemode='w') # define a Handler which writes INFO messages or higher to the sys.stderr# console = logging.StreamHandler () console.setLevel (logging.INFO) # set a format which is simpler for console use# set format formatter = logging.Formatter ('% (name)-12s:% (levelname)-8s% (message) s') # tell the handler to use this format# tells handler to use this format console.setFormatter (formatter) # add the handler to the root Logger# adds handlerlogging.getLogger ('') .addHandler (console) # Now to root logger We can log to the root logger, or any other logger. First the root...# defaults to root loggerlogging.info ('Jackdaws love my big sphinx of quartz.') # Now, define a couple of other loggers which might represent areas in your# application:logger1 = logging.getLogger (' myapp.area1') logger2 = logging.getLogger ('myapp.area2') logger1.debug (' Quick zephyrs blow, vexing daft Jim.') logger1.info ('How quickly daft jumping zebras vex.') logger2.warning (' Jail zesty vixen who grabbed pay from quack.') logger2.error ('The five boxing wizards jump quickly.')
Output to the console:
Root: INFO Jackdaws love my big sphinx of quartz.myapp.area1: INFO How quickly daft jumping zebras vex.myapp.area2: WARNING Jail zesty vixen who grabbed pay from quack.myapp.area2: ERROR The five boxing wizards jump quickly. Example 2-how to use configuration files (TimedRotatingFileHandler):
Log.conf Log profile:
[loggers] keys=root,test.subtest,test [handlers] keys=consoleHandler, fileHandler [formatters] keys= simpleFormatter [logger _ root] level=INFOhandlers=consoleHandler, fileHandler [logger _ test] level=INFOhandlers=consoleHandler,fileHandlerqualname=tornadopropagate= 0 [logger _ test.subtest] level=INFOhandlers=consoleHandler,fileHandlerqualname=rocket.raccoonpropagate= 0 [handler _ consoleHandler] # output to handlerclass=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs= (sys.stdout,) [handler_fileHandler] # output to log file handlerclass=logging.handlers.TimedRotatingFileHandlerlevel=DEBUGformatter=simpleFormatterargs= ('rocket_raccoon_log' 'midnight') [formatter_simpleFormatter] format= [% (asctime) s muri% (name) s (% (levelname) s)% (filename) s asctime% (lineno) d]% (message) sdatefmt=logging.config.fileConfig (' conf/log.conf') logger = getLogging ()
Get the logger method:
Def getLogging (): return logging.getLogger ("test.subtest")
Configure logger and call:
Logging.config.fileConfig ('conf/log.conf') logger = getLogging () logger.info ("this is an example!")
Output from both the console and log files:
[2016-07-01 09:22:06470-test.subtest (INFO) main.py:55] this is an example!
This is the general introduction to the handlers of the logging module in the Python module.
Configure multiple handler for Logger in the configuration file
Use exampl
Read the configuration file:
Logging.config.fileConfig ("log.conf") # Adoption profile
Create a logger:
Logger = logging.getLogger ("simpleExample")
Log.conf file:
[loggers] # loggers list keys=root,main [handlers] # handlers list keys=consoleHandler,fileHandler [formatters] # formatters list keys= FMT [logger _ root] # root loggerlevel=DEBUGhandlers=consoleHandler,fileHandler # output root logger log information to file and console [logger_main] # main loggerlevel=DEBUGqualname=mainhandlers= fileHandler [handler _ consoleHandler] # console handlerclass=StreamHandlerlevel=DEBUGformatter=fmtargs= (sys.stdout,) [handler_fileHandler] # circular log file class=logging.handlers.RotatingFileHandlerlevel=DEBUGformatter=fmtargs= ('tst.log','a',20000,5 ) # Parameter is the parameter of _ _ init__ () of RotatingFileHandler [formatter_fmt] # format format=% (asctime) s -% (name) s -% (levelname) s -% (message) sdatefmt=
In the above file, you can output logs to multiple destinations by separating handler with commas:
Handlers=consoleHandler,fileHandler # will output the log information of root logger to the file and the console about what the logging log module of python is. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.