Python Logging Module Learning notes _python

Source: Internet
Author: User
Tags current time

Module-level functions

Logging.getlogger ([name]): Returns a Logger object that returns root if no name is specified logger
Logging.debug (), Logging.info (), logging.warning (), Logging.error (), logging.critical (): Setting the log level of root logger
Logging.basicconfig (): Create a Streamhandler for the log system with the default formatter, set the base configuration and add it to root logger

Example: logging_level_example.py

Copy Code code 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 are a debug message ')
Logging.info (' This was an info ')
Logging.warning (' This are a warning message ')
Logging.error (' This is a error message ')
Logging.critical (' This are a critical error message ')

Output:
Copy Code code as follows:

$ python logging_level_example.py Debug
DEBUG:root:This is a debug message
INFO:root:This is a info message
WARNING:root:This is a WARNING message
ERROR:root:This is a error message
CRITICAL:root:This is a CRITICAL error message

$ python logging_level_example.py Info
INFO:root:This is a info message
WARNING:root:This is a WARNING message
ERROR:root:This is a error message
CRITICAL:root:This is a CRITICAL error message

Loggers

Logger.setlevel (LEL): Specifies the lowest log level, and the level below LEL is ignored. Debug is the lowest built-in level, critical is the highest
Logger.addfilter (filt), Logger.removefilter (filt): Add or remove the specified filter
Logger.addhandler (HDLR), Logger.removehandler (HDLR): Add or remove the specified handler
Logger.debug (), Logger.info (), logger.warning (), Logger.error (), logger.critical (): Log level that can be set

Example: simple_logging_module.py

Copy Code code 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:
Copy Code code as follows:

$ python simple_logging_module.py
2005-03-19 15:10:26,618-simple_example-debug-debug Message
2005-03-19 15:10:26,620-simple_example-info-info Message
2005-03-19 15:10:26,695-simple_example-warning-warn Message
2005-03-19 15:10:26,697-simple_example-error-error Message
2005-03-19 15:10:26,773-simple_example-critical-critical Message

Handlers

The handler object is responsible for sending relevant information to the specified destination. Multiple handler can be added through the AddHandler () method
Handler.setlevel (LEL): Specifies the level of information to be processed, information below LEL level will be ignored
Handler.setformatter (): Select a format for this Handler
Handler.addfilter (filt), Handler.removefilter (filt): Add or remove a filter object

Formatters

The formatter object sets the final rules, structure, and content of the log information, the default time format is%y-%m-%d%h:%m:%s, and the following are some of the commonly used information formatter

% (name) s

Logger's name.

% (Levelno) s

Log level in digital form

% (LevelName) s

Log level in text form

% (pathname) s

The full pathname of the module that calls the log output function may not have

% (filename) s

The filename of the module that called the log output function

% (module) s

Call the module name of the log output function

% (FuncName) s

Call the function name of the log output function

% (Lineno) d

The line of code that contains the statement that calls the log output function

% (created) f

Current time, expressed as a floating-point number of the UNIX standard representation time

% (relativecreated) d

The number of milliseconds since the logger was created when logging information is output

% (Asctime) s

The current time in the form of a string. The default format is "2003-07-08 16:49:45,896". The comma is followed by a millisecond

% (thread) d

The thread ID. May not have

% (ThreadName) s

The name of the thread. May not have

% (process) d

The process ID. May not have

% (message) s

Messages from user output


Finally, a complete example:

Copy Code code as follows:

Import logging

# Set up logging to File-see previous
Logging.basicconfig (level=logging. DEBUG,
format= '% (asctime) s% (name) -12s% (levelname) -8s% (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 to console use
Formatter = logging. Formatter ('% (name) -12s:% (levelname) -8s% (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 no other logger. The root of the ...
Logging.info (' Jackdaws Love me big sphinx of quartz. ')

# Now, define a couple of the 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. ')

After running the results seen in the terminal

Copy Code code as follows:

Root:info jackdaws Love me 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

Copy Code code as follows:

10-22 22:19 root INFO jackdaws love I big sphinx of quartz.
10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.
10-22 22:19 myapp.area1 INFO How quickly daft jumping the zebras.
10-22 22:19 myapp.area2 WARNING jail Zesty Vixen The who grabbed pay from quack.
10-22 22:19 myapp.area2 ERROR The five boxing wizards the jump.

The debug information found only appears in the file because the Setlevel in Streamhandler is info and can see the difference between Logger.setlevel () and Handler.setlevel ()

For more information, see http://docs.python.org/library/logging.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.