Python Common functions

Source: Internet
Author: User
Tags remove filter string format

Tag: Eve process body resolved to open the return character. com

First, the use of logging module

Module for easy logging and thread-safe

CRITICAL = 50

FATAL = CRITICAL

ERROR = 40

WARNING = 30

WARN = WARNING

INFO = 20

DEBUG = 10

NOTSET = 0

Simple to use

#!/usr/local/bin/python#-*-coding:utf-8-*-import logginglogging.debug (' Debug Message ') Logging.info (' info message ') ) Logging.warn (' Warn message ') logging.error (' Error message ') logging.critical (' Critical message ')
#输出: WARNING:root:warn messageERROR:root:error messageCRITICAL:root:critical Message

By default, the logging module prints the log to the screen (stdout) with a log level of warning (that is, only log information that is higher than warning at the log level is output), as shown in the log format:

Log level

Level

When to use

DEBUG

Detailed information that is typically interesting when debugging a problem.

INFO

Prove that things work as expected.

WARNING

Indicates that something has happened, or that a problem will occur in the near future (such as ' Disk full '). The software is still working properly.

ERROR

Because of the more serious problem, the software has been unable to perform some functions.

CRITICAL

A critical error indicates that the software cannot continue running.

Simple configuration

#!/usr/local/bin/python#-*-coding:utf-8-*-import logging# in the following way simple configuration output mode and log level logging.basicconfig (filename= ' Logger.log ', Level=logging.info) logging.debug (' Debug Message ') Logging.info (' INFO message ') Logging.warn (' Warn Message ') Logging.error (' Error message ') logging.critical (' Critical message ')

Output:
The standard output (screen) does not display any information and finds that Logger.log is generated under the current working directory as follows:

INFO:root:info MessageWARNING:root:warn messageERROR:root:error messageCRITICAL:root:critical Message

  

Because the log level is info set through level=logging.info , all log information is output.

Here comes the question again.

    1. Can you configure that information with the above configuration method?

Before you solve the above problems, you need to understand a few of the more important concepts,Logger,Handler,Formatter,Filter

A few important concepts

    • The Logger recorder exposes an interface that the application code can use directly.
    • Handler processor that sends (logger-generated) log records to the appropriate destination.
    • Filter filters provide better granularity control, which determines which log records are output.
    • The Formatter formatter, which indicates the layout of the log records in the final output.

Logger Recording Device

Logger is a tree-level structure, you must create a logger instance before using interface debug,info,warn,error,critical, that is, create a logger, and if you do not create it explicitly, create a root logger by default. and apply the default log level (WARN), the processor handler (Streamhandler, the log information printout on the standard output), and the formatter formatter (the default format is the format of the output in the first simple-to-use program).

Method of creation: Logger = Logging.getlogger (logger_name)

After you create the logger instance, you can use the following methods to set the log level to increase the processor handler.

    • Logger.setlevel (logging. Error) # Set the log level to error, that is, only logs with a log level greater than or equal to error will be output
    • Logger.addhandler (handler_name) # Add a processor to the logger instance
    • Logger.removehandler (handler_name) # Remove a processor for the logger instance

Handler Processor

There are many kinds of handler processor types, more commonly used are three,Streamhandler,filehandler,nullhandler, details can be accessed Python Logging.handlers

After you create Streamhandler, you can set the log level by using the following methods, set the formatter formatter, and add or remove filter filters.

    • Ch.setlevel (logging. WARN) # Specifies the log level, and logs below the WARN level are ignored
    • Ch.setformatter (formatter_name) # set a formatter formatter
    • Ch.addfilter (filter_name) # Adds a filter that can add multiple
    • Ch.removefilter (filter_name) # Delete a filter

Streamhandler

Create method: sh = logging. Streamhandler (Stream=none)

Filehandler

Method of creation: FH = logging. Filehandler (filename, mode= ' A ', Encoding=none, Delay=false)

Nullhandler

The Nullhandler class is located in the core logging package and does not do any formatting or output.
It is essentially a "do-nothing" handler, which is used by library developers.

Formatter Format Editor

Use the formatter object to set the last rule, structure, and content of the log information, and the default time format is%y-%m-%d%h:%m:%s.

Create method: Formatter = logging. Formatter (Fmt=none, Datefmt=none)

Where FMT is a formatted string of messages, Datefmt is a date string. If the FMT is not specified, the '% (message) s ' will be used. If you do not specify DATEFMT, the ISO8601 date format will be used.

Filter Filter Filters

Handlers and loggers can use filters to accomplish more complex filtering than levels. The filter base class only allows events below a specific logger level. For example, the filter initialized with ' a.b ' allows logger ' a.b ', ' a.b.c ', ' a.b.c.d ', ' A.B.D ' and other recorded events, logger ' a.bb ', ' b.a.b ' and so on. If initialized with an empty string, all events are accepted.

