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",
    enable_log_file=True,
    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.

  • enable_log_file (bool): Enables 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.2.0.tar.gz (8.4 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.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file logging_by_oriash-0.2.0.tar.gz.

File metadata

  • Download URL: logging_by_oriash-0.2.0.tar.gz
  • Upload date:
  • Size: 8.4 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.2.0.tar.gz
Algorithm Hash digest
SHA256 ddf4627e8333a8d4e8daafb0019d4d29bb880dd6bdc3d7bf2aeb02b6e8527566
MD5 691bda64419f7043d4beb3e6002c48ee
BLAKE2b-256 4e932e69d95b6c07f3273a54253b4d717d42ac468fa8c7ef7c98d5d3228dfeac

See more details on using hashes here.

Provenance

The following attestation bundles were made for logging_by_oriash-0.2.0.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.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for logging_by_oriash-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23c8ae2db616a8fee2d83203bf9630c56745b199f97ca8e5cb48b7746ab45516
MD5 35923f4fcb39a6b4ba6bcf3fd040350e
BLAKE2b-256 915977098667890b553dff8dd7bf1b6569243def8bc59d2cded34e8b04ca7177

See more details on using hashes here.

Provenance

The following attestation bundles were made for logging_by_oriash-0.2.0-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