Comprehensive logging utility with colored output, user context tracking, and automatic truncation.
Project description
Enhanced Logger Loggio
A comprehensive logging utility for project that provides:
- Timestamp information in logs
- User ID tracking for authenticated requests
- File name and line number of the log call
- Clean and consistent log formatting
- Multiple usage patterns for different scenarios
Features
- Contextual Information: Every log includes timestamp, log level, file name, and line number
- Authentication Awareness: Can include user ID in logs when available
- Format String Support: Supports Python's standard string formatting with %s, %d, etc.
- Flexible Configuration: Configurable output destination (terminal and/or file)
- Consistent API: Familiar logging methods (debug, info, warning, error, critical)
Installation
From PyPI (Recommended)
pip install loggio
Then import in your code:
from loggio import get_logger
From Source
To use standalone, use as a fork.
To get updates, use as a submodule.
As standalone
Go to inside your project.
Clone the repository:
git clone https://github.com/xcollantes/loggio loggio
As submodule to existing repo
Go to where you want to dependency.
Clone the repository as submodule:
loggio must be maintained since Python import statements don't work
work hyphens.
git submodule add https://github.com/xcollantes/loggio loggio
Cloning your project that contains submodules
Clone the repository with submodules:
git clone --recursive https://github.com/xcollantes/my-project-with-submodules
Remember --recursive for submodules.
If you clone without --recursive:
git submodule update --init --recursive
Create and Activate Virtual Environment
# Create virtual environment.
python3 -m venv env
# Activate virtual environment.
# On macOS/Linux:
source env/bin/activate
# On Windows:
# venv\Scripts\activate
Install Dependencies
pip install -r requirements.txt
Environment Configuration
Create a .env file in the project root with the required environment
variables. The following variables are used by the application:
Running the Application
Basic Usage
python3 main.py
Usage Examples
Basic Usage (No Authentication)
For utility functions, background tasks, or non-authenticated endpoints:
from loggio import get_logger
# Create a logger instance.
logger = get_logger(name="module.name")
# Log messages with different levels
logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
Output example:
INFO:2023-05-01 14:30:45:example.py:24:This is an info message.
Using Format Strings
The logger supports Python's standard string formatting:
from loggio import get_logger
logger = get_logger(name="module.formatter")
# Using format strings
item_id = "A123"
priority = 2
logger.info("Processing item %s with priority %d", item_id, priority)
Output example:
INFO:2023-05-01 14:31:22:formatter.py:15:Processing item A123 with priority 2
Usage with User Context
When you have authentication info:
from loggio import get_logger
# Create a logger instance.
logger = get_logger(name="module.auth")
# Get user info from somewhere (e.g., decoded token)
user_info = {"uid": "user123", "email": "user@example.com"}
# Log with user context
logger.info("User performed an action.", user_context=user_info)
Output example:
INFO:2023-05-01 14:32:10:auth_service.py:45:user123: User performed an action.
Format Strings with User Context
Combining format strings with user context:
logger.info("Processing file %s for action %s", filename, action, user_context=user_info)
Error Handling with Logging
try:
# Some risky operation
result = perform_risky_operation()
logger.info(f"Operation successful: {result}")
except Exception as e:
logger.error(f"Operation failed: {str(e)}")
# Handle or re-raise as appropriate
Configuration Options
When creating a logger instance, you can configure:
name: Logger name for filtering and organization (e.g., "api.auth", "service.processor")level: Minimum log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) - defaults to "INFO"fileout: Whether to write logs to a file (default: False)terminal: Whether to output logs to the terminal/console (default: True)fileout_path: Path for log file iffileoutis True (default: "logs/app.log")
Example with custom configuration:
logger = get_logger(
name="batch.processor",
level="DEBUG",
fileout=True,
fileout_path="logs/batch_processor.log",
terminal=True
)
Log Format
The logger produces logs in the following format:
LEVEL:TIMESTAMP:FILENAME:LINE_NUMBER:MESSAGE
For example:
INFO:2023-05-01 14:35:22:auth_routes.py:28:user123: Processing protected resource request
Best Practices
- Use a hierarchical naming scheme for loggers (e.g.,
api.auth,service.processor) - Include meaningful context in log messages
- Use the appropriate log level for different scenarios:
- DEBUG: Detailed information for debugging
- INFO: Confirmation that things are working as expected
- WARNING: Something unexpected happened, but the application can continue
- ERROR: A more serious problem that prevented a function from working
- CRITICAL: A very serious error that might prevent the program from continuing
- Include user context when available for better traceability
- Include error details when logging exceptions
Implementation Details
The Enhanced Logger builds on Python's built-in logging module with custom
formatting and context handling. It uses stacklevel=2 to ensure the correct
file and line number are recorded in the logs rather than showing the logger
implementation file itself.
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 loggio-2025.12.13.tar.gz.
File metadata
- Download URL: loggio-2025.12.13.tar.gz
- Upload date:
- Size: 12.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63d3e0001cf83379dccfb3092c7e9a3e498a0aa202b6ad626d7d2167649294bf
|
|
| MD5 |
87295461b35cc53b994d2fd3057e56a3
|
|
| BLAKE2b-256 |
71e696e7886764633ccd648507fe270ca12b0568d7b49a22bb73a83aebec62f1
|
File details
Details for the file loggio-2025.12.13-py3-none-any.whl.
File metadata
- Download URL: loggio-2025.12.13-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e9ac17d6ed2152d706fe4d6239f067c787312941a2fa37f3e3cd3aa0c928f7e
|
|
| MD5 |
27db8fb1919ccf0f20552fae83a37da6
|
|
| BLAKE2b-256 |
5acf7b76db2a40790473e78470402092d3fa877e579fc862ed26df0d0e8bd7cc
|