Python Log Module Logging introduction

Source: Internet
Author: User
Logging is divided into 4 modules: loggers, handlers, filters, and formatters.

Loggers: An interface that provides application invocation
Handlers: Send the log to the specified location
Filters: Filtering Log information
Formatters: Formatted output log

Logger

Logger.setlevel () Setting the log level
Logger.addhandler () and Logger.removehandler () Add and remove log processors
Logger.addfilter () and Logger.removefilter () Add and remove filters
Logger.debug (), Logger.info (), logger.warning (), Logger.error (), and logger.critical () create different levels of logging
GetLogger () Gets the root instance of the log

Handler

SetLevel () Setting the log level
Setformatter () Setting the output format
AddFilter () and removefilter () Add and remove filters

Formatter

The default form is:%y-%m-%d%h:%m:%s.
Format is:% () s

Log Configuration Management

Hard-coded form
Copy the 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 CodeThe code is 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


Configuring management logs with files

Code:
Copy the Code code as follows:


Import logging
Import Logging.config

Logging.config.fileConfig (' logging.conf ')

# Create Logger
Logger = Logging.getlogger (' simpleexample ')

# ' Application ' code
Logger.debug (' Debug message ')
Logger.info (' info message ')
Logger.warn (' Warn message ')
Logger.error (' Error message ')
Logger.critical (' critical message ')


configuration file:
Copy CodeThe code is as follows:


[Loggers]
Keys=root,simpleexample

[Handlers]
Keys=consolehandler

[Formatters]
Keys=simpleformatter

[Logger_root]
Level=debug
Handlers=consolehandler

[Logger_simpleexample]
Level=debug
Handlers=consolehandler
Qualname=simpleexample
Propagate=0

[Handler_consolehandler]
Class=streamhandler
Level=debug
Formatter=simpleformatter
Args= (Sys.stdout,)

[Formatter_simpleformatter]
format=% (asctime) s-% (name) s-% (levelname) s-% (message) s
datefmt=


Output:
Copy CodeThe code is as follows:


$ python simple_logging_config.py
2005-03-19 15:38:55,977-simpleexample-debug-debug Message
2005-03-19 15:38:55,979-simpleexample-info-info Message
2005-03-19 15:38:56,054-simpleexample-warning-warn Message
2005-03-19 15:38:56,055-simpleexample-error-error Message
2005-03-19 15:38:56,130-simpleexample-critical-critical Message


Log Format

% (Levelno) S: Print the value of the log level
% (levelname) S: Print log level name
% (pathname) s: Prints the path of the currently executing program, which is actually sys.argv[0]
% (filename) s: Prints the current name of the executing program
% (funcName) s: Print the current function of the log
% (Lineno) d: Print the current line number of the log
% (asctime) s: Time to print logs
% (thread) d: Print thread ID
% (threadname) s: Print thread name
% (process) d: Print process ID
% (message) s: Print log information

Flow chart

  • 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.