Python built-in Log Module logging Usage Details, pythonlogging

Source: Internet
Author: User

Python built-in Log Module logging Usage Details, pythonlogging

Introduction to the logging Module

The logging module of Python provides a common log system for third-party modules or applications. This module provides different log levels and records logs in different ways, such as files, http get/POST, SMTP, and Socket. You can even implement specific logging methods by yourself.
The logging module has the same mechanism as log4j, but the specific implementation details are different. The module provides logger, handler, filter, and formatter.

  • Logger: Provides log interfaces for use in code. Logger has two longest-used operations: Configuring and sending log messages. You can use logging. getLogger (name) to obtain the logger object. If no name is specified, the root object is returned. The same logger object is returned multiple times by calling the getLogger method with the same name.
  • Handler: sends a log record to a suitable destination, such as a file or socket. A logger object can add 0 to multiple handler using the addHandler method, and each handler can define different log levels to achieve hierarchical log filtering and display.
  • Filter: provides an elegant way to determine whether a log record is sent to handler.
  • Formatter: specify the specific format of log output. The formatter constructor requires two parameters: the message format string and the date string. Both parameters are optional.

Similar to log4j, the calling of logger, handler, and log message can have a specific log Level, only when the Level of log message is higher than that of logger and handler.

Logging Usage Analysis

1. initialize logger = logging. getLogger ("endlesscode"), it is best to add the name of the module to be logged after the getLogger () method. The % (name) s in the log format corresponds to the name of the module here.
2. Set the level logger. setLevel (logging. DEBUG). There are NOTSET <DEBUG <INFO <WARNING <ERROR <CRITICAL in Logging. logs with the level above the set level are recorded.
3. Handler. StreamHandler and FileHandler are commonly used. In windows, you can simply think of them as console and file logs, one printed on the CMD window, and the other recorded on a file.
4. formatter defines the order, structure, and content of the final log information. I like to use this format '[% (asctime) s] [% (levelname) s] % (message) s ',' % Y-% m-% d % H: % M: % s ',
% (Name) s Logger name
% (Levelname) s Log Level in text format
% (Message) s user-Output message
The current time in the string format of % (asctime) s. The default format is "16:49:45, 896 ". The comma is followed by a millisecond
% (Levelno) s Log Level in digital form
% (Pathname) s indicates the complete path name of the module that calls the log output function.
% (Filename) s name of the module that calls the log output function
% (Module) s calls the module name of the log output function
% (FuncName) s name of the function that calls the log output function
% (Lineno) d code line in which the statement that calls the log output function is located
% (Created) f current time, expressed by floating points of time in UNIX standard
% (RelativeCreated) d Number of milliseconds since Logger was created when the log information is output
% (Thread) d thread ID. Not possible
% (ThreadName) s thread name. Not possible
% (Process) d process ID. Not possible
5. Use object. debug (message) to record logs
Next, write an instance. In the CMD window, only logs of the error level and above are displayed, but the debug level and above information are displayed in the log.

Import logginglogger = logging. getLogger ("simple_example") logger. setLevel (logging. DEBUG) # create a filehandler to record the log in the file. The level is debug or above. fh = logging. fileHandler ("spam. log ") fh. setLevel (logging. DEBUG) # create a streamhandler to load the log in the CMD window. The level is error or above. ch = logging. streamHandler () ch. setLevel (logging. ERROR) # Set the log format formatter = logging. formatter ("% (asctime) s-% (name) s-% (levelname) s-% (message) s") ch. setFormatter (formatter) fh. setFormatter (formatter) # Add the corresponding handler to the logger object. addHandler (ch) logger. addHandler (fh) # start logging logger. debug ("debug message") logger.info ("info message") logger. warn ("warn message") logger. error ("error message") logger. critical ("critical message ")

After running the command, only two logs are recorded in the CMD window, and five logs are recorded in spam. log.

When a project is large, logs are used in different files. You can encapsulate the Log into a class for use.

#! /Usr/bin/env python # coding = gbkimport logging, osclass Logger: def _ init _ (self, path, clevel = logging. DEBUG, Flevel = logging. DEBUG): self. logger = logging. getLogger (path) self. logger. setLevel (logging. DEBUG) fmt = logging. formatter ('[% (asctime) s] [% (levelname) s] % (message) s',' % Y-% m-% d % H: % M: % s') # Set CMD log sh = logging. streamHandler () sh. setFormatter (fmt) sh. setLevel (clevel) # Set file log fh = logging. fileHandler (path) fh. setFormatter (fmt) fh. setLevel (Flevel) self. logger. addHandler (sh) self. logger. addHandler (fh) def debug (self, message): self. logger. debug (message) def info (self, message): self.logger.info (message) def war (self, message): self. logger. warn (message) def error (self, message): self. logger. error (message) def cri (self, message): self. logger. critical (message) if _ name _ = '_ main _': logyyx = Logger ('yyx. log', logging. ERROR, logging. DEBUG) logyyx. debug ('one debug info') logyyx.info ('one info') logyyx. war ('a warning information') logyyx. error ('one error information') logyyx. cri ('one fatal critical information ')

In this way, you only need to instantiate an object every time you use it.

