Python Third party Library series VII--logging Library

Source: Internet
Author: User

Running the program is like walking life, some beautiful, not beautiful things need to record down, slowly weighing ~

The logging module is born and records what you want to record.

First, print the log to the screen

Import Logging

Logging.debug (' This is a bug information ')
logging.info (' The ' is a info information ')
Logging.warning (' This is a warning information ')
logging.error (' The ' is a error information ')
logging.critical (' is a critical information ')

# screen Print
# WARNING:root:this is a WARNING information
# ERROR:root:this is a ERROR information
# Critical:root: This is a critical information
Generally, the log level size relationship is: CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET, and of course you can define the logging level yourself. Also, by default, logging prints the log to the screen with a log level of warning, so we write debug and info that are ignored and won't print.

Second, print the log to the file

1. Use Logging.basicconfig () to configure

import logging Logging.basicconfig (level=logging.
                    DEBUG, format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ', Datefmt= '%a,%d%b%Y%h:%m:%s ', filename= ' Myapp.log ', filemode= ' W ') logging.de Bug (' This is a bug information ') logging.info (' The ' is an info information ') logging.warning (' This is a warning information  ') Logging.error (' This is an error information ') logging.critical (' This is a critical information ') # Myapp.log file Print #Mon, Nov 2014 23:19:32 Loggingtest.py[line:11] DEBUG is a bug information #Mon Nov 2014 23:19:32 NE:12] Info This is an info information #Mon Nov 2014 23:19:32 loggingtest.py[line:13] WARNING This is a WARNING inform ation #Mon, Nov 2014 23:19:32 loggingtest.py[line:14] Error This is a error information #Mon, Nov 2014 23:19:32 Logg INGTEST.PY[LINE:15] CRITICAL This is a CRITICAL information 
logging.basicconfig function Parameters:
FileName: Specify log file name and print to screen if not specified
FileMode: With the same meaning as the file function, specify the open mode of the log file, ' W ' or ' a '
Format: Specifies the formatting and content of the output, and the format can output a lot of useful information, as shown in the previous example:
% (Levelno) S: Print log-level values
% (levelname) S: Print log level name
% (pathname) s: Prints the path of the current execution program, which is actually sys.argv[0]
% (filename) S: Print the current executing program name
% (funcName) s: Print the current function of the log
% (Lineno) d: Print the current line number of the log
% (asctime) s: Time to print log
% (thread) d: Print thread ID
% (threadname) s: Print thread name
% (process) d: printing process ID
% (message) s: Print log information
DATEFMT: Specify time format, with Time.strftime ()
Level: Set the logging levels, default to logging. WARNING
Stream: Specifies the output stream of the log, which specifies the output to the Sys.stderr,sys.stdout or file, the default output to Sys.stderr, and the stream is ignored when the stream and filename are specified at the same time.

2. Use Filehandler to configure

Import logging from
logging import filehandler
log_path = '/home/tops/adms-agent/logs '

Logging.basicconfig (level=logging. DEBUG)
File_time_handler = Filehandler (log_path+ '/adms-agent.log ')
file_time_handler.setformatter ( Logging. Formatter ("% (asctime) s-% (LevelName) s-{% (filename) s:% (funcName) s:% (Lineno) d}:% (message) S"))
File_time_ Handler.suffix = "%y-%m-%d.log"
log = Logging.getlogger ("adms-agent")
Log.addhandler (File_time_handler) Logging.debug (' This is

a bug information ') logging.info (' The ' is
a info information ')
logging.warning (' This is a warning information ')
logging.error (' The ' is a error information ')
logging.critical (' It is a critic Al Information ')

The above is just an example of using Filehandler to write a log to a file, there are many kinds of log redirection methods, such as:

Logging. Streamhandler: Log output to stream, can be sys.stderr, sys.stdout, or file
Logging. Filehandler: Log output to File

Log rollback mode, when used in Rotatingfilehandler and Timedrotatingfilehandler
Logging.handlers.BaseRotatingHandler
Logging.handlers.RotatingFileHandler
Logging.handlers.TimedRotatingFileHandler

Logging.handlers.SocketHandler: Remote output log to TCP/IP sockets
Logging.handlers.DatagramHandler: Remote output log to UDP sockets
Logging.handlers.SMTPHandler: Remote output log to mail address
Logging.handlers.SysLogHandler: Log Output to Syslog
Logging.handlers.NTEventLogHandler: Remote output log to the Windows NT/2000/XP event log
Logging.handlers.MemoryHandler: Log output to memory in the formulation buffer
Logging.handlers.HTTPHandler: Remote output to HTTP server via "get" or "POST" three, logging is thread-safe



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.