Create method: Filter = logging. Filter (name= ")

Here is a summary of related concepts:

With these concepts in hand, it is important to be clear that logger is a tree-shaped hierarchy;
Logger can contain one or more handler and filter, i.e. logger is a one-to-many relationship with handler or Fitler;
A logger instance can add multiple handler, a handler can add multiple formatters or multiple filters, and the log level will inherit.

Logging Work Flow

Logging Module Use procedure

    1. The first time you import the logging module or use the Reload function to re-import the logging module, the code in the logging module will be executed, which will result in the default configuration of the logging log system.
    2. Custom configuration (optional). The logging standard module supports three configuration methods: Dictconfig,fileconfig,listen. Where Dictconfig is configured through a dictionary logger,handler,filter,formatter;fileconfig is configured by a file, while listen listens on a network port and configures it by receiving network data. Of course, in addition to the above collectivization configuration, you can also directly invoke the methods in the Logger,handler and other objects in the code to explicitly configure.
    3. Use the GetLogger function in the global scope of the logging module to get an instance of the Logger object (the argument is a string representing the name of the Logger object instance, which is the name to get the corresponding Logger object instance).
    4. Log information is logged using methods such as debug,info,error,warn,critical in the Logger object.

Logging Module Processing Flow

    1. Determine whether the level of the log is greater than the level of the Logger object, or, if it is greater than, execute down, otherwise the process ends.
    2. Generate logs. The first step is to determine if there is an exception, and if so, add the exception information. The second step is to handle the placeholder in the logging method (such as debug,info, etc.), which is a generic string formatting process.
    3. Filter by using filters registered in the Logger object. If there are multiple filters, the filter is filtered in turn, and if a filter returns false, the filter ends and the log information is discarded, no longer processed, and the process is ended. Otherwise, the processing process is executed down.
    4. Finds handlers in the current logger object and, if no handler is found, looks up to the logger object's parent logger and, if one or more handler is found, uses handler to process the log information. However, in each handler process log information, the log information will first determine whether the level is greater than the level of handler, if greater than the execution (by the Logger object into the handler object), otherwise, the processing process ends.
    5. Executes the filter method in the Handler object, which executes the filter that is registered to the handler object in turn. If there is a filter that determines that the log information is false, then all the filter is no longer executed, and the log information is discarded directly, and the processing process ends.
    6. Use the formatter class to format the final output. Note: Formatter with the 2nd step of the string format, it will add additional information, such as the time the log was generated, the source of the origin of the log sources of files, and so on.
    7. Really output log information (to network, file, terminal, mail, etc.). As to which destination to export to, it is determined by the kind of handler.

Note: The above content excerpt from the third article, the content of a slight change, reproduced hereby declare.

Look at the log configuration again

How to configure

    • Explicitly create logger logger, processor handler, and formatter formatter, and make relevant settings;
    • Configure it in a simple way, using the Basicconfig () function to configure it directly;
    • Configuration files are configured using the Fileconfig () function to read the configuration file;
    • Configuration by configuration dictionary, using the Dictconfig () function to read configuration information;
    • Configuration over the network, using the Listen () function for network configuration.

Basicconfig keyword Parameters

Td>

keyword

description

filename

filemode

format

handler use the specified format string.

datefmt

level

stream

Useful format formats

Format

Describe

% (Levelno) s

Print the value of the log level

% (LevelName) s

Print Log level name

% (pathname) s

To print the path of the current executing program

% (filename) s

Print 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 the log

% (thread) d

Print thread ID

% (ThreadName) s

Print Thread Name

% (process) d

Print Process ID

% (message) s

Print log information

Useful DATEFMT formats

Reference Time.strftime

Configuration examples

Explicit configuration

Use the program logger.py as follows:

#!/usr/local/bin/python#-*-coding:utf-8-*-import logging# Create loggerlogger_name = "example" logger = Logging.getlogger (logger_name) logger.setlevel (logging. DEBUG) # Create File Handlerlog_path = "./log.log" FH = logging. Filehandler (Log_path) fh.setlevel (logging. WARN) # Create FORMATTERFMT = "% (asctime) -15s% (levelname) s% (filename) s% (Lineno) d% (process) d% (message) s" datefmt = "%a% D%b%Y%h:%m:%s "formatter = logging. Formatter (FMT, DATEFMT) # Add handler and Formatter to Loggerfh.setformatter (Formatter) Logger.addhandler (FH) # Print Log Infologger.debug (' Debug Message ') Logger.info (' info message ') Logger.warn (' Warn message ') logger.error (' Error message ') logger.critical (' Critical message ')

  

File configuration

The configuration file logging.conf is as follows:

KEYS=ROOT,EXAMPLE01[LOGGER_ROOT]LEVEL=DEBUGHANDLERS=HAND01,HAND02[LOGGER_EXAMPLE01]HANDLERS=HAND01, Hand02qualname=example01propagate=0[handlers]keys=hand01,hand02[handler_hand01]class=streamhandlerlevel= Infoformatter=form02args= (Sys.stderr,) [Handler_hand02]class=filehandlerlevel=debugformatter=form01args= (' Log.log ', ' a ') [formatters]keys=form01,form02[formatter_form01]format=% (asctime) s% (filename) s[line:% (lineno) d]% ( LevelName) s% (message) s

Use the program logger.py as follows:

#!/usr/local/bin/python#-*-coding:utf-8-*-import loggingimport logging.configlogging.config.fileConfig ("./ Logging.conf ") # Create Loggerlogger_name =" example "logger = Logging.getlogger (logger_name) logger.debug (' Debug Message ') logger.info (' info message ') Logger.warn (' Warn message ') logger.error (' Error message ') logger.critical (' Critical message ')

  

  

Python Common functions

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.