Skip to main content

Visualize your Python code execution flow as Mermaid Sequence Diagrams.

Project description

MermaidTrace: The Python Logger That Draws Diagrams

PyPI version Python Versions License CI Status Codecov

Stop reading logs. Start watching them.

MermaidTrace is a specialized logging tool that automatically generates Mermaid JS sequence diagrams from your code execution. It's perfect for visualizing complex business logic, microservice interactions, or asynchronous flows.

✨ Features

  • Decorator-Driven: Just add @trace or @trace_interaction to your functions.
  • Auto-Diagramming: Generates .mmd files that can be viewed in VS Code, GitHub, or Mermaid Live Editor.
  • Async Support: Works seamlessly with asyncio coroutines.
  • Context Inference: Automatically tracks nested calls and infers source participants using contextvars.
  • FastAPI Integration: Includes middleware for zero-config HTTP request tracing.
  • CLI Tool: Built-in viewer to preview diagrams in your browser.

🚀 Quick Start

Installation

pip install mermaid-trace

Basic Usage

from mermaid_trace import trace, configure_flow
import time

# 1. Configure output
configure_flow("my_flow.mmd")

# 2. Add decorators
@trace(source="Client", target="PaymentService", action="Process Payment")
def process_payment(amount):
    if check_balance(amount):
        return "Success"
    return "Failed"

@trace(source="PaymentService", target="Database", action="Check Balance")
def check_balance(amount):
    return True

# 3. Run your code
process_payment(100)

Nested Calls (Context Inference)

You don't need to specify source every time. MermaidTrace infers it from the current context.

@trace(source="Client", target="API")
def main():
    # Inside here, current participant is "API"
    service_call()

@trace(target="Service") # source inferred as "API"
def service_call():
    pass

FastAPI Integration

from fastapi import FastAPI
from mermaid_trace.integrations.fastapi import MermaidTraceMiddleware

app = FastAPI()
app.add_middleware(MermaidTraceMiddleware, app_name="MyAPI")

@app.get("/")
async def root():
    return {"message": "Hello World"}

CLI Viewer

Visualize your generated .mmd files instantly:

mermaid-trace serve my_flow.mmd

📂 Documentation

🤝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for details.

📄 License

MIT

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

mermaid_trace-0.4.0.tar.gz (62.9 kB view details)

Uploaded Source

Built Distribution

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

mermaid_trace-0.4.0-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file mermaid_trace-0.4.0.tar.gz.

File metadata

  • Download URL: mermaid_trace-0.4.0.tar.gz
  • Upload date:
  • Size: 62.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mermaid_trace-0.4.0.tar.gz
Algorithm Hash digest
SHA256 8c04b34038b115a547fa546558bff491b0158926374b7b85967bf95f0d43d1b9
MD5 6d2cd67abd08f1c21e965b16a5b9532c
BLAKE2b-256 a25670f8e0d0e9adda000689c9b8d8d3d5b7a815a0b132ed21f2a6af640bf28a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mermaid_trace-0.4.0.tar.gz:

Publisher: release.yml on xt765/mermaid-trace

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mermaid_trace-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: mermaid_trace-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mermaid_trace-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cc7a0a3b4034dee6d950e5c67bd1bbbd0e9f852f16f4d7594dfdf8627e683f8c
MD5 e6655ef5c8a96e238533697ecc481b8a
BLAKE2b-256 d029b6374cbe11d57d9091c302ad37df786507ac3fa1b9edab56fc65dedee0c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for mermaid_trace-0.4.0-py3-none-any.whl:

Publisher: release.yml on xt765/mermaid-trace

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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