Written for many years python Logs are built-in logging Module , You need to go through cumbersome configuration when using . occasionally , There may also be log loss , until loguru Appearance .
【 Read the whole passage 】
loguru It's really very simple , After installation, import directly to python Code block can be used directly .
1、 Installation environment
We still follow pip The way to install , The mirror station of Tsinghua University is used by default .
pip install loguru -i https://pypi.tuna.tsinghua.edu.cn/simple
After installation, you can start to use directly .
2、 General log printing
When printing the regular log on the console, you can use it directly without adding any configuration , After using, we can verify the printing effect .
from loguru import logger
# Use debug Level print a line of log
logger.debug('I am debug message.')
Here I am pycharm The log effect picture taken down above .
We can see that the effect is still satisfactory , The color matching is also quite nice, And the log time is accurate to seconds . The log information you want to see basically includes .
3、 Write log file
It is also relatively simple to write log files without configuration , Just call add Function to add files .
# Add log file
logger.add('./message-info.log')
# Print a row debug journal
logger.debug('I am debug message.')
here , Not only will a line of logs appear on the console , And in message-info.log A message is also written in the file dubug Log information .
If you want to specify your own output format when writing files , You can go directly to add Function can be set directly , At the same time, you can set the level of output to the log file .
# In a specific format info Level logs are written to the log file
log = logger.add('./info.log',format='{time} | {level} | {message}',level='INFO')
# At this time , Print more info Level of logging
logger.info('I am info message <1>.')
logger.info('I am info message <2>.')
logger.info('I am info message <3>.')
logger.info('I am info message <4>.')
logger.info('I am info message <5>.')
The following is the pycharm Log effect printed by console
The following is the display effect in the log file , It is the same as the log format we set .
4、 Stop writing log files
In the above process, we add the log information in the custom log format to info.log When in the file, a log Variable .
This log In fact, a log file is returned id, When you need to stop writing logs to the log file , Just call remove Function will log the file id Pass it as a parameter to stop writing logs to the log file .
# Stop writing logs to the log file
logger.remove(log)
here , hinder The log file will no longer be written to the log file .
5、 Scroll through log files
In practice , We don't just have a log file . When a log file exceeds a certain size 、 Or in a specific time period, you can generate a new log file by setting parameters to continue logging .
# exceed 10M Generate a new log file
logger.add('info.log',rotation='10 MB')
# A new log file is generated at 1 a.m. every day
logger.add('info.log',rotation='01:00')
also , In order to prevent log files from occupying disk after long-term recording , Generally, you can also set 30 Clean up log files every day , This solves the problem that the disk is full .
# Set the retention time of this log file to 30 God
logger.add('info.log',retention='30 days')
Usually, if the log file is stored in the original way , So it takes up a lot of space .loguru For us, too , It is to compress the log file by specifying the file compression format .
logger.add('info.log',compression='zip')
6、 Exception log capture
In practice coding In the process , Especially the production environment , It is particularly uncomfortable if some abnormal information is not written to the log file . In the production environment , Logging is the only way to solve the problem .
loguru It provides an annotation method to write the function information with exceptions directly to the log file , such as , An exception occurred during the operation of such a function .
@logger.catch
def func(param):
return 10 / param
func(0)
The denominator of the above function is 0 It is certain that exceptions will be thrown when , We use annotations @logger.catch Exception information will be written .
meanwhile , Detailed exception information will also be recorded in the log file .
【 Past highlights 】
Can you do it if you don't learn English well coder, Stop worrying about it and learn it first ...
Data cleaning tools flashtext, The efficiency has been directly increased by dozens of times !
One help Function solves python View all document information for ...
python Custom exception /raise Keyword throws an exception
python Local music player production process ( Complete source code attached )