Skip to main content

Custom Python logging library

Project description

logging-utils

A simple, flexible logging utility for Python that enables easy configuration of both console and file logging. This package supports custom log levels, file rotation, and timezone-aware timestamps for streamlined logging in various environments.

Features

  • Configurable logging for both console and file outputs

  • Rotating log files based on file size

  • Automatic timezone handling based on the TZ environment variable

  • Fully customizable log levels and formats

Installation

pip install logging-by-oriash

Usage

To start logging with custom settings, import and use the setup_logger function.

Basic Example

from logging_utils import setup_logger

logger = setup_logger(__name__)  # or use any other name
logger.info("This is an info message.")
logger.error("This is an error message.")

Output:

2024-11-07 12:34:56,789 - __main__ - INFO [example.py:10] - This is an info message.
2024-11-07 12:34:56,790 - __main__ - ERROR [example.py:11] - This is an error message.

Advanced Configuration

The setup_logger function allows you to customize log levels, formats, and file-based logging options.

import logging

from logging_utils import setup_logger, LogFileConfig

logger = setup_logger(
    name=__name__,
    log_level=logging.DEBUG,
    log_format="%(asctime)s - %(levelname)s - %(message)s",
    disable_log_file=False,
    log_file_config=LogFileConfig(
        log_file_level=logging.WARNING,
        log_filename="custom.log",
        max_bytes=100_000,
        backup_count=3
    )
)

logger.debug("This is a debug message.")
logger.warning("This is a warning message.")

Output:

2024-11-07 12:34:56,789 - DEBUG - This is a debug message.
2024-11-07 12:34:56,790 - WARNING - This is a warning message.

Configuration Options

setup_logger Parameters

  • name (str): Name of the logger instance.

  • log_level (int | str): Log level for the logger. Defaults to the LOG_LEVEL environment variable or logging.INFO.

  • log_format (str): Format for log messages. Defaults to DEFAULT_LOG_FORMAT.

  • disable_log_file (bool): Disables file logging if set to True. Defaults to False.

  • log_file_config (LogFileConfig): Configuration object for file logging settings.

LogFileConfig Options

LogFileConfig is a data class used to specify file-based logging settings:

  • log_file_level (int | str | None): Log level for file logging. Defaults to the logger’s level if not set.

  • log_filename (str): Path to the log file. Defaults to "app.log".

  • max_bytes (int): Max file size in bytes before rotating. Defaults to 1 MiB.

  • backup_count (int): Number of backup files to keep after rotation. Defaults to 5.

Environment Variables

  • LOG_LEVEL: Sets the default log level for the logger.

  • TZ: Specifies the timezone for timestamps in logs (e.g., "Asia/Tokyo").

Example with Environment Variables

You can also configure the logger using environment variables. For example:

export LOG_LEVEL=DEBUG
export TZ=Asia/Tokyo

This configuration sets the log level to logging.DEBUG and formats timestamps in the Tokyo timezone.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Contributing

Contributions, issues, and feature requests are welcome! Feel free to check out the issues page or submit a pull request.

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

logging_by_oriash-0.0.1rc0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

logging_by_oriash-0.0.1rc0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file logging_by_oriash-0.0.1rc0.tar.gz.

File metadata

  • Download URL: logging_by_oriash-0.0.1rc0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for logging_by_oriash-0.0.1rc0.tar.gz
Algorithm Hash digest
SHA256 ffdef4afb73256ef8b5f60478b86089087ee9d596667d41fcdc39c87343b00d1
MD5 b944ca7025a5e7e11055a810aa3dc5b8
BLAKE2b-256 63ba69d0da8c901f3c378cc5457cbc927619aff4f1e387e324ab25cb9d5ff32a

See more details on using hashes here.

Provenance

The following attestation bundles were made for logging_by_oriash-0.0.1rc0.tar.gz:

Publisher: publish.yml on oriash93/logging-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file logging_by_oriash-0.0.1rc0-py3-none-any.whl.

File metadata

File hashes

Hashes for logging_by_oriash-0.0.1rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 a703f840786262262c8df7f6ddd474539d5de1ebb3e211ac796fd2d8bbe94f26
MD5 50bfaa6f910020cf6deec9d1ee3937a8
BLAKE2b-256 3e2818728a56c73616e825ec9c58a3e346ce9a748fee0d47b908f30628b78f6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for logging_by_oriash-0.0.1rc0-py3-none-any.whl:

Publisher: publish.yml on oriash93/logging-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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