A simple example of the Python (2.7.6) Standard Log Module, python2.7.6

Source: Internet
Author: User

A simple example of the Python (2.7.6) Standard Log Module, python2.7.6

The logging module in the Python Standard Library provides a set of standard APIs to process log information printing.

import logginglogging.basicConfig(    level    = logging.DEBUG,    format   = '%(asctime)s [%(threadName)s] (%(filename)s:%(lineno)d) %(levelname)s - %(message)s',    datefmt  = '%Y-%m-%d %H:%M:%S',    filename = 'myapp.log',)logging.debug('This is a debug message')logging.info('This is an info message')logging.warning('This is a warning message')

The output log in myapp. log is as follows:

2015-03-11 15:54:34 [MainThread] (logging_demo.py:10) DEBUG - This is a debug message2015-03-11 15:54:34 [MainThread] (logging_demo.py:11) INFO - This is an info message2015-03-11 15:54:34 [MainThread] (logging_demo.py:12) WARNING - This is a warning message

Parameter descriptions of the logging. basicConfig function:

Parameters Description
Filename File Name of the FileHandler instance created by the user
Filemode Log File opening mode. The default value is 'A'
Format Log output format
Datefmt Time Output Format
Level Log level. The size relationship is CRITICAL (50)> ERROR (40)> WARNING (30)> INFO (20)> DEBUG (10)> NOTSET (0)
Stream Used to initialize the stream of StreamHandler. this parameter is specified with filename and will be ignored.

 

 

 

 

 

 

 

Format symbol in logging:

Symbol Description
% (Asctime) s Time
% (Filename) s File Name
% (FuncName) s Function Name
% (Levelname) s Log Level Value
% (Levelno) s Log Level
% (Lineno) d Row number
% (Module) s Module
% (Message) s Log message
% (Name) s Log name
% (Pathname) s Logger name
% (Process) d Process ID
% (ProcessName) s Process name
% (Thread) d Thread ID
% (ThreadName) s Thread name

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.