Package: op-log
Easy-to-use context-rich Python logging library.Full documentation:oribarilan.github.io/oplog.Source code:github.com/oribarilan/oplog.InstallationYou can install oplog from PyPI using pip:pipinstallop-logWhat 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 atoribarilan.github.io/oplog.Key featuresObject 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 StartedSetting up the loggeroplog naturally extends Python's built-in logger.
To start, create anOperationHandler, 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 asVerboseOpLogLineFormatter).importloggingfromoplogimportOperated,OperationHandlerfromoplog.formattersimportVerboseOplogLineFormatterstream_op_handler=OperationHandler(handler=logging.StreamHandler(),# <-- any logging handlerformatter=VerboseOplogLineFormatter(),# <-- custom formatter or built-in ones)logging.basicConfig(level=logging.INFO,handlers=[stream_op_handler])# using a decorator, for simplicity@Operated()deffoo():passfoo()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, likelogger.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 ManagersFor more control, you can use the context manager syntax. This allows, for example, to add custom properties to the operation.importloggingfromoplogimportOperation,OperationHandlerfromoplog.formattersimportVerboseOplogLineFormatterstream_op_handler=OperationHandler(handler=logging.StreamHandler(),# <-- any logging handlerformatter=VerboseOplogLineFormatter(),# <-- custom formatter or built-in ones)logging.basicConfig(level=logging.INFO,handlers=[stream_op_handler])# using a context manager, for more controldefbar():withOperation(name="my_operation")asop:op.add("metric",5)passbar()Output:2023-08-31 17:41:09.088966 (0ms): [my_operation / Success] {'metric': 5}