A simple logger usable as decorator
Project description
DECOLOG
A lightweight Python logging decorator that automatically logs function calls, arguments, execution time, and exceptions. Provides both console and file logging with configurable verbosity.
Installation
pip install decolog
Quick Start
Basic Usage
from decolog import Logger
import logging
# Initialize logger
logger = Logger(
app_name="my_app",
dir_path="./logs",
log_level=logging.DEBUG,
console_handler_level=logging.DEBUG,
file_handler_level=logging.DEBUG
)
# Decorate functions - logs calls automatically
@logger
def add_numbers(a, b):
return a + b
@logger
def greet(name):
return f"Hello, {name}!"
# Function calls are automatically logged
result = add_numbers(5, 3) # Logs: function name, args, execution time
message = greet("World") # Logs: function name, args, execution time
Per-Function Verbosity Control
Control whether function arguments are truncated in logs:
# Default: arguments are truncated (verbose=False)
@logger
def function1(x, y, z):
return x + y + z
# Explicit verbose=False (same as default)
@logger(is_verbose=False)
def function2(x, y, z):
return x + y + z
# Full arguments shown (verbose=True)
@logger(is_verbose=True)
def function3(x, y, z):
return x + y + z
Features
- Automatic function logging: Logs function name, arguments, execution time
- Exception logging: Automatically logs exceptions with traceback
- Dual output: Logs to both console and file
- Per-function verbosity: Control argument display per function
- Configurable log levels: Set different levels for console and file output
- Daily log rotation: Creates new log files daily
Parameters
Logger Initialization
Logger(
app_name: str, # Application name (used in log files)
dir_path: str | Path, # Directory for log files
log_level: int = logging.DEBUG, # Overall log level
console_handler_level: int = logging.DEBUG, # Console log level
file_handler_level: int = logging.DEBUG # File log level
)
Decorator Usage
@logger(is_verbose=False) # Truncate function arguments in logs (default)
@logger(is_verbose=True) # Show full function arguments in logs
Log Format
Logs follow this format:
YYYY-MM-DD HH:MM:SS|app_name|LOG_LEVEL:message
Example log entry:
2023-11-15 14:30:45|my_app|DEBUG:add_numbers(*(5, 3), **{})
2023-11-15 14:30:45|my_app|DEBUG:executed add_numbers(*(5, 3), **{}) in 0.0001 seconds
Advanced Example
from decolog import Logger
import logging
# Configure logger
logger = Logger(
app_name="data_processor",
dir_path="/var/log/myapp",
console_handler_level=logging.INFO, # Less verbose console output
file_handler_level=logging.DEBUG # Detailed file logs
)
# Decorate multiple functions with different verbosity
@logger
def load_data(file_path):
# ... implementation ...
pass
@logger(is_verbose=True)
def process_data(data, config):
# ... implementation ...
pass
@logger(is_verbose=False)
def save_results(results, output_path):
# ... implementation ...
pass
# All function calls are automatically logged
load_data("input.csv")
process_data(data, config={"param": "value"})
save_results(results, "output.json")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
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 decolog-0.1.0-py3-none-any.whl.
File metadata
- Download URL: decolog-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7d5dc8332e630aafdcbcc98c73c9077ac163349acffef2ad008a4440830fa39
|
|
| MD5 |
7370bb2b301d9a0eab85e7385d6fb289
|
|
| BLAKE2b-256 |
c93984cacd755cbe2908404a66e67e13af427249293fc86d53961ebac8410c81
|