Skip to main content

Unified logging and alerting library for Python.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

commonlog (Python)

A unified logging and alerting library for Python, supporting Slack and Lark integrations via WebClient. Features configurable providers, alert levels, and file attachment support.

Installation

Install via pip:

pip install commonlog

Or copy the python/ directory to your project.

Usage

from python import commonlog, Config, SendMethod, AlertLevel, Attachment

# Configure logger
config = Config(
    provider="lark", # or "slack"
    send_method=SendMethod.WEBCLIENT,
    token="app_id++app_secret", # for Lark, use "app_id++app_secret" format
    channel="your_lark_channel_id",
    redis_host="localhost", # required for Lark
    redis_port="6379",      # required for Lark
)
logger = commonlog(config)

# Send error with attachment
logger.send(AlertLevel.ERROR, "System error occurred", Attachment(url="https://example.com/log.txt"))

 # Send info (logs only)
logger.send(AlertLevel.INFO, "Info message")

# Send to a specific channel
logger.send_to_channel(AlertLevel.ERROR, "Send to another channel", channel="another-channel-id")

Lark Token Caching

When using Lark, the tenant_access_token is cached in Redis. The expiry is set dynamically from the API response minus 10 minutes. You must set redis_host and redis_port in your config.

Channel Mapping

You can configure different channels for different alert levels using a channel resolver:

from commonlog import commonlog, Config, SendMethod, AlertLevel, DefaultChannelResolver

# Create a channel resolver
resolver = DefaultChannelResolver(
    channel_map={
        AlertLevel.INFO: "#general",
        AlertLevel.WARN: "#warnings",
        AlertLevel.ERROR: "#alerts",
    },
    default_channel="#general"
)

# Create config with channel resolver
config = Config(
    provider="slack",
    send_method=SendMethod.WEBCLIENT,
    token="xoxb-your-slack-bot-token",
    channel_resolver=resolver,
    service_name="user-service",
    environment="production"
)

logger = commonlog(config)

# These will go to different channels based on level
logger.send(AlertLevel.INFO, "Info message")    # goes to #general
logger.send(AlertLevel.WARN, "Warning message") # goes to #warnings
logger.send(AlertLevel.ERROR, "Error message")  # goes to #alerts

Custom Channel Resolver

You can implement custom channel resolution logic:

class CustomResolver(ChannelResolver):
    def resolve_channel(self, level):
        if level == AlertLevel.ERROR:
            return "#critical-alerts"
        elif level == AlertLevel.WARN:
            return "#monitoring"
        else:
            return "#general"

Configuration Options

Common Settings

  • provider: "slack" or "lark"
  • send_method: "webclient" (token-based authentication)
  • channel: Target channel or chat ID (used if no resolver)
  • channel_resolver: Optional resolver for dynamic channel mapping
  • service_name: Name of the service sending alerts
  • environment: Environment (dev, staging, production)

Provider-Specific

  • token: API token for WebClient authentication (required)

Alert Levels

  • INFO: Logs locally only
  • WARN: Logs + sends alert
  • ERROR: Always sends alert

File Attachments

Provide a public URL. The library appends it to the message for simplicity.

attachment = Attachment(url="https://example.com/log.txt")
logger.send(AlertLevel.ERROR, "Error with log", attachment)

Trace Log Section

When include_trace is set to True, you can pass trace information as the fourth parameter to send():

trace = """Traceback (most recent call last):
  File "app.py", line 10, in main
    raise ValueError("Something went wrong")
ValueError: Something went wrong"""

logger.send(AlertLevel.ERROR, "System error occurred", None, trace)

This will format the trace as a code block in the alert message.

Testing

cd python
PYTHONPATH=.. python -m unittest test_commonlog.py

API Reference

Classes

  • Config: Configuration class
  • Attachment: File attachment class
  • Provider: Abstract base class for alert providers
  • commonlog: Main logger class

Constants

  • SendMethod.WEBCLIENT: Send method (token-based authentication)
  • AlertLevel.INFO, AlertLevel.WARN, AlertLevel.ERROR: Alert levels

Methods

  • commonlog(config): Create a new logger
  • commonlog.send(level, message, attachment=None, trace=""): Send alert with optional trace

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

commonlog-0.1.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

commonlog-0.1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: commonlog-0.1.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for commonlog-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c788d031495997fba8778cbfb5d6b52b028b358edd02a499fb609120da9c434e
MD5 c820015c9f23d588804ed54c0f9e10b2
BLAKE2b-256 31a6ae18dfec326122e28da9aed6c4d3adc6b3ce6248f75df2341c3cf5a93ffb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: commonlog-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for commonlog-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1c93e496a83cf611e1f0cac3ddbbf90df65b1339ec05cf8ea346f59505b3b1eb
MD5 56b496b02cecff944591e70a01ee3ea8
BLAKE2b-256 ccf1a2c573c90edbdc32c1ad2482406badb6595758e131376cc58da2838f7ae1

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