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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcd769ef1b7c63197f9acb7a8683b229494c2c670a04048c226efbeba48698dc
|
|
| MD5 |
03f1040ba03ce165a12e2c5d6787a1f8
|
|
| BLAKE2b-256 |
808e4ab1563bbf9dc6c5f7bf3085cda46434f30792bddbaf30b12384299acfa2
|
Provenance
The following attestation bundles were made for logging_by_oriash-0.1.0.tar.gz:
Publisher:
publish.yml on oriash93/logging-utils
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
logging_by_oriash-0.1.0.tar.gz -
Subject digest:
bcd769ef1b7c63197f9acb7a8683b229494c2c670a04048c226efbeba48698dc - Sigstore transparency entry: 147344243
- Sigstore integration time:
-
Permalink:
oriash93/logging-utils@f424c105f86f9ac0af58a6bad70f57c4ac57b762 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/oriash93
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f424c105f86f9ac0af58a6bad70f57c4ac57b762 -
Trigger Event:
push
-
Statement type:
File details
Details for the file logging_by_oriash-0.1.0-py3-none-any.whl.
File metadata
- Download URL: logging_by_oriash-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87b1fae1ea77e3224e9d880192dd1f0b930911efeff643d44cedffbba48b4b46
|
|
| MD5 |
2e2e7c04a479f858102d78eb52823b24
|
|
| BLAKE2b-256 |
ab98d78a49869bf22ce834e48be466e68d81506b5c5556021ee8f7ddb09afdf8
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
logging_by_oriash-0.1.0-py3-none-any.whl -
Subject digest:
87b1fae1ea77e3224e9d880192dd1f0b930911efeff643d44cedffbba48b4b46 - Sigstore transparency entry: 147344248
- Sigstore integration time:
-
Permalink:
oriash93/logging-utils@f424c105f86f9ac0af58a6bad70f57c4ac57b762 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/oriash93
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f424c105f86f9ac0af58a6bad70f57c4ac57b762 -
Trigger Event:
push
-
Statement type: