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.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: enhanced_logger-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 793763d1ab05d623576bf4615560de3ce5d17faa171c84ba3dde163a9ad1ee72
MD5 053f5d5ba7f66a5181c018461f5cdce8
BLAKE2b-256 b5e388d10c3e1dd89aa38e717873744e1ecbd5c79bea6ef50e6b79b156497d51

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for enhanced_logger-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f6d0e874cf7d9f00068baa4a318fe8edf88e276cd7f4a630146ae15dfc61fcbe
MD5 60ec29aca58fb9568c1c1ae52a69ab38
BLAKE2b-256 97bbb2c609dba86a7d7986b86aa103570a0282c68515feda5ace0db85c929a1f

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