Skip to main content

A logging utility package with colored logs.

Project description

Medusa Logger

Medusa Logger is a Python package that provides a customizable logging utility with support for automatic inclusion of caller's filename and line number in logs. It aims to simplify logging in Python applications by offering features such as log level configuration, colored logs, and conditional logging.

View the full documentation here

Medusa Logger

Features

  • Caller Information: Automatically includes the caller's filename and line number in log messages for better traceability.
  • Customizable Log Levels: Easily configure log levels to control the severity of messages that the logger will process.
  • Colored Logs: Enhance log readability with colored logs for different log levels.
  • Conditional Logging: Conditionally log messages based on specific conditions to control verbosity.

Installation

You can install Medusa Logger via pip:

pip install medusa-logger

Usage

Please check out example usage here

Basic Logging

  1. Import the Logger class from the medusa_logger.logger module.
  2. Initialize a Logger instance with a name and an optional log level. The default log level is INFO.
  3. Use the logging methods (debug, info, warning, error, critical) to log messages at various severity levels.
from medusa_logger.logger import Logger

# Initialize the logger with a custom name and log level
logger = Logger(name='application_log', level='DEBUG')

# Log messages at different levels
logger.debug("Debug message for detailed diagnostic information")
logger.info("Info message for general information")
logger.warning("Warning message for potential issues")
logger.error("Error message for serious problems")
logger.critical("Critical message for severe conditions")

Function Logging with Decorators

Medusa Logger provides a logging decorator that can be applied to functions to automatically log calls, returns, and execution times. To use this feature:

  1. Import the log decorator from the medusa_logger.decorator module.
  2. Apply the @log decorator to any function. You can optionally specify a log level or a condition for logging.
from medusa_logger.decorator import log

@log(level="DEBUG")
def compute_sum(a, b):
    """Function to demonstrate logging with a decorator."""
    return a + b

@log(condition=lambda args, kwargs: args[0] > args[1])
def conditional_log_example(x, y):
    """Function that logs only if the condition is true."""
    return x * y

# Call the decorated functions
sum_result = compute_sum(1, 2)
product_result = conditional_log_example(5, 3)

Advanced Configuration

Setting Global Log Level: You can set a global log level by modifying the environment variable MD_LOG_LEVEL before initializing your logger. This can be done within your script or externally through your system's environment settings. By default it is set to DEBUG.

Python

import os
os.environ["MD_LOG_LEVEL"] = "WARNING"

Bash

export MD_LOG_LEVEL="WARNING"

Handling Exceptions

Medusa Logger makes it easy to log exceptions. Use the logging methods within exception handling blocks to log errors and critical issues.

try:
    # Potentially problematic code
    result = 10 / 0
except ZeroDivisionError as e:
    logger.error(f"Caught an exception: {e}")

Contributing

Contributions to Medusa Logger are welcome! To contribute, follow these steps:

  1. Fork the repository and clone it to your local machine.
  2. Install the development dependencies by running pip install -r requirements.txt.
  3. Make your changes and ensure tests pass by running pytest.
  4. Submit a pull request with a clear description of your changes and why they are beneficial.

Please adhere to the code of conduct when contributing to this project.

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

medusa-logger-1.3.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

medusa_logger-1.3.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file medusa-logger-1.3.1.tar.gz.

File metadata

  • Download URL: medusa-logger-1.3.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for medusa-logger-1.3.1.tar.gz
Algorithm Hash digest
SHA256 35c3478cbed7d65bb7b03459ad515ad2a0066a8548965cf9349590bfdb346ff3
MD5 4de192f8ac5388f69dcc62f985066428
BLAKE2b-256 9b99f2651d6ddca0c905d82b25d1e39b0ccd746425e309d07e776330e4579ddd

See more details on using hashes here.

File details

Details for the file medusa_logger-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: medusa_logger-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for medusa_logger-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 caf0ddf12bc13213222c6ca7e8cf5c05c88e482cebd58fb27144e6d719deb8ef
MD5 d5555957523c61d16e6e1b61e6ca5e95
BLAKE2b-256 fa826a8ef677641dddcba50ae07f0653f7b0b8f8af980708baa2cce89b89c0dd

See more details on using hashes here.

Supported by

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