Python Logging Module TOC
Objective
Design of logging Module
The logical inheritance structure of logger
Log output to multiple I/O devices
Log format and Content rich
The level of logging is also the classification of the log on the level dimension.
You can filter by the
Logging.addlevelname to add a custom logging level;
5) Use Logging.log to log custom logging level log;
The output text of the log is as follows:
Copy the Code code as follows:
2011-01-18 10:02:45,415-debug:this is a message
2011-01-18 10:02:45,463-info:this is a INFO
2011-01-18 10:02:45,463-critical:this is a CRITICAL issue
2011-01-18 10:02:45,463-error:this is a ERROR
2011-01-18 10:02:45,463-mycustomerro
disable a logging level;
4 use Logging.addlevelname to increase custom logging level;
5 use Logging.log to log the logging level log of the custom;
The output of the text log is as follows:
Copy Code code as follows:
2011-01-18 10:02:45,415-debug:this is a message
2011-01-18 10:02:45,463-info:this is a INFO
2011-01-18 10:02:45,463-critica
Python's log function, began to run on the python2.4, a variety of error, and later replaced by python2.7.9.Attach the Linux python upgrade process:1. Download the Python installation package---Python-2.7.9. Tar.xz 2, download to download directory, unzip tar -xzvf Python-3.3. 0.tgz 3, enter the uncompressed folder CD
introduction logging module is a python built-in standard module, mainly used for output log, you can set the level of output logs, log save path, log files and rollback, etc. It can be said that the logging module consists mainly of 4 parts: *logger Logger , which provides interfaces that application code can use directly NBSP;NBSP; *handler process
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 ') Lo
handler:Example one-how to not use the configuration file (Streamhandler):Import logging# set up logging to File-see previous sections for more Detailslogging.basicconfig (level=logging. DEBUG, format= '% (asctime) s% (name) -12s% (levelname) -8s% (message) s ', datefmt= '%m-%d %h:%m ', filename= '/temp/myapp.log ', f
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
Background
In a new project to join the log function, want to write a, but a chance, through Google found Python built a very powerful log module: logging. A cursory study, here are some of my notes.
Why use Logs
Trace some of the operation information of the program, in order to achieve the time to understand the status of the program, quickly capture program anomalies, timely detection of the purpose of
Reference: http://blog.csdn.net/zyz511919766/article/details/25136485Logging moduleThe log has 5 levels, debug,info,warning,error,critical, respectively, and the corresponding level numbers are 10,20,30,40,50Output log standards to the screen#!/usr/bin/env python#-*-coding:utf-8-*-ImportLogginglogging.debug ("Welcome to Akon Debug System") Logging.info ("Welcome
recently when doing automated testing, want to add a log to him, so use the logging module, the following is the Python add log several waysFirst, the Python code configuration (of course, there is a multi-module common Python code settings, this online there are many examples, it is not here to repeat)Import loggingIm
In Python,LoggingThe module mainly deals with logs. The so-called log, can be understood as the software in the course of operation, the recorded some of the operational information software developers can add logs according to their own needs, logs can help software developers understand the operation of the software information, the maintenance of the software is particularly important. Log level: Levels when it ' s used
created
% (asctime) s The current time in string form. The default format is "2003-07-08 16:49:45,896". The comma is followed by milliseconds
% (thread) d thread ID. Probably not.
% (threadname) s thread name. Probably not.
% (process) d process ID. Probably not.
% (message) s user-output message
Logger object: Can print the file, also can output the screenImport logging# creates a logger object and is named log =
handler
Handlers
The handler object is responsible for sending relevant information to the specified destination. Can be file, screen, network, socket, etc.
Handler.setlevel (LEL): Specifies the level of information being processed, and information below the LEL level is ignored
Handler.setformatter (): Select an output format for this Handler
Handler.addfilter (filt), Handler.removefilter (filt): Add or remove a filter object
Log Print to screen
Import Logginglogging.debug (' This was
Use Python logging to record log information such as DebugDebug above information written to fileInfo above info output in consoleImport Osimport loggingvde_logging_name = "vde_logging" Log_file_path = Os.path.join (Os.path.dirname (__file__), "Vde_ Regression.log ") def logger_initialization (): # 1. Logging logger = Logging.getlogger (vde_logging_name) logger.s
Here we need to modify Python's processing flow after the exception occurs Sys.excepthook is the exception handler for the Python system, overrides a custom processor, and starts the PDB after printing the exception information.ImportSYSdefinfo (type, value, TB):#Exception Type #Exception Value #Call Stack ifHasattr (SYS,'PS1')or notSys.stderr.isatty ():#we're in interactive mode or we don't have a tty-like #device, so we call the def
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.