Python Log module logging

Source: Internet
Author: User

1. Basic usage

Python provides a standard log interface, which is the logging module. The log level has debug, info, WARNING, error, critical five (level in turn), respectively, the corresponding function is debug (), info (), WARNING (), error (), Critical ().

You can see that the debug () and info () methods do not display any information because the default log level is warning, so logs below this level are not logged.

You can use the function Basiccinfig to modify the log level

>>> Import logging>>> logging.basicconfig (level=logging.info) >>> logging.info ("Nihao") Info:root:nihao>>> logging.debug ("DFASL") >>> Logging.basicconfig (level=logging. DEBUG) >>> Logging.info (4) info:root:4>>>

The Basicconfig () function can also define more content, such as

Logging.basicconfig (format=log_format,datefmt= '%y-%m-%d%h:%m:%s%p ', level=logging. DEBUG)

Example

Import Logginglog_format='% (filename) s% (funcName) s% (asctime) s% (message) s'Log_filename="Logging_test.log"logging.basicconfig (filename=log_filename, Format=log_format, datefmt='%y-%m-%d%h:%m:%s:%s%p', filemode='W', level=logging.info) logging.warning ("warning###########") logging.warning ("[Email protected]@@@@@@@@@@@@@") Logging.error ("error~~~~~~~~~~~~~~~~")

Result (output file "Logging_test.log" content)

del.py <module> 2015-04-30 16:29:02:02 pm warningdel.py <module> 2015-04-30 16:29:02:02 pm errordel.py <mo dule> 2015-04-30 16:29:02:02 PM Error

Parameters of the Logging.basicconfig function

FileName: Specifies the log file name FileMode: The same as the file function, specifies the open mode of the log files, ' W ' or ' A ' format: Specifies the formats and contents of the output, and format can output a lot of useful information, as shown in the example above:    % ( Levelno) S: Print log level value    % (levelname) S: Print log level name    % (pathname) s: Prints the path of the currently executing program, which is actually sys.argv[0]    % (filename) s: Print current executing program name    % (funcName) S: Print log current function    % (Lineno) d: Print log current line number    % (asctime) s: Time to print log    % (thread) d: Print thread ID    % (threadname) s: Print thread name    % (process) d: Print process ID    % (message) s: Print log information datefmt: 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 simultaneously

Note : The log is set by using the Basicconfig () method, and the default way that the log is written to the file is ' a ', which is ' append ', if you want to overwrite the file, use filemode= ' W '.

The logging module is very powerful and can customize more complex log forms through a more free interface. The following 3 objects need to be used logger, formatter, handler. The following is an introduction to logger

2. Logger

The Logger object provides a log interface directly.

The handler object can be used to write the contents of the log to different places.

Example: outputting logs to files and screens at the same time

#Coding:utf-8ImportLogginglogger=Logging.getlogger () logger.setlevel (logging. DEBUG) Formatter= Logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s') FH= Logging. Filehandler ("Test.log") Ch=logging. Streamhandler () Fh.setformatter (formatter) ch.setformatter (formatter) logger.addhandler (FH) # You can set the AddHandler add content (FH, CH, fh+ch) to set the output position logger.addhandler (CH) logger.error ("error$$$$$$$$$$") Logger.debug ("aaaaa**********")

Python Log module logging

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.