Python Learning Notes-basic "Sixth Week"--logging module

Source: Internet
Author: User

Logging of common modules

Module for easy logging and thread-safe

Import Logging  logging.basicconfig (filename= ' Log.log ',                    format= '% (asctime) s-% (name) s-% (LevelName) s-% ( Module) s:  % (message) s ',                    datefmt= '%y-%m-%d%h:%m:%s%p ',                    level=10) logging.debug (' Debug ') Logging.info ( ' Info ') logging.warning (' Warning ') logging.error (' Error ') logging.critical (' critical ') logging.log (' log ')

for Rating:

CRITICAL = 50FATAL = Criticalerror = 40WARNING = 30WARN = Warninginfo = 20DEBUG = 10NOTSET = 0

only operations larger than the current log level will be logged.

For formats, the following properties are configured:

---------------------------------------------------------------------------------------------

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 we look at how to use.

The simplest usage

Import Logging logging.warning ("User [Alex] attempted wrong password more than 3 times") logging.critical ("Server was down") #输出WARNING: Root:user [Alex] attempted wrong password more than 3 TimesCRITICAL:root:server was down

Take a look at what these log levels mean, respectively.

Level When
it ' s used
DEBUG Detailed information, typically of interest only when diagnosing problems.
INFO Confirmation that things is working as expected.
WARNING An indication this something unexpected happened, or indicative of some problem in the "near" (e.g. ' disk space low ') . The software is still working as expected.
ERROR Due to a more serious problem, the software have not been able to perform some function.
CRITICAL A serious error, indicating the program itself is unable to continue running.

  

If you want to write the log in a file, it's easy.

Import Logging Logging.basicconfig (filename= ' Example.log ', level=logging.info) logging.debug (' This message should go To the log file ') Logging.info (' so should this ') logging.warning (' And this, too ')

One of the level=loggin in the following sentence. Info means that the logging level is set to info, that is, only logs that are higher than the log is info or the info level will be recorded in the file, in this case, the first log is not recorded, if you want to record the debug log, Change the log level to debug on the line.

Logging.basicconfig (filename= ' Example.log ', level=logging.info)

Feel the above log format forgot to add time, log do not know how to line, the following to add!

Import logginglogging.basicconfig (format= '% (asctime) s% (message) s ', datefmt= '%m/%d/%y%i:%m:%s%p ') logging.warning ( ' Is if this event was logged. ') #输出12/12/2010 11:46:36 AM is while this event was logged.

If you want to print log in the screen and file log at the same time, you need to know a little bit more complex knowledge.

The logging library takes a modular approach and offers several categories of components:loggers, handlers, filters, and Formatters.

    • Loggers expose the interface that application code directly uses.
    • Handlers send the log records (created by loggers) to the appropriate destination.
    • Filters provide a finer grained facility for determining which log records to output.
    • Formatters specify the layout of log records in the final output.
Import logging #create Loggerlogger = Logging.getlogger (' Test-log ') logger.setlevel (logging. DEBUG)  # Create console handler and set level to DEBUGCH = logging. Streamhandler () Ch.setlevel (logging. DEBUG) # Create file handler and set level to WARNINGFH = logging. Filehandler ("Access.log") fh.setlevel (logging. WARNING) # Create Formatterformatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') # Add Formatter to CH and fhch.setformatter (Formatter) FH . Setformatter (Formatter) # Add ch and fh to Loggerlogger.addhandler (CH) logger.addhandler (FH) # ' application ' Codelogger.debug (' Debug Message ') Logger.info (' info message ') Logger.warn (' Warn message ') logger.error (' Error message ') logger.critical (' Critical message ')

Reference Links:

Http://www.cnblogs.com/alex3714/articles/5161349.html

Http://www.cnblogs.com/wupeiqi/articles/4963027.html

Python Learning Notes-basic "Sixth Week"--logging module

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.