Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use logging logs in python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report