Slides: https://daniel-at-github.github.io/lightning_loguru/

Install

pipenv shell
pipenv install loguru

Usage

from loguru import logger
logger.debug("Hello Python Vigo")
2018-12-20 15:13:28.412 | DEBUG    | __main__:<module>:3 - Hello Python Vigo

Custom formats

You could replace de default sink with a custom format

from loguru import logger
import sys
logger.remove()
logger.add(sys.stderr,
           format="{time} {level} {message}",
           filter=None,
           level="DEBUG")
logger.success("Another example deployed")
2018-12-20T15:50:56.536470+0100 SUCCESS Another example deployed

Levels

from loguru import logger
import sys
logger.remove()
logger.add(sys.stderr, level="TRACE",
           format='<level>{level: <8}</level> '
                  '=> <level>{message}</level>')
logger.trace("log data")
logger.debug("log data")
logger.info("log data")
logger.success("log data")
logger.warning("log data")
logger.error("log data")
logger.critical("log data")
TRACE    => log data
DEBUG    => log data
INFO     => log data
SUCCESS  => log data
WARNING  => log data
ERROR    => log data
CRITICAL => log data

File logging

# File timestamp
logger.add("file_{time}.log")
# Rotation by size, once a day or at X days
logger.add("file_1.log", rotation="500 MB")    # too big
logger.add("file_2.log", rotation="12:00")     # at noon
logger.add("file_3.log", rotation="1 week")    # after a week
# Cleanup by time
logger.add("file_X.log", retention="10 days")
# Compress to save space
logger.add("file_Y.log", compression="zip")

String formatting using braces

logger.success("If you're using Python {}, better with {feature}",
               3.6,
               feature="f-strings")
SUCCESS  => If you're using Python 3.6, better f-strings

Asynchronous logs

Asynchronous, Thread-safe, Multiprocess-safe

logger.add("somefile.log", enqueue=True)

Exceptions

from loguru import logger

@logger.catch
def my_function(x, y, z):
    # An error? It's catched anyway!
    return 1 / (x + y - z)

my_function(1, 2, 3)

Exceptions

2018-12-20 16:38:37.569 | ERROR    | __main__:<module>:9 - An error has been caught in function '<module>', process 'MainProcess' (8465), thread 'MainThread' (140530453767936):
Traceback (most recent call last):

> File "example.py", line 9, in <module>
    my_function(1, 2, 3)
    <function my_function at 0x7fcfc9725268>

  File "example.py", line 7, in my_function
    return 1 / (x + y - z)
                │   │   └ 3
                │   └ 2
    1

ZeroDivisionError: division by zero

Structured logging

from loguru import logger
logger.add("log.json",
           serialize=True,
           level="DEBUG")
logger.success("Mechanized!!")
{"record": {"message": "Mechanized!!", "line": 5, "name": "__main__", "level": {"icon": "\u2714\ufe0f", "name": "SUCCESS", "no": 25}, "function": "<module>", "file": {"path": "example.py", "name": "example.py"}, "module": "example", "elapsed": {"repr": "0:00:00.001335", "seconds": 0.001335}, "time": {"repr": "2018-12-20 17:49:29.347426+01:00", "timestamp": 1545324569.347426}, "extra": {}, "thread": {"name": "MainThread", "id": 140491034552064}, "exception": null, "process": {"name": "MainProcess", "id": 9793}}, "text": "2018-12-20 17:49:29.347 | SUCCESS  | __main__:<module>:5 - Mechanized!!\n"}

Structured logging

{
  "record": {
    "message": "Mechanized!!",
    "line": 5,
    "name": "__main__",
    "level": {
      "icon": "\u2714\ufe0f️",
      "name": "SUCCESS",
      "no": 25
    },
    "function": "<module>",
    "file": {
      "path": "example.py",
      "name": "example.py"
    },
    "module": "example",
    "elapsed": {
      "repr": "0:00:00.001335",
      "seconds": 0.001335
    },
    "time": {
      "repr": "2018-12-20 17:49:29.347426+01:00",
      "timestamp": 1545324569.347426
    },
    "extra": {},
    "thread": {
      "name": "MainThread",
      "id": 140491034552064
    },
    "exception": null,
    "process": {
      "name": "MainProcess",
      "id": 9793
    }
  },
  "text": "2018-12-20 17:49:29.347 | SUCCESS  | __main__:<module>:5 - Mechanized!!\n"
}

and much more

  • Lazy evaluation of expensive functions
  • Customizable levels
  • Better datetime handling
  • Suitable for scripts and libraries
  • Entirely compatible with standard logging
  • Personalizable defaults through environment variables
  • Convenient parser
  • Exhaustive notifier

Credits

Python packages
loguru
ansi2html - capturing terminal log colors
markdownreveal - slides
Github
https://daniel-at-github.github.io/lightning_loguru/