Python Log tool (logging) Basic tutorial

Source: Internet
Author: User

What is logging?

A log is a tracking event that occurs when the software is running, and the software developer can quickly locate the problem through the log. The event is also of importance, that is, the severity of the incident.

When do I use the log?

Logging provides a set of convenient logging functions, which are debug (), info (), warning (), error (), critical ().

Determine how logging is used by the tasks that you want to perform.

The task you want to perform The best way to log records
A script or program appears on the terminal Print ()
Events that occur during a program's normal operation Logging.info () or Logging.debug ()
Alarms for specific events Logging.warning ()
Errors for specific events Raise
Report an error without throwing an exception Logging.error () or logging.critical ()

Level list (ascending order)

Level Usage Scenarios
DEBUG More information, usually for program troubleshooting
INFO Typically used to notify events that a program has occurred
WARNING Report abnormal events, but the software still works
ERROR More serious problems, the software cannot run a feature
CRITICAL Critical error, the software itself cannot be run

Log Default level is WARNING, can be understood as more than WARNING records, less than WARNING are not recorded , of course, you can also customize the log configuration scheme.

Tracked events are handled in different ways, the simplest way is to output to the console, and the other most common is to write to the file.

Example
Import logginglogging.warning (' Watch out! ') # Print a message to console Logging.info (' I told you ') # won't print anything

Execution results

WARNING:root:Watch out!
logging log to file
Import logginglogging.basicconfig (filename= ' Example.log ', level=logging. DEBUG) Logging.debug (' This message should go to the log file ') Logging.info ("So should this") logging.warning (' and this, too ‘)

View the contents of the Example.log file.

DEBUG:root:This message should go to the log fileINFO:root:So should thisWARNING:root:And this, too

If we run the above program several times, we will find that the content in the example.log file is appended and can be changed by using the FileMode parameter if you do not want to append.

Logging.basicconfig (filename= ' Example.log ', filemode= ' W ', level=logging. DEBUG)
multiple modules logging log to file

If your program has multiple modules, you can write to a log file in the following ways.

# Myapp.pyimport Loggingimport mylibdef Main ():    logging.basicconfig (filename= ' Myapp.log ', level=logging.info)    logging.info (' Started ')    mylib.do_something ()    logging.info (' finished ') if __name__ = = ' __main__ ':    Main ()
# Mylib.pyimport Loggingdef do_something ():    logging.info (' Doing something ')

View Myapp.log files after running myspp.py

INFO:root:StartedINFO:root:Doing somethingINFO:root:Finished
logging formatting log records

The logging can be formatted by formatting the string.

Import logginglogging.warning ('%s before '%s ', ' Look ', ' leap! ')

Run results

WARNING:root:Look Before you leap!
change the format of display information

To change the default display format, you must specify the display format you want.

Import logginglogging.basicconfig (format= '% (levelname) s:% (message) s ', level=logging. DEBUG) Logging.debug (' This message should appear on the console ') Logging.info ("So should this") logging.warning (' and this , too ')

Execution results

Debug:this message should appear on the consoleinfo:so should thiswarning:and this, too
Show date and time

To display the date and time, you can use the% (asctime) s parameter.

Import logginglogging.basicconfig (format= '% (asctime) s% (message) s ') logging.warning (' is "when this event was logged. ')

Execution results

2015-01-12 11:41:42,612 is while this event was logged.

The date display format defaults to ISO8601, and you can use the datefmt parameter if you want to customize the date and time format.

Import logginglogging.basicconfig (format= '% (asctime) s% (message) s ', datefmt= '%y-%m-%d%i:%m:%s%p ') logging.warning ( ' Is if this event was logged. ')

Execution results

2015-02-26 03:58:44 PM is if this event was logged.

So far, simple log entry is not a problem, if you want to know more about the usage of logging, please refer to the Python Log tool (logging) advanced Tutorial .

Python Log tool (logging) Basic tutorial

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.