Simple loguru_logger_lite built on top of loguru to make a quick setup for basic logging
Project description
loguru_logger_lite
This simple logger is built on top of loguru to make a quick setup for basic logging.
The logger has four transports (called "sinks" here, same as in loguru):
- stdout
- stderr
- file
- kafka
The standard sinks (stdout, stderr and file) are configured with the same arguments as in loguru (see "add" method in loguru documentation).
Installation
pip install loguru-logger-lite
Usage
get_default_logger() method
The basic logger can be created using get_default_logger method. The get_default_logger method returns preconfigured loguru logger with stdout and stderr sinks. All messages whose level is less than ERROR are sent to stdout while messages whose level is ERROR or above are sent to stderr.
from loguru_logger_lite import Logger
logger = Logger.get_default_logger()
logger.info('Test log message')
The output will be like this:
MODULE: example | COMPONENT: __main__ | PID: 230552 | INFO | 2022-04-08T19:59:36.290220-0400 | Test log message
NOTE: The above log message format will also be a default format if the format option is not provided in sink configuration.
get_logger(sinks) method
The get_logger method is used to create a logger with a custom set of sinks. This method expects a list of Sink objects and returns a loguru logger with the configured sinks.
from loguru_logger_lite import Logger, LogLevels, Sink, Sinks, \
KafkaSinkOptions, FileSinkOptions
logger = Logger.get_logger([
Sink(name=Sinks.FILE,
opts=FileSinkOptions(path='test.log', level=LogLevels.DEBUG)),
Sink(name=Sinks.KAFKA,
opts=KafkaSinkOptions(level=LogLevels.TRACE,
bootstrap_servers=['localhost:9092'],
sink_topic='log_topic'))
])
logger.info('Test log message')
NOTE: When using file and kafka sinks, the output will be a json with text and record root items. The text item is formatted log message and record is loguru Record dictionary (see loguru documentation).
Kafka sink
The kafka sink, along with the basic loguru options, has additional parameters for kafka producer:
- bootstrap_servers - a list of strings of kafka brokers addresses.
- producer_config - kafka producer configuration (see kafka-python documentation).
- sink_topic - a kafka topic name where to send log messages.
get_kafka_sink(options) method
The kafka sink can be used standalone with existing loguru logger:
from loguru import logger
from loguru_logger_lite import Logger, LogLevels, KafkaSinkOptions
kafka_sink = Logger.get_kafka_sink(options=KafkaSinkOptions(
level=LogLevels.TRACE,
bootstrap_servers=['localhost:9092'],
sink_topic='log_topic')
)
logger.add(kafka_sink)
logger.info('Test log message')
LICENSE
MIT
AUTHOR: Dmitry Amanov
Project details
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 loguru_logger_lite-0.0.6.tar.gz
.
File metadata
- Download URL: loguru_logger_lite-0.0.6.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b55617fafedbfe85ef07972a26729928599a348fe494652bf446416b3971777 |
|
MD5 | fefb44d10089defa0d47607b9a91213c |
|
BLAKE2b-256 | 352b7f303f11d6262813d1075679b6eb0224ae6376c38812fda0ae04d4733c53 |
File details
Details for the file loguru_logger_lite-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: loguru_logger_lite-0.0.6-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67a415f72e240581db81512cb344d8b186b2ab0dbd87edd682038a00bd07dc9d |
|
MD5 | cadc94eec056e3edf2975215558da0b4 |
|
BLAKE2b-256 | 1a92ac76f0a9851bace0b356ff279e22033fd65c475a7f43c4aca5fc2f7dd048 |