Loguru: (Stupidly) simplest logger for your python projects
Intro
I am a lazy man.
Time is fair for everyone, then it is possible to spend an amount of time on minor stuff.
Logging is a minor thing for any programming project.
Coding for logging is not a value-added activity, but we have to do it.
Think about the below scenarios:
- Generally, medium or large-size programming projects must have a logging function. Many data analysts or data scientist assume that log is only useful for professional developers.
- Do we need logs if we just write simple python scripts? Why not! Especially if the code has a while-for-loop. We never know what unexpected exceptions may occur when the scripts go live. It is better to record exceptions in logs and later fix them.
- Do Data Scientists and ML engineers need this? Yes. We can track the experiment results and parameters, which can then be compared and analyzed.
Is Python’s built-in logging module sufficient?
Of course, but for professional developers
Getting started with the built-in logging module is quite straightforward, but going to the advanced level will be time-consuming. To adapt the code to fit your project, you have to fine-tune the format and set a lot of parameters. For example, the entry code is this:
import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
But you will realize that output of this piece of code is just like “print” function. Even there is no timestamp at all.
Use Loguru
Loguru is best the option for ML engineers or Data Scientists. Loguru is designed for logging, and that is all.
The official introduction of Loguru has two keywords: (Stupidly) simple and enjoyable.
Pros
- It is out of the box but powerful. It acts as a professional level when compared to built-in logging module.
- Set Handler, Formatter, Filter with simple add function.
- Even if you do not know what roles Handler Formatter and Filter are playing in a logging module, never mind, with Loguru you don’t need to know.
- There are classic visualization for a specific type of log. e.g.Debug is displayed in yellow color, info is in blue, and warn is in red. It is surprised that built-in logging doesn’t consider such basic requirement. Loguru does still out of the box yet professional.
- Timestamp format is logged automatically for you
- Other details like tracing the lines of code
- If you are the professional developer, still, Loguru is for you as well. As you can still configure it to suit a variety of needs
- Do you want to control the log file’ size, e.g. less than 100MB?
- Or set the scheduler to recreate a logger every morning? and archive the older ones
- Maybe set the retention max period as 10 days, if it is older than 10 days, then delete it
- Zip the log files to save space
I bet you have at least one of the above requirements.
All in all, Loguru make your code from Zero to Hero in one line of code.
Cons
(Five minutes for me to think hard..)
Maybe it will make you addict to logging and try to log everything.
Time to get hands dirty
Ready?
from loguru import logger
logger.add("file.log")
logger.debug("That's it, beautiful and simple logging!")
Done!
Take a look at of above code, these 3 lines shall meet basic logging requirements of small programs.
Want to get a higher level? Just pick one line of code below.
logger.add("file_1.log", rotation="500 MB") # Automatically rotate too big file
logger.add("file_2.log", rotation="12:00") # New file is created each day at noon
logger.add("file_3.log", rotation="1 week") # Once the file is too old, it's rotated
logger.add("file_X.log", retention="10 days") # Cleanup after some time
logger.add("file_Y.log", compression="zip") # Save some loved space
Still want more?
At a higher level, here are the requirements from real project:
- Asynchronous, thread-safe, multi-process safe . loguru also supports multi-threaded processing
logger.add("somefile.log", enqueue=True)
- Collaborate between team members
In a complex projects, developer team and data science team shall work together. data science team is allowed to simply start recording logs enjoyable with one piece of code below without considering any configuration.
from loguru import logger
Before you go
Key in bebow to enjoy the logging.
pip install loguru