Skip to main content

l4py is a Python library that simplifies logging configuration with features like JSON formatting, file rotation, and dynamic log levels via environment variables. It integrates seamlessly with Django and leverages the Python standard logging module for easy customization.

Project description

l4py

Unit-Tests
Published Python Package

l4py is a Python library that simplifies logging configuration and enhances logging output with flexible formatting and output options. It offers an easy-to-use interface to configure both console and file logging with various customization features like JSON formatting, file rotation, and automatic log level handling. The library leverages the Python standard logging module and integrates seamlessly with Django's logging configuration.

Key Features:

  • File Logging: Automatically handles file logging with customizable file names, maximum size, and retention count.
  • JSON Support: Optionally format log messages in JSON for structured output, both in console and log files.
  • Django Integration: Simplifies Django logging configuration with a pre-built function to create a LOGGING dict compatible with Django's settings.
  • Customizable Logging Levels: l4py allows you to define log levels using environment variables, following the pattern L4PY_LOG_LEVEL_{logger_name} and L4PY_LOG_LEVEL_ROOT. This enables dynamic configuration of log levels without the need to modify the code.
  • Utility Functions: Includes utility functions for app name retrieval and platform-specific log file naming.

Installation

pip install l4py

or from Github:

git clone https://github.com/roymanigley/l4py.git
cd l4py
pip install -r requirements.txt
python setup.py install

Usage

All th values set in the builder and the environment variables are the default values, and they don't have to be set explicit

from l4py import LogConfigBuilder, get_logger, utils
import platform
import logging
import os

# Example of defining the loglevel using environment variables
os.environ.setdefault('L4PY_LOG_LEVEL_ROOT', 'INFO')
os.environ.setdefault('L4PY_LOG_LEVEL_module.class', 'INFO')

# Initializes the logging dict using `logging.config.dictConfig`
LogConfigBuilder()\
    .file(f'{utils.get_app_name()}-{platform.uname().node}.log')\
    .file_json(True)\
    .file_max_count(5)\
    .file_max_size_mb(5)\
    .console_json(False)\
    .init()

# returns a logger config dict
config_dict = LogConfigBuilder()\
    .file(f'{utils.get_app_name()}-{platform.uname().node}.log')\
    .file_json(True)\
    .file_max_count(5)\
    .file_max_size_mb(5)\
    .console_json(False)\
    .build_config_dict()

# Add this to you django `settings.py`
LOGGING = LogConfigBuilder()\
    .file(f'{utils.get_app_name()}-{platform.uname().node}.log')\
    .file_json(True)\
    .file_max_count(5)\
    .file_max_size_mb(5)\
    .console_json(False)\
    .build_config_dict_for_django(
        django_log_level=logging.INFO,
        show_sql=True
    )


logger = get_logger()

logger.debug('This is a DEBUG Message')
logger.info('This is a INFO Message')
logger.warning('This is a WARN Message')
logger.critical('This is a CRITICAL Message')
logger.fatal('This is a FATAL message')

With l4py, logging configuration becomes intuitive and consistent across different environments, making it a great choice for developers looking for a flexible and easy-to-integrate logging solution in Python applications.

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

l4py-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

l4py-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file l4py-0.1.0.tar.gz.

File metadata

  • Download URL: l4py-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for l4py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8aad7ebff634bb7c2cfbcf02c217be01c8faf5ba6eab5c35d8418ebc17fdb0aa
MD5 c8737363c60fd947fb7d995b5298a316
BLAKE2b-256 e570ba96fc6d28f18e372a2c630f065917901c761830b584a2b6858ab3718b98

See more details on using hashes here.

File details

Details for the file l4py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: l4py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.16

File hashes

Hashes for l4py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8debe9fb85c45031726a7d473456e1243a2f8374582dec6491af482418e29276
MD5 1affd9ee13a4333d85e88a2e4a661f67
BLAKE2b-256 6c95e513416483fbb90a23f2bfc755a10b828810424ea31ee0c20159c9f69592

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