Skip to main content

PyZerolog

This is yet another python logger inspired by logging module for Go programming language zerolog.

Why another logger?

Python has very powerful logging module which covers most of the needs. Though when in comes to JSON logging, it becomes hard to deal with: you need at to write the whole another formatter at the very least.

This logging module has JSON logging enabled by default. It also takes advantage of types of fields provided in the logger and anables user to write their own type formatter.

You can install it with

pip install pyzerolog

Getting started

It is as easy as:

from zlog import logger

logger.info().msg("Some logging message")

Which should print the following line into stdout:

{"level": "info", "message": "Some logging message", "timestamp": "2022-07-22T21:58:55.203468"}

Adding custom fields

If you want to print your own fields, you may very well do so:

logger.info().string("string_key", "value").msg("Message with string")

logger.info().int("integer_key", 123).msg("Message with int")

logger.info().string("some_key", "example").int("another_key", 5).msg("Message with int and string")

logger.info().bool("is_valid", True).string("login", "super_user").int("attempt", 8).msg("Some logging data about the user")

Which will print (line by line):

{"fields": {"string_key": "value"}, "level": "info", "message": "Message with string", "timestamp": "2022-07-22T21:58:56.929238"}
{"fields": {"integer_key": 123}, "level": "info", "message": "Message with int", "timestamp": "2022-07-22T21:58:56.930486"}
{"fields": {"another_key": 5, "some_key": "example"}, "level": "info", "message": "Message with int and string", "timestamp": "2022-07-22T21:58:56.930695"}
{"fields": {"attempt": 8, "is_valid": true, "login": "super_user"}, "level": "info", "message": "Some logging data about the user", "timestamp": "2022-07-22T21:58:56.930921"}

As you see, all of your fields would be stored under the fields key.

Making output more readable

Despite being very convinient when reading logs with machinery, raw JSON may not be as pretty when read by human. You can customize your logs with several different approaches:

Customize JSON itself

Under the hood initial logger uses special JSON formatter. It can be customized. For example:

logger.prettify = False
logger.info().msg("This message would be in one line.")
logger.prettify = True
logger.info().msg("And this would have some nice formatting.")

Which will print

{"level": "info", "message": "This message would be in one line.", "timestamp": "2022-07-22T21:59:00.839581"}
{
  "level": "info",
  "message": "And this would have some nice formatting.",
  "timestamp": "2022-07-22T21:59:00.840572"
}

Use special console formatter

If you don't plan to use your logs outside the terminal, you may use ConsoleFormatter which prints the log entry in human-readable format:

from zlog import logger, ConsoleFormatter

logger.formatter = ConsoleFormatter()
logger.info().string("android", "iphone").bool("error", False).msg("Convinient one-liner message")

Which results in pretty-looking

23:06 INF Convinient one-liner message android=iphone error=False

Release files for pyzerolog 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyzerolog 0.4.0
File Size Uploaded
pyzerolog-0.4.0.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyzerolog 0.4.0
File Interpreter ABI Platform
pyzerolog-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.2 kB

Release files / pyzerolog-0.4.0.tar.gz

Download URL pyzerolog-0.4.0.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1b027d074ed2d47da218e262706deb018f9c0bfd594ebebb1fcfaf6653ab6bd3
BLAKE2b-256 checksum
How to use checksums
b758097e1beaf708c6b9ddbcbb0be7a3095a6c6a23d2c5e4ab3f4790cc9d934c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release files / pyzerolog-0.4.0-py3-none-any.whl

Download URL pyzerolog-0.4.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8b8bf8b383f2159cd88c1bcef1f2ef6b3c5caa309ee812cf7bd800b69f83f5df
BLAKE2b-256 checksum
How to use checksums
faeb64ce52fa15045f5569f49d51abfcd56a469bcd4ba4b7f5a9c4ac6512242f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.3

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page