Simplify Your Python Logging with Loguru

Posted by Afsal on 06-Oct-2023

Hi Pythonistas!

Today we will learn about a package called loguru, which makes the logging very simple and has options like serialize to json. Let us dive into the code

Installation

pip install loguru

Basic logging

from loguru import logger

logger.add("app.log", rotation="1 day", retention="7 days")

logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")

Output

This will create a log file app.log which rotates every day and have a backup of 7days

Formatting log

logger.add('fomated.log', format="{time:DD-MM-YYYY HH:mm:ss} | {level} | {message}")

logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")

 

Output

Here i have changed the datetime format but there is no color

Adding color logs

logger.add('color.log', format="<green>{time:DD-MM-YYYY HH:mm:ss}</green> | {level} | <cyan>{message}</cyan>", colorize=True)

logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")

Output

Serializing logs

logger.add('json.log', format="{time:DD-MM-YYYY HH:mm:ss} | {level} | {message}", serialize=True)

logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")

Output

In this case we will get more details and logs in json format

{"text": "06-10-2023 08:37:54 | DEBUG | This is a debug message.\n", "record": {"elapsed": {"repr": "0:00:00.013474", "seconds": 0.013474}, "exception": null, "extra": {}, "file": {"name": "loguru_experiment.py", "path": "/home/afsal/Desktop/experiments/module_intro/loguru_experiment.py"}, "function": "<module>", "level": {"icon": "????", "name": "DEBUG", "no": 10}, "line": 36, "message": "This is a debug message.", "module": "loguru_experiment", "name": "__main__", "process": {"id": 4407, "name": "MainProcess"}, "thread": {"id": 140706067496960, "name": "MainThread"}, "time": {"repr": "2023-10-06 08:37:54.893886+05:30", "timestamp": 1696561674.893886}}}
{"text": "06-10-2023 08:37:54 | INFO | This is an info message.\n", "record": {"elapsed": {"repr": "0:00:00.013902", "seconds": 0.013902}, "exception": null, "extra": {}, "file": {"name": "loguru_experiment.py", "path": "/home/afsal/Desktop/experiments/module_intro/loguru_experiment.py"}, "function": "<module>", "level": {"icon": "ℹ️", "name": "INFO", "no": 20}, "line": 37, "message": "This is an info message.", "module": "loguru_experiment", "name": "__main__", "process": {"id": 4407, "name": "MainProcess"}, "thread": {"id": 140706067496960, "name": "MainThread"}, "time": {"repr": "2023-10-06 08:37:54.894314+05:30", "timestamp": 1696561674.894314}}}
{"text": "06-10-2023 08:37:54 | WARNING | This is a warning message.\n", "record": {"elapsed": {"repr": "0:00:00.014182", "seconds": 0.014182}, "exception": null, "extra": {}, "file": {"name": "loguru_experiment.py", "path": "/home/afsal/Desktop/experiments/module_intro/loguru_experiment.py"}, "function": "<module>", "level": {"icon": "⚠️", "name": "WARNING", "no": 30}, "line": 38, "message": "This is a warning message.", "module": "loguru_experiment", "name": "__main__", "process": {"id": 4407, "name": "MainProcess"}, "thread": {"id": 140706067496960, "name": "MainThread"}, "time": {"repr": "2023-10-06 08:37:54.894594+05:30", "timestamp": 1696561674.894594}}}
{"text": "06-10-2023 08:37:54 | ERROR | This is an error message.\n", "record": {"elapsed": {"repr": "0:00:00.014443", "seconds": 0.014443}, "exception": null, "extra": {}, "file": {"name": "loguru_experiment.py", "path": "/home/afsal/Desktop/experiments/module_intro/loguru_experiment.py"}, "function": "<module>", "level": {"icon": "❌", "name": "ERROR", "no": 40}, "line": 39, "message": "This is an error message.", "module": "loguru_experiment", "name": "__main__", "process": {"id": 4407, "name": "MainProcess"}, "thread": {"id": 140706067496960, "name": "MainThread"}, "time": {"repr": "2023-10-06 08:37:54.894855+05:30", "timestamp": 1696561674.894855}}}
{"text": "06-10-2023 08:37:54 | CRITICAL | This is a critical message.\n", "record": {"elapsed": {"repr": "0:00:00.014706", "seconds": 0.014706}, "exception": null, "extra": {}, "file": {"name": "loguru_experiment.py", "path": "/home/afsal/Desktop/experiments/module_intro/loguru_experiment.py"}, "function": "<module>", "level": {"icon": "☠️", "name": "CRITICAL", "no": 50}, "line": 40, "message": "This is a critical message.", "module": "loguru_experiment", "name": "__main__", "process": {"id": 4407, "name": "MainProcess"}, "thread": {"id": 140706067496960, "name": "MainThread"}, "time": {"repr": "2023-10-06 08:37:54.895118+05:30", "timestamp": 1696561674.895118}}}
{"text": "06-10-2023 08:37:54 | DEBUG | This is a debug message.\n", "record": {"elapsed": {"repr": "0:00:00.022400", "seconds": 0.0224}, "exception": null, "extra": {"extra": {"a": 1, "b": 2}}, "file": {"name": "loguru_experiment.py", "path": "/home/afsal/Desktop/experiments/module_intro/loguru_experiment.py"}, "function": "<module>", "level": {"icon": "????", "name": "DEBUG", "no": 10}, "line": 50, "message": "This is a debug message.", "module": "loguru_experiment", "name": "__main__", "process": {"id": 4407, "name": "MainProcess"}, "thread": {"id": 140706067496960, "name": "MainThread"}, "time": {"repr": "2023-10-06 08:37:54.902812+05:30", "timestamp": 1696561674.902812}}}

Adding extra data to logs

logger.add('extra_data.log', format="{time:DD-MM-YYYY HH:mm:ss} | {level} | {message}", serialize=True)

data = { "a": 1, "b": 2}
logger.debug("This is a debug message.", extra=data)

Output

{"text": "06-10-2023 08:45:02 | DEBUG | This is a debug message.\n", "record": {"elapsed": {"repr": "0:00:00.009421", "seconds": 0.009421}, "exception": null, "extra": {"extra": {"a": 1, "b": 2}}, "file": {"name": "loguru_experiment.py", "path": "/home/afsal/Desktop/experiments/module_intro/loguru_experiment.py"}, "function": "<module>", "level": {"icon": "????", "name": "DEBUG", "no": 10}, "line": 50, "message": "This is a debug message.", "module": "loguru_experiment", "name": "__main__", "process": {"id": 4542, "name": "MainProcess"}, "thread": {"id": 139657421549568, "name": "MainThread"}, "time": {"repr": "2023-10-06 08:45:02.983313+05:30", "timestamp": 1696562102.983313}}}

The extra data passed will be on the key extra

I hope you have learned something from this post please share your valuable suggestions with afsal@parseltongue.co.in