Skip to main content

An enhanced logging package for Python with additional features and performance metrics.

Project description

Enhanced Logger


PyPI version Downloads License

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

enhanced_logger-0.1.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

enhanced_logger-0.1.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file enhanced_logger-0.1.3.tar.gz.

File metadata

  • Download URL: enhanced_logger-0.1.3.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

Hashes for enhanced_logger-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ab1af8a4e0937f985a679d71a9e7a0a04c2488f5ae9ed6f4108d2e1888bfe342
MD5 f3404129873ddd035cf6109666c2e497
BLAKE2b-256 dcd07494c4cb3d50b258bf7fef13d603931ac8edbdecc8d25ee3f971235069bb

See more details on using hashes here.

File details

Details for the file enhanced_logger-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for enhanced_logger-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7193de5e7decb2d81aeb4bef0690f30a1500718a95ddc4e47017cb1c957a5e3b
MD5 0a44c3fc43b2f26d70578b23ee6698bf
BLAKE2b-256 53a7180390741d7695992b3d6ea3913e29ede3a154c57da78284edbda290cb6a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page