Skip to main content

Visualize your Python code execution flow as Mermaid Sequence Diagrams.

Project description

MermaidTrace: The Python Logger That Draws Diagrams

🌐 Language: English | 中文

PyPI version Python Versions License CI Status Codecov


📋 Overview

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.


📚 Documentation

User Guide · API Reference · Contributing Guidelines · Changelog · License


✨ Key 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

🤝 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.1.tar.gz (364.8 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.1-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mermaid_trace-0.4.1.tar.gz
  • Upload date:
  • Size: 364.8 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.1.tar.gz
Algorithm Hash digest
SHA256 abaefd7b65a94ae6448cf663dcbe1a3378b27771cbf5dd55c00fd7cc8e144c0f
MD5 de8d14efd051b18fd8aef1087345bf6e
BLAKE2b-256 1e312c156a273df345092706878310d152dc0b456c1fc132687219d11b800f5e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mermaid_trace-0.4.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: mermaid_trace-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 28.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7ab05a460e7973d5ef2277b8ab59a537195f0f74a4a1e87c062d4efe0dbd0783
MD5 8a3e723d966db24af7502a156bf7b81a
BLAKE2b-256 634e0974802802bb0eaee16773060a378d9ff6b441c3da6e3bbc2f60c832fbe2

See more details on using hashes here.

Provenance

The following attestation bundles were made for mermaid_trace-0.4.1-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