Python primarily uses the logging module for log processingMany programs have logging requirements, and the log contains information that has a normal program Access log, there may be errors, warnings and other information output, Python's logging module provides a standard log interface,You can store logs in various f
The python logging module may be confusing.The python logging module is mainly a general-purpose log system provided by python. The method used is actually quite simple. The following describes how to call multiple python files wh
is ' a 'Format:handler using the specified format stringdetefmt: Using the specified time date formatlevel: Indicates the levels of root loggerstream: Initializes the Streamhandler with the specified stream. This parameter is not compatible with ' filename ', if two are available, ' stream ' is ignored --format Format % (Levelno) s--print log level values% (levelname) s--print log level name% (pathname) s--Prints the path of the currently executing p
Module-level functions
Logging.getlogger ([name]): Returns a Logger object that returns root if no name is specified loggerLogging.debug (), Logging.info (), logging.warning (), Logging.error (), logging.critical (): Setting the log level of root loggerLogging.basicconfig (): Create a Streamhandler for the log system with the default formatter, set the base configuration and add it to root logger
Example: logging_level_example.py
Copy Code code as follows:
Import
Module-level functionsLogging.getlogger ([name]): Returns a Logger object that returns root if no name is specified loggerLogging.debug (), Logging.info (), logging.warning (), Logging.error (), logging.critical (): Set the log level for root loggerLogging.basicconfig (): Establishes a streamhandler for the logging system with the default formatter, sets the base configuration and adds it to root loggerLog levelLogging.basicconfig (): Establishes a st
Many programs have logging requirements, and the log contains information that has normal program access logs, there may be errors, warnings and other information output, Python's logging module provides a standard log interface, you can store various formats of the log, logging log can be divided into Debug, info, warning, error, critical 5
IntroducedA logger is configured to has a log level. This log level describes the severity of the messages, the logger would handle. Python defines the following log levels:§ DEBUG: Low level system information for debugging purposes§ INFO: General system information§ WARNING: Information describing a minor problem that has occurred.§ ERROR: Information describing a major problem that has occurred.§ CRITICA
1.logging ModuleThe logging module is the log module used to record the state of the program's operation.There are five levels:
Debug
Info
Warning
Error
Critical
Logging.debug ('Debug Message') #logging. info ('Info Message') logging.warning ('warn message') #表明发生了一些意外, or problems will o
contents of the output, format can output a lot of useful information, as in the example above:% (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
(DEBUG, INFO, WARNING, ERROR, CRITICAL)% (levelname) S Text logging level forThe Message ("DEBUG","INFO", "WARNING","ERROR","CRITICAL") % (pathname) s full pathname of the source filewhereThe logging call was issued (ifAvailable)%(filename) s filename portion of pathname%(module) s Module (name portion of Filename)% (lineno) D Source line numberwhereThe
] WARNING This is WARNING message
Parameters of the Logging.basicconfig function:FileName: Specify the log file nameFileMode: Same as file function, specify open mode of log file, ' W ' or ' a 'Format: Specifies the formats and contents of the output, format can output a lot of useful information, as in the example above:% (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.a
format is to use the original information. The default time format is:%y-%m-%d%h:%m:%s################################ #3You can use numbers when you set the level or severity. Some constants are built into the logging module, as shown in the following table:#################################Here are some uses of logging:1. Using the logging module in multiple th
future (for Example. Low disk space "). The software will work as Expected.ERROR: a more serious problem, the software does not perform some functionsCRITICAL: a serious error that indicates that the program itself may not continue to runThese 5 levels, also correspond to 5 kinds of methods to hit the Log: debug, info, warning, error, critical. The default is warning, which is tracked only when it is warning or above.2. Log OutputThere are two ways t
pm-root-error -index:2222# 2016-09-06 15:10:38 pm-root-warning-index:5555# 2016-09-06 15:10:38 pm-root-warning-index: 999# above can be seen that the write level is greater than the log level before the log data is written Log () module functionFunction: Custom write level, write log "have parameter"How to use: module name. log (number of levels written, "Write content")Formats such as: Logging.log (+, ' log ')#!/usr/bin/env
.
% (ThreadName) s
The name of the thread. Probably not.
% (process) d
The process ID. Probably not.
% (message) s
User-Output messages
Python uses the logging module to log logs involving four main classes:Logger provides an interface that the application can use directly;Handler sends the log record (created by logger) to the appropriate de
) d: Print log current line number% (asctime) S: Print log time% (thread) d: Print thread ID% (threadname) s: Print thread name% (process) d: Print process ID% (mess Age) S: Print log information datefmt: Specifies the time format, same as Time.strftime () level: Sets the log levels by default to logging. Warningstream: Specifies the output stream that will log, can specify output to Sys.stderr,sys.stdout o
Logging moduleMany programs have logging requirements, and the log contains information that has normal program access logs, there may be errors, warnings and other information output, Python's logging module provides a standard log interface, you can store various formats of the log, logging log can be divided into de
Many programs have logging requirements, and the log contains information that has normal program access logs, there may be errors, warnings and other information output, Python's logging module provides a standard log interface, you can store various formats of the log, logging log can be divided into debug(),,, info() warning() and error() critical()5个级别, below
Python logging module is mainly Python provided by the general log system, the use of the method is actually very simple, this piece is not much introduction. The following is mainly about the use of the Python logging module, involving multiple
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.