leap!change the format of display informationTo change the default display format, you must specify the display format you want.Import logginglogging.basicconfig (format= '% (levelname) s:% (message) s ', level=logging. DEBUG) Logging.debug (' This message should appear on the console ') Logging.info ("So should this") logging.warning (' and this , too ')Execution resultsDebug:this message should appear on the consoleinfo:so should thiswarning:and th
Python logging modulePython logging provides a standard logging interface, and the Python logging log is divided into 5 levels:debug(), info(), warning(), error() and critical()Simple usageimport logginglogging.warning("warning...
The Python standard log module uses Handler control log messages to write to different destinations, such as files, streams, messages, sockets, and so on. In addition to the Streamhandler, Filehandler, and Nullhandler definitions in the logging module, the other Handler are defined in the Logging.hangdlers module. These Handler are: Watchedfilehandler, Rotatingfilehandler, Timedrotatingfilehandler, Socketha
[Blog recommendation] logging module of python Howto (1)
This blog post is from Bkjia. If you have any questions, go to the blog page for an interactive discussion!Blog: http://xdzw608.blog.51cto.com/4812210/1608718
This article comes from the understanding of the source code added to the howto-logging section in py2.7.9 docs. The official do
#coding: Utf-8import loggingclass Testlog (object): ' Logging ' def __init__ after encapsulation (self, logger = None): ' "Specifies the file path to save the log, the log level, and the call file to deposit the log into the specified file ' # ' creates a logger Self.logger = logging. GetLogger (Logger) self.logger.setLevel (logging. DEBUG) # Create a handler for
be added through the AddHandler () methodHandler.setlevel (LEL): Specifies the level of information being processed, and information below the LEL level is ignoredHandler.setformatter (): Choose a format for this HandlerHandler.addfilter (filt), Handler.removefilter (filt): Add or remove a filter objectMultiple handler can be attached to each logger. Next, let's introduce some common handler:1) logging. StreamhandlerUsing this handler, you can output
For python, two problems have plagued me over the past few days:1. The current row number and function name cannot be obtained directly in python. This is a question raised by someone in the forum. the people below are just wondering why python does not provide _ line _ and _ func __like _ file __, however, no solution was found.2. How can a function call itself
,' Encoding ': ' Utf-8 ',},}, ' loggers ': {# Logging.getlogger (__name__) received the configuration #把上面定义的两个Handler加到这里 , log is written to the file and hit the terminal. ' handlers ': [ ' default ', ' console ') , ' level ': ' DEBUG ', # Up (higher level logging) pass ' propagate ': true,},},}def load_my_logging_cfg (): Logging.config.dictConfig (logging_dic) #生成个log实例, __name __ The current module name is passed in, unable to find the lo
(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
Four weeks five sessions (November 10)First, loggingLog is our key tool to troubleshoot problems, write log records, and when we have problems, you can quickly locate the code range to modify. Python gives us developers a good log module, let's introduce the logging module:First, let's look at an example:Import Logginglogging.debug (' This was debug message ') Logging.info (' This is Info message ') logging
. Timedrotatingfilehandler (' Web.log ', when= ' s ', interval=3,backupcount=5) Logger.addhandler (File_handler) file_ Formatter = logging. Formatter ('% (name) s-% (asctime) s-% (levelno) s-% (message) s ') File_handler.setformatter (file_formatter) logger.debug (' The Debug ') logger.info (' the info ') logger.warning (' The Warning ') logger.error (' the error ') logger.critical (' The Critical ')Generates one every 5 seconds and needs to be run be
Logging.basicconfig configuration logging to file A, and then using Logging.filehandler to generate records to file B loggerWhen using this logger log, both files A and B are logged simultaneously, and it feels like the mechanism is quite unexpected.#-*-Coding:utf8-*-import logginglogging.basicconfig ( level=logging. DEBUG, format= ' [% (asctime) s][% (process) d:% (thread) d][% (levelname) s
The two methods output by using the python logging module in stdout are described in detail.
Use the logging module of python to output data in stdout
Preface:
When using the python logging module, in addition to
In the previous article we learned why Python is debugging, and the two methods of Python debugging, but the debugging method is not finished, so in this article we will talk about the remaining two debugging methods. Hopefully, these kinds of debugging methods will help you get on the path to learning python faster.
Logging
#很多程序都有记录日志的需求, and the information contained in the log is a normal program access log, there may be errors, warnings and other information output, Python logging module provides a standard log interface, you can store the log in various formats, logging log can be divided into debug (), info (), warning (), error () andcritical () 5 levels importlogging #打印日志
(' Beginman ') #生成一个日志对象Print log2 #"Nameless"Log3 = Logging.getlogger ()Print Log3 #"The best way"Log = Logging.getlogger (__name__) #__name__ is the module's name in the Python package namespace.Print Log #Print __name__ #__main__Third, Logger objectby Logging.getlogger (NAM) to get the Logger object,Class logging. LoggerThe following properties and methods are available:1, Logger.propagatePrint Log.prop
: Specify the time format, same as Time.strftime () level: Set the logging level, which defaults to logging. Warningstream: Specifies the output stream that will log, can specify output to Sys.stderr,sys.stdout or file, default output to Sys.stderr, stream is ignored when stream and filename are specified simultaneouslyNote : The log is set by using the Basicconfig () method, and the default way that the lo
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.