This article mainly introduces the Pythonlogging module, which is a new feature introduced in 2.3 to process log management during program running, this article explains in detail some common classes and module-level functions of this module. If you need them, refer
Module-level functions
Logging. getLogger ([name]): returns a logger object. If no name is specified, the root logger is returned.
Logging. debug (), logging.info (), logging. warning (), logging. error (), and logging. critical (): Set the Log Level of the root logger.
Logging. basicConfig (): use the default Formatter to create a StreamHandler for the log system, set the basic configuration, and add it to the root logger.
Example: logging_level_example.py
The Code is as follows:
Import logging
Import sys
LEVELS = {'debug': logging. debug,
'Info': logging. info,
'Warning': logging. warning,
'Error': logging. error,
'Critical ': logging. critical}
If len (sys. argv)> 1:
Level_name = sys. argv [1]
Level = LEVELS. get (level_name, logging. NOTSET)
Logging. basicConfig (level = level)
Logging. debug ('this is a debug message ')
Logging.info ('this is an info message ')
Logging. warning ('this is a warning message ')
Logging. error ('this is an error message ')
Logging. critical ('this is a critical error message ')
Output:
The Code is as follows:
$ Python logging_level_example.py debug
DEBUG: root: This is a debug message
INFO: root: This is an info message
WARNING: root: This is a warning message
ERROR: root: This is an error message
CRITICAL: root: This is a critical error message
$ Python logging_level_example.py info
INFO: root: This is an info message
WARNING: root: This is a warning message
ERROR: root: This is an error message
CRITICAL: root: This is a critical error message
Loggers
Logger. setLevel (lel): Specify the lowest log level. If it is lower than lel, it will be ignored. Debug is the lowest built-in level, and critical is the highest
Logger. addFilter (filt) and Logger. removeFilter (filt): add or delete a specified filter.
Logger. addHandler (hdlr) and Logger. removeHandler (hdlr): add or delete a specified handler.
Logger. debug (), Logger.info (), Logger. warning (), Logger. error (), Logger. critical (): the log level that can be set
Example: simple_logging_module.py
The Code is as follows:
Import logging
# Create logger
Logger = logging. getLogger ("simple_example ")
Logger. setLevel (logging. DEBUG)
# Create console handler and set level to debug
Ch = logging. StreamHandler ()
Ch. setLevel (logging. DEBUG)
# Create formatter
Formatter = logging. Formatter ("% (asctime) s-% (name) s-% (levelname) s-% (message) s ")
# Add formatter to ch
Ch. setFormatter (formatter)
# Add ch to logger
Logger. addHandler (ch)
# "Application" code
Logger. debug ("debug message ")
Logger.info ("info message ")
Logger. warn ("warn message ")
Logger. error ("error message ")
Logger. critical ("critical message ")
Output:
The Code is as follows:
$ Python simple_logging_module.py
15:10:26, 618-simple_example-DEBUG-debug message
15:10:26, 620-simple_example-INFO-info message
15:10:26, 695-simple_example-WARNING-warn message
15:10:26, 697-simple_example-ERROR-error message
15:10:26, 773-simple_example-CRITICAL-critical message
Handlers
The handler object is responsible for sending relevant information to the specified destination. You can add multiple handler using the addHandler () method.
Handler. setLevel (lel): Specifies the level of information to be processed. Information lower than the lel level will be ignored.
Handler. setFormatter (): select a format for the handler.
Handler. addFilter (filt), Handler. removeFilter (filt): Adds or deletes a filter object.
Formatters
The Formatter object sets the last rule, structure, and content of log information. The default time format is % Y-% m-% d % H: % M: % S, below are some common information about Formatter.
% (Name) s |
Logger name |
% (Levelno) s |
Log Level in digital form |
% (Levelname) s |
Log Level in text format |
% (Pathname) s |
The complete path name of the module that calls the log output function. |
% (Filename) s |
File Name of the module that calls the log output function |
% (Module) s |
The name of the module that calls the log output function. |
% (FuncName) s |
Name of the function that calls the log output function |
% (Lineno) d |
The code line of the statement that calls the log output function |
% (Created) f |
Current Time, represented by floating points of time in UNIX standard |
% (RelativeCreated) d |
Number of milliseconds since Logger was created when log information is output |
% (Asctime) s |
The current time in string format. The default format is "16:49:45, 896 ". The comma is followed by a millisecond |
% (Thread) d |
Thread ID. Not possible |
% (ThreadName) s |
Thread name. Not possible |
% (Process) d |
Process ID. Not possible |
% (Message) s |
User Output Message |
The following is a complete example:
The Code is as follows:
Import logging
# Set up logging to file-see previous section for more details
Logging. basicConfig (level = logging. DEBUG,
Format = '% (asctime) s % (name)-12 s % (levelname)-8 s % (message) s ',
Datefmt = '% m-% d % H: % m ',
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
Formatter = logging. Formatter ('% (name)-12 s: % (levelname)-8 s % (message) s ')
# Tell the handler to use this format
Console. setFormatter (formatter)
# Add the handler to the root logger
Logging. getLogger (''). addHandler (console)
# Now, we can log to the root logger, or any other logger. First the root...
Logging.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 .')
Result displayed on the terminal after running
The Code is as follows:
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.
Results In the log file
The Code is as follows:
10-22 root INFO Jackdaws love my big sphinx of quartz.
10-22 myapp. area1 DEBUG Quick zephyrs blow, vexing daft Jim.
10-22 myapp. area1 INFO How quickly daft jumping zebras vex.
10-22 myapp. area2 WARNING Jail zesty vixen who grabbed pay from quack.
10-22 myapp. area2 ERROR The five boxing wizards jump quickly.
It is found that the DEBUG information only appears in the file, because the setLevel in StreamHandler is INFO. We can see the difference between Logger. setLevel () and handler. setLevel ().
For more information, see http://docs.python.org/library/logging.html