7. Module three logging module

Source: Internet
Author: User
Tags time in milliseconds

1. Logging module
# Python's logging module provides a standard log interface through which you can store logs in various formats, logging logs can be divided into # Debug (), info (), warning (), error () and critical () 5 levels # Level when it's used# DEBUG detailed information, typically of interest when diagnosing problem s.# INFO Confirmation that things is working as expected.# WARNING an indication that something Unex Pected happened, or indicative of some problem in the near future (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 this program itself could be unable to continue Running. Different log levels correspond to different values, as follows: CRITICAL = 50FATAL = Criticalerror = 40WARNING = 30WARN = Warninginfo = 20DEBUG = 10 Format: Loggi Ng. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ')% (name) s name of the logger (logging channel)% (Levelno) s Numeric logging level for the message (Debug, INFO, WARNING, ERROR, CRITICAL)% (levelname) s Text logging level for the message ("Debug"  , "INFO", "WARNING", "ERROR", "CRITICAL")% (pathname) s full pathname of the source file where the          Logging call was issued (if available)% (filename) s filename portion of pathname% (module) s                    Module (name portion of filename)% (Lineno) d Source Line number where the logging call was issued                     (if available)% (FuncName) s Function name% (created) f time when the LogRecord is created (Time.time () return value)% (asctime) s textual time when the LogRecord is created% (msecs) d Millis                    Econd portion of the creation time% (relativecreated) d time in milliseconds when the LogRecord is created, Relative to the time the logging module is loaded (typically at application startup time)% (thread) D Thread ID (if Available)% (ThreadName) s Thread name (if available)% (process) d process ID (if available)% (message) s The result of Record.getmessage (), computed just as the record is emitted

Print Log to screen:

# #日志打印到屏幕logging. Warning ("User [Alex] attempted wrong password more than 3 times") logging.critical ("Server was down")

Log output to file:

Logging.basicconfig (filename= ' Python.log ', level=logging.info,format= '% (asctime) s% (message) s ', datefmt= '%m/%d/%y %i:%m:%s%p ') logging.debug (' This message should go to the log file ') Logging.info (' so should this ') logging.warning (' and th is, too ')

Screen log bidirectional output:

#如果需要日志和屏幕同时输出的话, the following action is required # The logging library takes a modular approach and offers several categories of Components:logger s, handlers, filters, and formatters.# loggers expose the interface that application code directly uses.# handlers send th e log records (created by loggers) to the appropriate destination.# Filters provide a finer grained facility for Determini ng which log records to output.# formatters Specify the layout of log records in the final output.# 1, create a log object Logger=logg Ing.getlogger (' Test_log ') logger.setlevel (logging. DEBUG) # 2, create the console handler and set the rank ch = logging. Streamhandler () Ch.setlevel (logging. WARNING) fh = logging. Filehandler () Fh.setlevel (logging. DEBUG) # 3, create log format formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') Ch.setformatter (Formatter) fh.setformatter (Formatter # 4, add log instance to Handlerlogger.addhandler (CH) logger.addhandler (FH) # 5, Print log test 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. ERROR) # Create file handler and set level to WARNINGFH = logging. Filehandler ("Python.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 ')

  

7. Module three 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.