Python Module-logging module (i)

Source: Internet
Author: User

Logging module for writing log files

There are 5 levels, debug (), info (), warning (), error (), and Critical (), the highest level is critical ()

Debug () debug mode, info () is the normal information, warning () is warning, error () is wrong, critical () is a serious issue

Normal printing
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.debug (' the Debug ') Logging.info (' the info ') Logging.warning (' Request outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

Only the warning level and subsequent statements are printed in 5 levels

Write log file
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (filename= ' Log_test.log ', level= Logging.info) logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request Outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Level=loggin.info, the log level is set to info, only logs with info or higher than info will be written to the log file.

Write the contents of the log file

Write content only from info level, debug level is lower than info, no write

Log formatting

% (name) s the name of the log writer

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (name) s ', level=logging. INFO) logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') ) Logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

% (Levelno) s log level in digital form,% (levelname) s in text form

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (levelno) s,% (levelname) s ' , Level=logging.info) logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

% (Lineno) d The line of code where the statement of the log output function is called

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (Lineno) d ', level=logging. INFO) logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') ) Logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

% (pathname) s the full path name of the module that invokes the log output function

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (pathname) s ', level= Logging.info) logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request Outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

% (filename) s The file name of the module that called the log output function

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (filename) s ', level= Logging.info) logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request Outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

% (module) s call the module name of the log output function

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (module) s ', level=logging. INFO) logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') ) Logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

is the file name, no suffix

% (FuncName) s Call the function name of the log output function

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (funcName) s ', level= Logging.info) def log_test ():    logging.debug (' This Python is running ')    logging.info (' The infomation of Python ')    logging.warning (' Request outtime ')    logging.error (' The Python Run error ')    logging.critical (' the server Down ') log_test ()

Run results

% (created) F current time, represented by the UNIX standard floating-point number representing the time

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (created) f ', level=logging . INFO) Logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

% (relativecreated) d output log information number of milliseconds since it was created

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import loggingimport timelogging.basicconfig (format= '% ( relativecreated) d ', Level=logging.info) logging.debug (' This Python is running ') Time.sleep (2) logging.info (' The Infomation of Python ') logging.warning (' Request outtime ') logging.error (' The Python Run Error ') logging.critical (' The Server Down ')

Run results

The result is uncertain.

% (asctime) s string as the current time, the default format is "2018-02-15 17:59:31,546", after the comma is the millisecond

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (asctime) s ', level=logging . INFO) Logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

% (thread) d current thread number,% (threadname) s current thread name

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (thread) d,% (threadname) s ' , Level=logging.info) logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

% (process) d process number

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (process) d ', level=logging . INFO) Logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

% (message) s user-output message

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (format= '% (message) s ', level=logging . INFO) Logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

Formatted log writes
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import logginglogging.basicconfig (filename= ' Log_test.log ', level= logging.info,format= '% (message) s ', datefmt= '%m ') logging.debug (' This Python is running ') logging.info (' The infomation of Python ') logging.warning (' Request outtime ') logging.error (' The Python Run Error ') logging.critical (' The server Down ')

Run results

Python module-logging module (i)

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.