Python module: logging

Source: Internet
Author: User
Many programs need to record logs, and the information contained in the logs has normal program access logs, and may also have errors, warnings, and other information output, the logging module of python provides a standard log interface. you can store logs in various formats. logging logs can be classified into debug, info, warning, error, and critical, next, let's take a look at how to use many programs to record logs, and the information contained in the logs is normal program access logs, and may also have errors, warnings, and other information output, the logging module of python provides a standard log interface. you can store logs in various formats. logging logs can be divided into five levels: debug, info, warning, error, and critical, next let's take a look at how to use

First knowledge of the module:

# Logging first recognized import logging. warning ("user [James] attempted wrong password more than 3 times") logging. critical ("server is down") # WARNING: root: user [James] attempted wrong password more than 3 times # CRITICAL: root: server is down

The above code is the simplest way. The content in the brackets is the printed information, and the method after logging is the log level. let's take a look at the details of the five levels of logging.

If you want to write logs to a file, it is easy:

# Import logging to print logs to files. basicConfig (filename = "example. log ", level = logging. INFO, format = "% (asctime) s % (message) s", datefmt = "% m/% d/% Y % H: % M: % S [% A] ") # H 24-hour format I 12-hour format A weeks complete a weeks short for p AM/PM logging. debug ("This message shocould go to the log file") logging.info ("So shocould this") logging. warning ("And this, too ")

Logging. basicConfig defines the input file path, the level of the input log information, the input format, and the custom format. after the code is executed, the example. log file generates the following information:

10/31/2016 17:16:17 [Monday] So should this10/31/2016 17:16:17 [Monday] And this ,too

Level = loggin in the following sentence. INFO indicates to set the log record level to INFO. that is to say, only logs with a higher level than INFO or INFO will be recorded in the file. In this example, the first log will not be recorded. if you want to record the debug log, you can change the log level to DEBUG.

If you want to print logs on the screen and file logs at the same time, you need to understand a little complicated knowledge:

The logging library takes a modular approach and offers several categories of components: loggers, handlers, filters, and formatters.

Loggers expose the interface that application code directly uses.

Handlers send the log records (created by loggers) to the appropriate destination.

Filters provide a finer grained facility for determining which log records to output.

Formatters specify the layout of log records in the final output.

#! /Usr/bin/env python #-*-coding: UTF-8-*-#-Author-Lian import logging # create loggerlogger = logging. getLogger ("test_log") # Create a logger object. setLevel (logging. INFO) # global log level ch = logging. streamHandler () # print the log to the screen. setLevel (logging. DEBUG) # specify the ch log printing level. fh = logging. fileHandler ("access. log ") # store the log into the file fh. setLevel (logging. WARNING) # specify the input level of the fh log formatter = logging. formatter ("% (asctime) s-% (name) s-% (levelname) s-% (message) s") # define the log format, multiple logs can be written # add the log format to ch, fhch. setFormatter (formatter) fh. setFormatter (formatter) # add ch and fh to logger. addHandler (ch) logger. addHandler (fh) logger. debug ('debug message') logger.info ('info message') logger. warn ('warn message') logger. error ('error message') logger. critical ('Critical Message ')

The global log level is the bottom line of the entire program. to print a local log level, it cannot be lower than this level.

Screen printing information

2016-10-31 17:23:42,988 - test_log - INFO - info message2016-10-31 17:23:42,988 - test_log - WARNING - warn message2016-10-31 17:23:42,988 - test_log - ERROR - error message2016-10-31 17:23:42,988 - test_log - CRITICAL - critical message

Access. log:

2016-10-31 17:02:06,223 - test_log - WARNING - warn message2016-10-31 17:02:06,224 - test_log - ERROR - error message2016-10-31 17:02:06,224 - test_log - CRITICAL - critical message

All log formats:

Several important formats: % (lineno) d output the log code line, % (process) d output the log printing process ID, % (thread) d output the log printing thread ID

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.