logobj = Logger(‘filename',clevel,Flevel)

If you want to red the error log in the CMD window and yellow the warning log, you can use the ctypes module.

#! /Usr/bin/env python # coding = gbkimport logging, osimport ctypesFOREGROUND_WHITE = 0x0007FOREGROUND_BLUE = 0x01 # text color contains blue. FOREGROUND_GREEN = 0x02 # text color contains green. FOREGROUND_RED = 0x04 # text color contains red. FOREGROUND_YELLOW = FOREGROUND_RED | FOREGROUND_GREENSTD_OUTPUT_HANDLE =-11std_out_handle = ctypes. windll. kernel32.GetStdHandle (STD_OUTPUT_HANDLE) def set_color (color, handle = std_out_handle): bool = ctypes. windll. kernel32.SetConsoleTextAttribute (handle, color) return boolclass Logger: def _ init _ (self, path, clevel = logging. DEBUG, Flevel = logging. DEBUG): self. logger = logging. getLogger (path) self. logger. setLevel (logging. DEBUG) fmt = logging. formatter ('[% (asctime) s] [% (levelname) s] % (message) s',' % Y-% m-% d % H: % M: % s') # Set CMD log sh = logging. streamHandler () sh. setFormatter (fmt) sh. setLevel (clevel) # Set file log fh = logging. fileHandler (path) fh. setFormatter (fmt) fh. setLevel (Flevel) self. logger. addHandler (sh) self. logger. addHandler (fh) def debug (self, message): self. logger. debug (message) def info (self, message): self.logger.info (message) def war (self, message, color = FOREGROUND_YELLOW): set_color (color) self. logger. warn (message) set_color (FOREGROUND_WHITE) def error (self, message, color = FOREGROUND_RED): set_color (color) self. logger. error (message) set_color (FOREGROUND_WHITE) def cri (self, message): self. logger. critical (message) if _ name _ = '_ main _': logyyx = Logger ('yyx. log', logging. WARNING, logging. DEBUG) logyyx. debug ('one debug info') logyyx.info ('one info') logyyx. war ('a warning information') logyyx. error ('one error information') logyyx. cri ('one fatal critical information ')

Use logging for multiple modules
The logging module ensures that logging. getLogger ('Log _ name') is called multiple times in the same python interpreter and returns the same logger instance, even in the case of multiple modules. Therefore, in typical multi-module scenarios, logging is configured in the main module. This configuration applies to multiple sub-modules, then, you can get the Logger object directly through getLogger in other modules.
Configuration file:

[loggers] keys=root,main  [handlers] keys=consoleHandler,fileHandler  [formatters] keys=fmt  [logger_root] level=DEBUG handlers=consoleHandler  [logger_main] level=DEBUG qualname=main handlers=fileHandler  [handler_consoleHandler] class=StreamHandler level=DEBUG formatter=fmt args=(sys.stdout,)  [handler_fileHandler] class=logging.handlers.RotatingFileHandler level=DEBUG formatter=fmt args=('tst.log','a',20000,5,)  [formatter_fmt] format=%(asctime)s - %(name)s - %(levelname)s - %(message)s datefmt= 

Main module main. py:

import logging import logging.config  logging.config.fileConfig('logging.conf') root_logger = logging.getLogger('root') root_logger.debug('test root logger...')  logger = logging.getLogger('main') logger.info('test main logger') logger.info('start import module \'mod\'...') import mod  logger.debug('let\'s test mod.testLogger()') mod.testLogger()  root_logger.info('finish test...') 

Sub-module mod. py:

import logging import submod  logger = logging.getLogger('main.mod') logger.info('logger of mod say something...')  def testLogger():   logger.debug('this is mod.testLogger...')   submod.tst() 

Submodule submod. py:

import logging  logger = logging.getLogger('main.mod.submod') logger.info('logger of submod say something...')  def tst():   logger.info('this is submod.tst()...') 

Then run python main. py and the console outputs:

2012-03-09 18:22:22,793 - root - DEBUG - test root logger... 2012-03-09 18:22:22,793 - main - INFO - test main logger 2012-03-09 18:22:22,809 - main - INFO - start import module 'mod'... 2012-03-09 18:22:22,809 - main.mod.submod - INFO - logger of submod say something... 2012-03-09 18:22:22,809 - main.mod - INFO - logger say something... 2012-03-09 18:22:22,809 - main - DEBUG - let's test mod.testLogger() 2012-03-09 18:22:22,825 - main.mod - DEBUG - this is mod.testLogger... 2012-03-09 18:22:22,825 - main.mod.submod - INFO - this is submod.tst()... 2012-03-09 18:22:22,841 - root - INFO - finish test... 

It can be seen that, as expected, the output destination in tst. log and logger configuration is as follows:

2012-03-09 18:22:22,793 - main - INFO - test main logger 2012-03-09 18:22:22,809 - main - INFO - start import module 'mod'... 2012-03-09 18:22:22,809 - main.mod.submod - INFO - logger of submod say something... 2012-03-09 18:22:22,809 - main.mod - INFO - logger say something... 2012-03-09 18:22:22,809 - main - DEBUG - let's test mod.testLogger() 2012-03-09 18:22:22,825 - main.mod - DEBUG - this is mod.testLogger... 2012-03-09 18:22:22,825 - main.mod.submod - INFO - this is submod.tst()... 

The tst. log does not contain the output information of the root logger, because only the main logger and its sub-logger are configured in logging. conf to use RotatingFileHandler, while the root logger is output to the standard output.

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.