Introduction to Python logging module logging

Source: Internet
Author: User
This article mainly introduces the Python logging module logging. This article describes Logger, Handler, Formatter, log configuration management, and file configuration management logs, you can refer to the following four logging modules: loggers, handlers, filters, and formatters.

● Loggers: interfaces called By Applications
● Handlers: Sends logs to a specified location.
● Filters: Filter log information
● Formatters: format the output log

Logger

Logger. setLevel () sets the log level
Logger. addHandler () and Logger. removeHandler () add and delete log processors
Logger. addFilter () and Logger. removeFilter () add and delete filters
Logger. debug (), Logger.info (), Logger. warning (), Logger. error (), and Logger. critical () create different levels of logs
GetLogger (): get the root instance of the log

Handler

SetLevel () set the log level
SetFormatter () sets the output format
AddFilter () and removeFilter () add and delete filters

Formatter

The default format is % Y-% m-% d % H: % M: % S.
Format: % () s

Log configuration management

Hard encoding format

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


Log management through file configuration

Code:

The code is 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:

The 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:

The code is as follows:


$ Python simple_logging_config.py
15:38:55, 977-simpleExample-DEBUG-debug message
2005-03-19 15:38:55, 979-simpleExample-INFO-info message
15:38:56, 054-simpleExample-WARNING-warn message
15:38:56, 055-simpleExample-ERROR-error message
15:38:56, 130-simpleExample-CRITICAL-critical message


Log Format

% (Levelno) s: print the log-level value
% (Levelname) s: print the log level name
% (Pathname) s: print the path of the current execution program, which is actually sys. argv [0]
% (Filename) s: print the name of the currently executed program
% (FuncName) s: current function for printing logs
% (Lineno) d: print the current line number of the log
% (Asctime) s: log printing time
% (Thread) d: print thread ID
% (ThreadName) s: print the thread name
% (Process) d: print the process ID
% (Message) s: print log information

Flowchart

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.