Basic Primer _python-modules and packages. What are the best practices for logging module logging in operational development?

Source: Internet
Author: User
Tags system log

Brief introduction:

Description: This module provides files, HTTP Get/post,smtp,socket and other ways to achieve logging, and even automatic implementation of specific logging methods


Quick installation:

Pip Install--upgrade logging


Processing Flow:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/88/13/wKioL1fpGgWT6WX0AADB8mVHrYk784.png "title=" Logging_flow.png "alt=" Wkiol1fpggwt6wx0aadb8mvhryk784.png "/>


Log level:

Property name Property Description
Logging. NOTSET Default is 0
Logging. DEBUG Debug to 10
Logging.info typically 20
Logging. WARN Warning is 30
Logging. ERROR Error is 40
Logging. CRITICAL Severity is 50
Logging._levelnames Log Level Dictionary


Quick configuration:

Logging.basicconfig (**kwargs), None

Description: Quickly configure root Logger object, support Filename,filemode,format,datefmt,level,stream, because it is set for root logger object, So you must leave the Name field of Logging.getlogger (') blank when you get the log object

Logging.config.fileConfig (fname, Defaults=none, disable_existing_loggers=true), None

;  define multiple Log objects keys=root, error[handlers];  define multiple processing objects keys=root,  error[formatters];  define multiple Format Objects keys=root, error[logger_root]level=notset;  processing class, can have more than one, The comma separates the Handlers=root; logger name, and the default rootqualname=root;  does not inherit the parent class's log information if not set propagate=0[logger_error]level= info;  ensure that error messages are also printed in the Terminal handlers=error, rootqualname=errorpropagate=0[handler_root];  processing class, can have multiple, Comma-separated class=streamhandler;  parameter elements, can have multiple commas separated by args= (sys.stdout,);  log level, NotSet accept any level of log;  Sets the format of the processing object formatter=root[handler_error]class=logging.handlers.timedrotatingfilehandlerargs= ('./logs/error.log ',   ' d ',  1, 7) formatter=error[formatter_root];  Format Object Format format=% (asctime) s - % ( LevelName) s - % (message) s;  set the date format datefmt=[formatter_error]format=% (asctime) s - % ( LevelName) s - % (filename) s - % (Lineno) s - % (message) sdatefmt= 

Description: The default resolution is resolved in INI format, the configuration file must contain [logger],[handlers],[formatters] three nodes, representing the log objects, processing objects and format objects


Log object:

Creating objects

Description: A Log object can add multiple processing objects

Logging.getlogger (Name=none), logger

Description: Quickly create a log object that returns the root logger object if no name is specified, and name supports. Connect Filter objects using

Object methods

L.addhandler (handler), None

Description: Adding a Processing object to a Log object

L.debug (msg, *args, **kwargs, exc_info=true), None

Description: Using debug mode to write the log, *args,**kwargs will automatically replace the MSG in the replacement string, Exc_info is true when the exception information is logged

L.info (msg, *args, **kwargs, exc_info=true), None

Description: Using the default mode to write the log, *args,**kwargs will automatically replace the MSG in the replacement string, Exc_info is true when the exception information is logged

L.warning (msg, *args, **kwargs, exc_info=true), None

Description: Using the warning mode to write the log, *args,**kwargs will automatically replace the MSG in the replacement string, Exc_info is true when the exception information is logged

L.error (msg, *args, **kwargs, exc_info=true), None

Description: Using error mode to write log, *args,**kwargs will automatically replace the MSG in the replacement string, Exc_info is true when the exception information is logged

L.critical (msg, *args, **kwargs, exc_info=true), None

Description: Using critical mode to write the log, *args,**kwargs automatically replaces the replacement string in MSG, and the exception information is logged when Exc_info is true


Working with objects:

Note: The logging module provides only the underlying stream processing class (Streamhandler) and the File processing class (Filehandler) by default, and if you want to use the extended processing class, import Logging.handlers Import the extension processing class before it can be used.

Creating objects

Note: The Log object can add multiple processing objects, each processing object can add more than one filter object, so that the log will pass through the filter

Logging. Streamhandler (Stream=none), Streamhandler

Description: Create a Stream processing object, the parameter stream can be any file object, default is Sys.stderr

Logging. Filehandler (filename, mode= ' A ', Encoding=none, delay=0), Filehandler

Note: Above, the comparison will automatically help you open the file, create a file processing object

Logging. Rotatingfilehandler (filename, mode= ' A ', maxbytes=0, backupcount=0, Encoding=none, delay=0), Rotatingfilehandler

Note: Ditto but can be polled when the specified size of the log file is renamed (appended to the end of the number), creating a new file to continue the output, MaxBytes to 0 means infinity, Backupcount indicates the number of backup files

Logging.handlers.TimedRotatingFileHandler (filename, when= ' h ', interval=1, backupcount=0, Encoding=none, Delay=false , Utc=false)-Timedrotatingfilehandler

Note: Ditto but can be polled when the log file is renamed beyond the specified time (additional time at the end), create a new file to continue output, when can be s seconds/m minutes///days/w weeks, interval represents frequency

Logging.handlers.SocketHandler (host, port), Sockethandler

Description: Creates a TCP processing object that sends logs to the port of the corresponding host

Logging.handlers.DatagramHandler (host, port), Datagramhandler

Description: Creates a UDP processing object that sends logs to the port of the corresponding host

Logging.handlers.SysLogHandler (address= (' localhost ', 514), Facility=1, Socktype=none), Sysloghandler

Description: Creates a local processing object that writes logs to the Linux system log via SYSLOGD

Logging.handlers.NTEventLogHandler (appname, Dllname=none, logtype= ' application '), Nteventloghandler

Description: Creates a local processing object that writes logs through Evetlog to the Windows event log

Logging.handlers.SMTPHandler (Mailhost, fromaddr, Toaddrs, subject, Credentials=none, Secure=none)

Description: Create a message Processing object to send the log to the corresponding mailbox by mail

Logging.handlers.MemoryHandler (capacity, flushlevel=40, Target=none), Memoryhandler

Description: Creates a memory processing object that stores the log zero to memory periodically flushed to the target

Logging.handlers.HTTPHandler (host, url, method= ' POST '), HttpHandler

Description: Creates an HTTP processing object that sends logs through the HTTP interface to the corresponding server store

Object methods

H.setlevel (level), None

Description: Sets the log level for log processing objects and, once set, is processed only if the log exceeds this level

H.setformatter (FMT), None

Description: Format object for the log processing object, the specific format can refer to the Format object related information


Format object:

Logging. Formatter (Fmt=none, Datefmt=none), Formatter

Description: Create a Format object, FMT for log Information format, DATAFMT for time format, default to "%y-%m-%d%h:%m:%s"

Special fields

Field name Field description
% (name) s Name of the Logger
% (Levelno) s Log level in digital form
% (LevelName) s Log level in text form
% (pathname) s The full pathname of the call log output function, possibly empty
% (filename) s The full file name of the call log output function may be empty
% (module) s Call the module name of the log output function
% (FuncName) s Function name of the call log output function
% (Lineno) d The line of code where the log output is called
% (created) f Current time, default timestamp format
% (relativecreated) d Number of milliseconds since logger was created
% (Asctime) s Current time in string form, default "year-month-day hour: minutes: seconds, milliseconds"
% (thread) d Thread ID, may not have
% (ThreadName) s Thread name, may not have
% (process) d Process ID, may not have
% (message) s User-Output messages


Filter objects:

Description: Each processing object can add multiple filter objects, so long as a filter object is rejected, the log information will not be processed

Logging. Filter (name= "")

Description: Create a filter, the Log Object name format is generally A.B.C, when the name of a.b means that only the prefix A.B log will be processed, otherwise the log will be discarded.


Application Scenarios:

1. Rotatingfilehandler and Timedrotatingfilehandler are thread-safe and can be used in multithreaded scenarios

2. Multi-process scenarios The official recommendation is to use the TCP/UDP transfer log, a single pressure test to support the 6000~9000/s record, and also consider using the log handle provided by Python-logstash or PYZMQ

#!/usr/bin/env python#-*-coding:utf-8-*-"" "# # authors:limanman# 51ctobg:http://xmdevops.blog.51cto.com/# Purpose:# "" "# Description: Import public Module Imports Loggingimport logging.config# Description: Import other modules if __name__ = = ' __main__ ': logging.config.fileConfig (' Log Ging.conf ') Root_logger = Logging.getlogger (' root ') Error_logger = Logging.getlogger (' ERROR ') Root_logger.info (' I    NFO message. ') Error_logger.error (' error message. ')


This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1856763

Basic Primer _python-modules and packages. What are the best practices for logging module logging in operational development?

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.