mnohosten/tracy-markdown 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mnohosten/tracy-markdown

Composer 安装命令:

composer require mnohosten/tracy-markdown

包简介

Tracy debugger extension that allows copying error reports as Markdown - useful for AI-assisted development

README 文档

README

Tests PHP Version Tracy Version License

A Tracy debugger extension that adds a "Copy Markdown" button to error reports. Perfect for AI-assisted development - simply click the button to copy a well-formatted error report that you can paste directly into your AI assistant (Claude, ChatGPT, etc.).

Screenshot

Why Use This?

When developing with AI assistants, providing detailed error context helps get better solutions faster. This extension formats Tracy's error information into clean, readable Markdown that includes:

  • Complete error details with proper formatting
  • Source code context highlighting the exact line
  • Full stack trace with file paths and line numbers
  • HTTP request information (URL, method, parameters)
  • Environment details (PHP version, timestamp)
  • Automatic sensitive data redaction for security

Installation

composer require mnohosten/tracy-markdown

Quick Start

Basic PHP Application

<?php

use Tracy\Debugger;
use Mnohosten\TracyMarkdown\TracyMarkdownExtension;

// Enable Tracy debugger
Debugger::enable();

// Register the Markdown extension
TracyMarkdownExtension::register();

Nette Framework

Option 1: Bootstrap file

In your app/bootstrap.php:

<?php

use Tracy\Debugger;
use Mnohosten\TracyMarkdown\TracyMarkdownExtension;

Debugger::enable();
TracyMarkdownExtension::register();

Option 2: DI Configuration

In your config/common.neon:

services:
    - Mnohosten\TracyMarkdown\TracyMarkdownExtension::register()

Symfony Framework

In your config/packages/dev/tracy.yaml or bootstrap:

<?php

use Tracy\Debugger;
use Mnohosten\TracyMarkdown\TracyMarkdownExtension;

if (class_exists(Debugger::class)) {
    Debugger::enable();
    TracyMarkdownExtension::register();
}

Features

Visual Interface

When an error occurs, a "Copy Markdown" button appears in the top-right corner of Tracy's BlueScreen with:

  • Modern, responsive design
  • Hover and click animations
  • Visual feedback (checkmark) when copied
  • Works on both desktop and mobile browsers
  • Fallback for browsers without Clipboard API

Markdown Output Sections

The copied Markdown includes all essential debugging information:

1. Error Information

  • Exception class name
  • Error message with special character escaping
  • Error code (if present)

2. Source File

  • Complete file path with line number
  • Code snippet with 5 lines of context before and after
  • Line highlighting showing the exact error location

3. Stack Trace

  • Complete call stack with all frames
  • File paths and line numbers for each frame
  • Function/method names with arguments
  • Formatted argument types (strings, ints, objects, arrays)

4. Previous Exception

  • Captured if the exception was caused by another
  • Includes file location and message
  • Helps trace root causes

5. HTTP Request (Web Context)

  • URL: Complete request URL
  • Method: GET, POST, PUT, DELETE, etc.
  • GET Parameters: Query string data (formatted JSON)
  • POST Parameters: Form/body data (formatted JSON with sensitive data redacted)
  • Headers: HTTP headers (Authorization and Cookie headers excluded)

6. Environment

  • PHP version
  • Tracy version
  • Timestamp of error occurrence

Example Output

# Error

**Exception**: Call to undefined method User::getIdString()

## Source file

**File**: `/var/www/app/Controller/AuthController.php:267`

\`\`\`php
   262 |     /**
   263 |      * Helper to send verification email.
   264 |      */
   265 |     private function sendVerificationEmail(User $user): void
   266 |     {
>  267 |         $token = $this->emailVerifications->createToken($user->getIdString(), $user->email);
   268 |         $verifyUrl = ($_ENV['APP_URL'] ?? 'http://localhost:8180') . '/auth/verify-email/' . $token;
   269 |
   270 |         try {
   271 |             $this->mailer->sendEmailVerification($user->email, $user->name, $verifyUrl);
\`\`\`

## Stack trace

\`\`\`
#0 /var/www/app/Controller/AuthController.php:245 App\Controller\AuthController->sendVerificationEmail(User)
#1 /var/www/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php:43 App\Controller\AuthController->register(Slim\Psr7\Request, Slim\Psr7\Response, array(0))
#2 [internal]:0 call_user_func(array(2))
\`\`\`

## Request

**URL**: `http://localhost:8180/auth/register`
**Method**: `POST`

### POST parameters

