Petty Petty Joy # 1: loguru

    Today we will discuss why someone needed to write a replacement for a standard pet logger loggingand how to use this thing.



    It’s sick!


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


    logging- A robust, stable solution tightly embedded in the Python ecosystem. You import it where necessary, make a couple of manipulations - and that’s all, it’s kind of like cherishing it logger.exception('што-то-там'). And the entry 'што-то-там'will fall into some kind of journal.


    After that, usually developers are interested in exactly where 'што-то-там'and what happens with this record. Programmers climb into the settings of the logger and, with the help of a bunch of different options, begin to teach it to properly push records into files, databases, error collectors, and other places where log entries can be stored.


    Configs for the logger appear, which begin 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 are rapidly evolving towards something much more indigestible. A normally configured config for logging, with different levels of logging, different message collectors and rotation of log files is a hefty piece of text that is already really difficult to dig into.


    To not hurt and even pleasant


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


    And here is a demo of this library, and it exhaustively shows how it is possible to configure logging in your powerful application in a couple of lines.



    Why is it worth considering loguruas an alternative logging?


    • Simplicity. I already said about this above, but I will say it again - loguruit’s easier to competently configure than logging.
    • Clear ways to configure log file rotation and archive old records.
    • Heaps of batteries are already included in the box - message colors, formatting, sending failure notifications to e-mail, function call stacks along with crash reports and many other nice amenities.
    • And, of course, asynchrony! Yes, the Python world is moving farther towards async / await, and this tectonic shift of all power-hungry programming clearly requires asynchronous loggers.

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


    • Lieba is relatively young and there may be surprises.
    • The authors promise full compatibility with logging, but you may well come across problems of docking loguruwith libraries from third parties for logging. For example, when hanging handlers for Sentry or Airbrake.

    Despite these potential difficulties, loguruit’s worth it to be thoroughly tested and involved in your next project.


    Also popular now: