An enhanced logging package for Python with additional features and performance metrics.
Project description
Enhanced Logger
The Enhanced Logger
package is an extension of the built-in Python logging
module, designed to address additional features and performance metrics. This package includes:
- Trace logging to log all function entry and exit
- JSON and XML formatters
- Performance metrics logging
- HTTP and database logging handlers
- File logging for all logger types (except DB Handler)
Features
- Trace Logging: Log entry and exit points of functions with context.
- Performance Metrics: Log execution time and memory usage of functions.
- Enhanced Formatters: Log entries in JSON and XML formats.
- HTTP Logging: Send log entries to a remote server via HTTP.
- Database Logging: Store log entries in various types of databases (SQLite, PostgreSQL, MongoDB).
- File Logging: Option to store logs in a
.log
file.
Installation
To install the Enhanced Logger package, use the following command:
pip install enhanced-logger
Usage
Trace Logger
Trace logging allows you to trace the entry and exit points of functions.
from enhanced_logger import EnhancedLogger
trace_logger = EnhancedLogger.configure_trace_logger('trace_logger')
@trace_logger.trace
def sample_function(x, y):
return x + y
sample_function(5, 10)
Performance Logger
Performance logging provides execution time and memory usage information.
from enhanced_logger import EnhancedLogger
performance_logger = EnhancedLogger.configure_performance_logger('performance_logger')
@performance_logger.log_performance
def example_function(n):
total = 0
for i in range(n):
total += i
return total
example_function(1000000)
Enhanced Performance Logger
Enhanced performance logging with additional details.
from enhanced_logger import EnhancedLogger
enhanced_performance_logger = EnhancedLogger.configure_enhanced_performance_logger('enhanced_performance_logger')
@enhanced_performance_logger.log_performance
def compute_factorial(n):
if n == 0:
return 1
else:
return n * compute_factorial(n - 1)
compute_factorial(10)
JSON Formatter
Logs entries in JSON format.
from enhanced_logger import EnhancedLogger
json_logger = EnhancedLogger.configure_json_logger('json_logger')
json_logger.info('This is a test log entry in JSON format.')
You can also log context data.
from enhanced_logger import EnhancedLogger
json_logger = EnhancedLogger.configure_json_logger('json_logger')
context_info = {
'context': {
'user_id': 12345,
'transaction_id': 'abcde12345'
}
}
json_logger.info('This is a test log entry in JSON format with context.', extra=context_info)
or you can also use decorator.
@EnhancedLogger.json_log('json_logger')
def add(x, y):
return x + y
@EnhancedLogger.json_log('json_logger')
def multiply(x, y):
return x * y
print(add(5, 3))
print(multiply(5, 3))
XML Formatter
Logs entries in XML format.
from enhanced_logger import EnhancedLogger
xml_logger = EnhancedLogger.configure_xml_logger('xml_logger')
xml_logger.info('This is a test log entry in XML format.')
HTTP Handler
Sends log entries to a remote server via HTTP. (Assumes a logging endpoint is available)
from enhanced_logger import EnhancedLogger
http_logger = EnhancedLogger.configure_http_logger('http_logger', 'http://example.com/log', method='POST', headers={'Content-Type': 'application/json'})
http_logger.info('This is a test log entry sent via HTTP.')
DB Handler
Stores log entries in various types of databases likes SQLite/Postgres/MongoDB.
SQLite
from enhanced_logger import EnhancedLogger
sqlite_logger = EnhancedLogger.configure_db_logger('sqlite_logger', 'sqlite', {'db_path': 'logs.db'})
sqlite_logger.info('This is a test log entry stored in a SQLite database.')
PostgreSQL
from enhanced_logger import EnhancedLogger
postgres_logger = EnhancedLogger.configure_db_logger('postgres_logger', 'postgres', {
'dbname': 'testdb',
'user': 'dbuser',
'password': 'dbpass',
'host': 'localhost',
'port': 5432
})
postgres_logger.info('This is a test log entry stored in a PostgreSQL database.')
MongoDB
from enhanced_logger import EnhancedLogger
mongodb_logger = EnhancedLogger.configure_db_logger('mongodb_logger', 'mongodb', {
'uri': 'mongodb://localhost:27017/',
'db_name': 'testdb'
})
mongodb_logger.info('This is a test log entry stored in a MongoDB database.')
More Examples
You can also store logs in a .log
file by specifying file name for the log_to_file
argument.
from enhanced_logger import EnhancedLogger
# JSON Logger
@EnhancedLogger.json_log('json_logger', log_to_file='json_logs.log')
def add(x, y):
return x + y
print(add(5, 3))
# Performance Logger
performance_logger = EnhancedLogger.configure_performance_logger('performance_logger', log_to_file='performance_logs.log')
@performance_logger.log_performance
def compute_factorial(n):
if n == 0:
return 1
else:
return n * compute_factorial(n - 1)
print(compute_factorial(10))
# HTTP Logger
from enhanced_logger import EnhancedLogger
http_logger = EnhancedLogger.configure_http_logger('http_logger', 'http://example.com/log', method='POST', headers={'Content-Type': 'application/json'}, log_to_file='http_logs.log')
http_logger.info('This is a test log entry sent via HTTP.')
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
File details
Details for the file enhanced_logger-0.1.1.tar.gz
.
File metadata
- Download URL: enhanced_logger-0.1.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0ecdcd73cfa856ebf85b37dd5b1688acdded73efea038acc30998816b1da0d3 |
|
MD5 | d324d8a0aff8216d691d11a67eab84e6 |
|
BLAKE2b-256 | 27c86e192d2a438d830a50f6695c32d9dd30eee9092a1e4778ea480d33a43250 |
File details
Details for the file enhanced_logger-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: enhanced_logger-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 591e369a1f80da895362aed7e31b99baa5cdf0ce8fa5b1355851830cf672963e |
|
MD5 | 3cd38c063853536bedb663f39abccd82 |
|
BLAKE2b-256 | 9cc5ebb9355cc37b38ad5f09c7072fef93e134c1e042ac11ef72fa4ad5cdf348 |