This is the 15th day of my participation in Gwen Challenge

In project development, debugging is essential, and the logging module provides great convenience for debugging.

Simple configuration of the Logging module

The log level

Logging provides five logging levels at which messages can be logged using different logging functions

level The log function describe
DEBUG logging.debug() Lowest level. Used for small details. Usually you only care about these messages when you are diagnosing a problem
INFO logging.info() Used to record general time information in a program or to confirm that everything is working properly
WARNING logging.warning() Used to indicate a possible problem that does not prevent the program from working, but might in the future
ERROR logging.error() Used to log errors that cause a program to fail to do something
CRITICAL logging.critical() Highest level. Used to indicate a fatal error that causes or will cause a program to stop working completely

Logging initialization

import logging
logging.basicConfig()
Copy the code

Create a Logging instance object

logger = logging.getLogger()
Copy the code

Set the level

logger.setLevel(logging.DEBUG)
Copy the code

Specifies the handler for the Logger instance object

When the log file reaches the specified size, the original log file is cleared. For example, log.1 is set to 1M, and log

from logging import handlers
Create a handler object that specifies the location and size of the log file
handler = handlers.RotatingFileHandler("logs/log", maxBytes=1024*50, backupCount=5)
# specify the log format of the handler object
handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(lineno)s - %(message)s"))
Copy the code

Logger instance object adds handler

logger.addHandler(handler)
Copy the code

The complete code

import logging
from logging import handlers
# Logging initialization
logging.basicConfig()
Create logger instance object
logger = logging.getLogger()
Set logger object debug level
logger.setLevel(logging.DEBUG)
Create handler object for logger, specify log file location, size, number of logs
handler = handlers.RotatingFileHandler("logs/log", maxBytes=1024*50, backupCount=5)
# specify the log format of the handler object
handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(lineno)s - %(message)s"))
Bind handler to logger object
logger.addHandler(handler)
Copy the code

Logger Log usage

We can now write the required log information to the log file by calling the functions provided by logging

n = 0
try:
    print(10 / n)
except Exception as e:
    print('e:',e)
    logging.error(e)
logger.error("Error occurred")
Copy the code

Viewing log Files

2018-08-02 13:54:30, 576-root-error-24 - Division by zero 2018-08-02 13:54:30, 576-root-error-25 - ERROR occurredCopy the code

The log is disabled

Disable (logging.logging_level) is used wherever logging is to be disabled. The loggLE_level function for logging will be disabled. The disabled level must correspond to the level you wrote in your code

When not disabled

n = 0
    try:
        print(10 / n)
    except Exception as e:
        print('e:',e)
        logging.error(e)
logger.error("Error 1 occurred")
# logging.disable(logging.ERROR)
logger.error("Error 2 occurred")
Copy the code
-- -- -- -- -- -- -- -- -- -- -- -- -- -- to view the log file -- -- -- -- -- -- -- -- -- -- - the 2018-08-02 15:44:35, 24-524 - root - ERROR - division by zero 15:44:35, 2018-08-02-524 2018-08-02 15:44:35,524 - root-error-27 - ERROR 2 occurredCopy the code

After the disabled

n = 0
    try:
        print(10 / n)
    except Exception as e:
        print('e:',e)
        logging.error(e)
logger.error("Error 1 occurred")
logging.disable(logging.ERROR)
logger.error("Error 2 occurred")
Copy the code
-- -- -- -- -- -- -- -- -- -- -- -- -- -- to view the log file -- -- -- -- -- -- -- -- -- -- - the 2018-08-02 15:48:09, 24-237 - root - ERROR - division by zero 15:48:09, 2018-08-02-237 Root-error-25 - An ERROR occurredCopy the code