Skip to main content

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

loguru_logger_lite-0.0.9.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

loguru_logger_lite-0.0.9-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file loguru_logger_lite-0.0.9.tar.gz.

File metadata

  • Download URL: loguru_logger_lite-0.0.9.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for loguru_logger_lite-0.0.9.tar.gz
Algorithm Hash digest
SHA256 adae992618d0c6d8a369042bbd5dd22b70f30f9de953e7174dff6fa2cdf7c0e8
MD5 c94cc6beb153fa6cc1199afec35bc8c0
BLAKE2b-256 57c3b06090b5c7366048aac15700a8f277e4805351be66057ce2286ec198a211

See more details on using hashes here.

File details

Details for the file loguru_logger_lite-0.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for loguru_logger_lite-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 b1a3a978ae59b02ed1d5003d6f491774ef817cdb213de20379783beef563491e
MD5 01ca40f2a3ffcda40fd31578b263b9f2
BLAKE2b-256 7b64fdbefe8d0013e1882f63168474c651c327cf5526b933446dfd04967a6891

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page