📜 ⬆️ ⬇️

Small python joy # 1: loguru

Today we will discuss why someone needed to write a replacement for the standard logging logger and how to use this thing.



It's sick!


When it comes to logging in Python, logging comes to mind immediately.


logging is a robust, stable solution, tightly embedded in the Python ecosystem. You import it where it is necessary, you make a couple of manipulations - and that’s it, everything seems to be possible to write the cherished logger.exception('--') . And the entry '--' will fall into some kind of journal.


After that, developers are usually interested in where exactly the '--' and what happens next with this recording. Programmers climb into the logger settings and start using a heap of different options to teach it to properly stuff records into files, databases, error collectors, and other places where log files can be stored.


There are configs for the logger, which start with a cumbersome, but more or less understandable:


 LOGGER_CONFIG = { "version": 1, "disable_existing_loggers": False, "formatters": { "simple": { "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" } }, "handlers": { "console": { "class": "logging.StreamHandler", "level": "DEBUG", "formatter": "simple", "stream": "ext://sys.stdout" }, }, "root": { "level": "INFO", "handlers": ["console"] } } 

Such configs quickly evolve towards something much more indigestible. A normally configured config for logging , with different logging levels, different message collectors and rotation of log files is a hefty piece of text that is really hard to dig into.


In order not to hurt and even pleasant


Once a couple of programmers finally got into trouble digging (and making mistakes) in numerous logging tuning options. These engineers wrote their logger, extremely simple and at the same time very powerful. This thing is called Loguru .


And here is the demo of this library, and it exhaustively shows how it is possible in a couple of lines to understand and simply configure journaling in your python application.



Why consider loguru as an alternate logging ?



Of course, you have to pay for everything. And for the use of loguru will have to pay two things:



Despite these possible difficulties, loguru worth it to be carefully tested and use in your nearest project.


')

Source: https://habr.com/ru/post/454436/


All Articles