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.1.0.tar.gz (8.6 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.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: logging_by_oriash-0.1.0.tar.gz
  • Upload date:
  • Size: 8.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 bcd769ef1b7c63197f9acb7a8683b229494c2c670a04048c226efbeba48698dc
MD5 03f1040ba03ce165a12e2c5d6787a1f8
BLAKE2b-256 808e4ab1563bbf9dc6c5f7bf3085cda46434f30792bddbaf30b12384299acfa2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for logging_by_oriash-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87b1fae1ea77e3224e9d880192dd1f0b930911efeff643d44cedffbba48b4b46
MD5 2e2e7c04a479f858102d78eb52823b24
BLAKE2b-256 ab98d78a49869bf22ce834e48be466e68d81506b5c5556021ee8f7ddb09afdf8

See more details on using hashes here.

Provenance

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