Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the third-party logging framework loguru for Python

Shulou Source: shulou.com Published: 2022-06-01 11:38:40 10月28日 Update

This article mainly explains "Python how to use the third-party log framework loguru", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to use the third-party log framework loguru" bar!

Install pip install loguru

1. Output log

From loguru import loggerlogger.debug ("this is a debug log")

The colored log appears after the execution of the terminal, which is pretty cool.

2. Export to a file

From loguru import loggerlogger.add ("file_ {time} .log") logger.debug ("this is a debug log") logger.info ("this is an info log")

There is one more log file in the directory: file_2019-03-14019-53-25_661314.log

3. Log rules

Set log format, filter, log level

From loguru import loggerlogger.add ("file.log", format= "{time} {level} {message}", filter= "", level= "INFO") logger.debug ("this is a debug log") logger.info ("this is an info log")

Output

2019-03-14T20:01:25.392454+0800 INFO this is an info log

4. Log file

File management mode

Logger.add ("file_1.log", rotation= "500 MB") # A file that is too large will regenerate a file logger.add ("file_2.log", rotation= "12:00") # create a new file logger.add ("file_3.log", rotation= "1 week") every day at 12:00 # A new file logger.add ("file_X.log") will be created if the file takes too long Retention= "10 days") # empties logger.add ("file_Y.log", compression= "zip") # saves zip format after a period of time

5. Other parameters

Logger.add ("somefile.log", enqueue=True) # asynchronously write logger.add ("somefile.log", serialize=True) # serialize to json

6. Time formatting

Logger.add ("file.log", format= "{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}")

Cooperate with notifiers module

7. Create multiple file processor objects in the project and solve the problem of Chinese garbled code.

# coding=utf-8import osimport sysfrom loguru import loggerBASE_DIR = os.path.dirname (os.path.dirname (os.path.abspath (_ _ file__)) log_file_path = os.path.join (BASE_DIR, 'Log/my.log') err_log_file_path = os.path.join (BASE_DIR,' Log/err.log') logger.add (sys.stderr, format= "{time} {level} {message}", filter= "my_module" Level= "INFO") # logger.add (s) logger.add (log_file_path, rotation= "500MB", encoding='utf-8') # Automatically rotate too big filelogger.add (err_log_file_path, rotation= "500MB", encoding='utf-8', level='ERROR') # Automatically rotate too big filelogger.debug ("That's it, beautiful and simple logging!") logger.debug ("Chinese log may not be allowed") logger.error ("serious error") Thank you for your reading The above is the content of "how Python uses the third-party logging framework loguru". After the study of this article, I believe you have a deeper understanding of how Python uses the third-party logging framework loguru, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Logs files this is framework third party time format learning output content problems Chinese garbled creative parameters processors multiple objects that is engineering Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Huawei NVidia vpn Linux