Easy-to-use context-rich Python logging library.
Project description
Easy-to-use context-rich Python logging library.
Full documentation: oribarilan.github.io/oplog.
Source code: github.com/oribarilan/oplog.
Installation
You can install oplog from PyPI using pip:
pip install op-log
What is oplog?
oplog is a modern logging library for Python application. oplog offers a different paradigm for logging, which is based on the concept of logging operations. Instead of creating a "log-book", which is a long scroll of text messages, oplog is about logging operations with rich data.
Please refer to our full documentation at oribarilan.github.io/oplog.
Key features
- Object Oriented: Intuitive API, easy to use and extend.
- Modern & Scalable: Unlike log messages, oplog is scaleable. Ingesting oplogs to a columnar database allows you to query, analyze and monitor your app in a modern and performant way.
- Standardized: No more mess and inconsistency across your logs. oplog creates a standard for how logs should be written across your code base. Clean code, clean logs.
- Production Ready: Easily create dashboards and monitors on top of logged data.
- Lightweight: oplog is a layer on top of the standard Python logging library. It is easy to integrate and use.
- Minimal: While oplog is rich with metadata, you only log what you need. Creating smaller and more efficient logs.
Getting Started
Setting up the logger
oplog naturally extends Python's built-in logger.
To start, create an OperationHandler
, and attach to it any logging handler of your choice. Additionally, you should customize your output log format with a formatter. You can create your own or use a built-in one (such as VerboseOpLogLineFormatter
).
import logging
from oplog import Operated, OperationHandler
from oplog.formatters import VerboseOplogLineFormatter
stream_op_handler = OperationHandler(
handler=logging.StreamHandler(), # <-- any logging handler
formatter=VerboseOplogLineFormatter(), # <-- custom formatter or built-in ones
)
logging.basicConfig(level=logging.INFO, handlers=[stream_op_handler])
# using a decorator, for simplicity
@Operated()
def foo():
pass
foo()
Output:
2023-08-31 17:31:08.519900 (0ms): [foo.foo / Success]
As you can see, you can use any handler, formatter and filter you want. Oplog does not interfere with them.
- Line 6 (highlighted) makes any handler an "Operation Handler". If you want to also handle log-book-style logs, you can keep your existing handler (for log message, like
logger.info("This is a conventional log message")
). - Line 7 (highlighted) decides on the log format. It is using a built-in formatter, but you can create your own formatter easily.
Using Context Managers
For more control, you can use the context manager syntax. This allows, for example, to add custom properties to the operation.
import logging
from oplog import Operation, OperationHandler
from oplog.formatters import VerboseOplogLineFormatter
stream_op_handler = OperationHandler(
handler=logging.StreamHandler(), # <-- any logging handler
formatter=VerboseOplogLineFormatter(), # <-- custom formatter or built-in ones
)
logging.basicConfig(level=logging.INFO, handlers=[stream_op_handler])
# using a context manager, for more control
def bar():
with Operation(name="my_operation") as op:
op.add("metric", 5)
pass
bar()
Output:
2023-08-31 17:41:09.088966 (0ms): [my_operation / Success] {'metric': 5}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file op-log-0.21.0.tar.gz
.
File metadata
- Download URL: op-log-0.21.0.tar.gz
- Upload date:
- Size: 1.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96004dbdeafb4cc169a88bd9d9650312894f3fce6690cd43599883b6dd280fdf |
|
MD5 | 6fabe4a3cd90d529ca65b99cbe5cdd85 |
|
BLAKE2b-256 | 1dfc7c33d5c3bf492aa38d116140bc15e5879e7145ace0145a4462ae1a03971c |
File details
Details for the file op_log-0.21.0-py3-none-any.whl
.
File metadata
- Download URL: op_log-0.21.0-py3-none-any.whl
- Upload date:
- Size: 20.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c555587427ca5f4cb64601483b5c7797d35679043d4a80aa8b6f7f81225ef13b |
|
MD5 | fa957079de0a9d07aa97dddc85e43878 |
|
BLAKE2b-256 | e2db2f6f7b3d15a9dc5a8ae4bba4dda62d9ecc478dbbe0414ea69693107573f3 |