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 log decorator in Python

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

Share

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

It is believed that many inexperienced people have no idea about how to use log decorator in Python. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Crude version

1) the function is passed into the decorator as an argument.

2) A function is defined in the decorator to handle the function passed in as an argument.

3) return the functions defined in this decorator

Give it a try:

The effect is as follows:

Although this can achieve the functions we need, there is actually a lot of room for optimization.

two。 Ordinary version

An obvious problem with the first version of the code is that the handlers defined in the decorator do not support kwargs, while supporting kwargs in the decorator is just an easy task.

The second problem is that the timestamp is generated in the form of string interception, which is too rough. You can actually use strftime for string conversion.

The modifications are as follows:

It seems that the optimization is almost done, but there is still room for improvement.

3. Optimized version

In the first two versions of the code, we used print for log output, but this way of handling logs is not standard.

Using the logging module to control log output is a better choice.

In order to log using the logging module, we need to configure logging-related options first.

1) first, generate a logger and configure the log level:

2) configure the log format and add handler control output stream:

You can set the log level that handler needs to process. If it is not set, the Level of logger itself, that is, DEBUG level, is used by default.

3) finally, add this handler to the logger:

The complete configuration of logging is as follows:

When using it, it is very simple to replace print with logger.debug:

The effect is as follows:

In this way, a relatively perfect log decorator is completed.

Common log level configuration is attached:

After reading the above, have you mastered how to use the log decorator in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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