Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use logging logs in python

Shulou Source: shulou.com Published: 2022-06-02 08:18:13 10月25日 Update

Today, I will talk to you about how to use logging logs in python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Create a new python file named py3_logging.py, and write the operation code in this file:

# logging record log file # set log level, and # set log format import logging#logging module is a standard library module of Python. # the key benefit of logging API provided by standard library module is that all Python modules can use this logging function. # so, your application log can integrate your own log information # with information from third-party modules. # Common log level: # DEBUG: detailed debugging information, which is usually meaningful only when diagnosing a problem. # INFO: confirm that the code works as expected # WARNING: indicates that something unexpected has occurred, or # or indicates that some problems have occurred in the near future (such as "insufficient disk space"). # the software is still working as expected. # ERROR: this code cannot perform some functions due to a serious error. # CRITICAL: a serious error indicates that the program itself may not be able to continue.

# then set the basic configuration of the log # filename: file name # level: log level # format: format of the log display # format format parameters reference: # https://docs.python.org/3/library/logging.html#logrecord-attributeslogging.basicConfig(filename='test.log', level=logging.DEBUG, format='% (asctime) SVR% (levelname) SVR% (message) s')

Def add (x, y): "addition function"return x + y"

Def subtract (x, y): "" subtraction function "" return x-y

Def multiply (x, y): "" multiplication function "" return x * y

Def divide (x, y): "" division operation function "" return x / y

Num_1 = 20num_2 = 10

Add_result = add (num_1, num_2) # logging logging.debug using logging.debug ('Add: {} + {} = {}' .format (num_1, num_2, add_result))

Sub_result = subtract (num_1, num_2) logging.debug ('Sub: {}-{} = {}' .format (num_1, num_2, sub_result))

Mul_result = multiply (num_1, num_2) logging.debug ('Mul: {} * {} = {}' .format (num_1, num_2, mul_result))

Div_result = divide (num_1, num_2) logging.debug ('Div: {} / {} = {}' .format (num_1, num_2, div_result))

The result of running is to generate a file test.log with the contents of the file:

After reading the above, do you have any further understanding of how to use logging logs in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Log file module content function operation code information format level run function standard error problem accident next multiplication key subtraction Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Docker NVidia MySQL