\`\`\`json
{
    "email": "test@example.com",
    "name": "Test User",
    "password": "***REDACTED***"
}
\`\`\`

### Headers

\`\`\`
Content-Type: application/json
Accept: application/json
User-Agent: Mozilla/5.0
\`\`\`

## Environment

- **PHP**: 8.4.15
- **Tracy**: 2.11.0
- **Date**: 2025-12-14 22:30:45

Security Features

Automatic Sensitive Data Redaction

The extension automatically detects and redacts sensitive information in POST data to prevent accidental exposure:

Redacted Fields (case-insensitive, partial matching):

  • password, passwd, pwd
  • secret
  • token
  • api_key, apikey
  • credit_card, cc

Example:

{
    "username": "john.doe",
    "password": "***REDACTED***",
    "user_token": "***REDACTED***",
    "email": "john@example.com"
}

Nested Data Support:

{
    "user": {
        "name": "John",
        "password": "***REDACTED***"
    }
}

Header Filtering

The following headers are automatically excluded from reports:

  • Authorization - Prevents exposure of bearer tokens, API keys
  • Cookie - Prevents session hijacking

Browser Support

The extension uses modern Clipboard API with automatic fallback:

  • Modern browsers: Uses navigator.clipboard.writeText() for secure, asynchronous copying
  • Older browsers: Falls back to document.execCommand('copy') for compatibility
  • All browsers: Shows visual feedback with checkmark animation

Requirements

  • PHP: 8.0 or higher
  • Tracy: 2.9 or higher
  • Browser: Any modern browser (Chrome, Firefox, Safari, Edge)

Development

Running Tests

# Install dependencies
composer install

# Run test suite
vendor/bin/phpunit

# Run with detailed output
vendor/bin/phpunit --testdox

# Run specific test
vendor/bin/phpunit --filter testFormatException

Code Coverage

Install a coverage driver:

# PCOV (recommended - faster)
pecl install pcov

# OR Xdebug
pecl install xdebug

Generate coverage report:

# With PCOV
vendor/bin/phpunit --coverage-html coverage-html

# With Xdebug
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html coverage-html

# View report
open coverage-html/index.html

Test Coverage

The library maintains 100% code coverage with 53 tests and 134 assertions:

  • ✅ Exception formatting (all output sections)
  • ✅ Request data collection and sanitization
  • ✅ Sensitive data redaction (11 different patterns)
  • ✅ Tracy BlueScreen integration
  • ✅ Copy button rendering and interaction
  • ✅ Edge cases (null values, missing data, XSS prevention)

See TESTING.md for detailed coverage information.

Static Analysis

# Install PHPStan
composer require --dev phpstan/phpstan

# Run analysis
vendor/bin/phpstan analyse src tests

Architecture

Class Structure

src/
├── TracyMarkdownExtension.php  # Main entry point, Tracy integration
├── MarkdownFormatter.php       # Exception → Markdown conversion
└── MarkdownPanel.php           # UI rendering (button, CSS, JS)

Extension Points

The library is designed to be extensible:

// Get the formatter instance for custom use
$extension = TracyMarkdownExtension::getInstance();
$formatter = $extension->getFormatter();

// Format any exception manually
$markdown = $formatter->formatException($exception);

// Collect request data manually
$requestData = $formatter->collectRequestData();

Troubleshooting

Button Not Appearing

Check Tracy is enabled:

Debugger::$productionMode = false; // Force development mode
Debugger::enable(Debugger::Development);

Verify extension is registered:

TracyMarkdownExtension::register();
// Must be called AFTER Debugger::enable()

Copy Not Working

Try the fallback method:

  • The extension automatically falls back to document.execCommand('copy')
  • Check browser console for JavaScript errors
  • Ensure the page is served over HTTPS (required for Clipboard API)

Missing Request Data

CLI mode:

  • Request data is only collected in web (non-CLI) context
  • Use the formatter directly for CLI exceptions

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Write tests for new functionality
  4. Ensure all tests pass (vendor/bin/phpunit)
  5. Maintain 100% code coverage
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Code Style

  • Follow PSR-12 coding standard
  • Use strict types (declare(strict_types=1))
  • Add PHPDoc blocks for public methods
  • Write descriptive commit messages

FAQ

Q: Does this work with Tracy Bar? A: This extension specifically adds functionality to Tracy's BlueScreen (error page). It doesn't modify the Tracy Bar.

Q: Can I customize the Markdown output? A: Currently, the format is fixed. If you need customization, you can extend MarkdownFormatter and override the formatException() method.

Q: Is sensitive data really redacted? A: Yes, the extension uses pattern matching to detect 11 common sensitive field patterns and replaces values with ***REDACTED***. However, always review output before sharing.

Q: What about files uploaded via POST? A: File uploads appear as their metadata only (filename, size, type). File contents are never included.

Q: Can I use this in production? A: Tracy itself should only run in development. This extension follows the same principle - it only activates when Tracy's BlueScreen is shown.

Changelog

See CHANGELOG.md for release history.

License

MIT License - see LICENSE file for details.

Credits

Created by Martin Krizan

Built with:

  • Tracy - PHP debugging tool by David Grudl
  • PHPUnit - Testing framework

Support

Happy debugging! 🐛 → 📋 → 🤖

mnohosten/tracy-markdown 适用场景与选型建议

mnohosten/tracy-markdown 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 382 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 12 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「debugging」 「markdown」 「debugger」 「tracy」 「error-reporting」 「ai-development」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mnohosten/tracy-markdown 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mnohosten/tracy-markdown 我们能提供哪些服务?
定制开发 / 二次开发

基于 mnohosten/tracy-markdown 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 382
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 36
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-14