baraja-core/tracy-sentry-bridge 问题修复 & 功能扩展

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

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

baraja-core/tracy-sentry-bridge

Composer 安装命令:

composer require baraja-core/tracy-sentry-bridge

包简介

The package provides an easy connection between Tracy and Sentry.

README 文档

README

The package provides an easy connection between Tracy debugger and Sentry error tracking service. It allows you to automatically forward all Tracy log entries to Sentry while preserving the original Tracy logging functionality.

💡 Key Principles

  • Seamless Integration: Works as a drop-in replacement for Tracy's default logger
  • Dual Logging: All logs are sent to both the original Tracy logger and Sentry simultaneously
  • No Data Loss: Original Tracy logs remain intact regardless of Sentry status
  • Automatic Severity Mapping: Tracy log levels are automatically converted to appropriate Sentry severity levels
  • Fault Tolerant: If Sentry logging fails, errors are captured by the fallback logger
  • Zero Configuration: Simple one-line registration in your bootstrap

🏗️ Architecture

The package implements Tracy's ILogger interface and uses the decorator pattern to wrap the original Tracy logger. This ensures that all existing logging functionality continues to work while adding Sentry integration on top.

┌─────────────────────────────────────────────────────────────────┐
│                     Your Application                            │
│                           │                                     │
│                     log($value, $level)                         │
│                           ▼                                     │
│  ┌─────────────────────────────────────────────────────────┐   │
│  │                   SentryLogger                           │   │
│  │                  (ILogger impl.)                         │   │
│  │                                                          │   │
│  │   ┌──────────────────────────────────────────────────┐  │   │
│  │   │                    log()                          │  │   │
│  │   │                      │                            │  │   │
│  │   │          ┌───────────┴───────────┐                │  │   │
│  │   │          ▼                       ▼                │  │   │
│  │   │   ┌─────────────┐       ┌───────────────┐         │  │   │
│  │   │   │   Fallback  │       │  logToSentry  │         │  │   │
│  │   │   │   Logger    │       │               │         │  │   │
│  │   │   │  (original) │       │ ┌───────────┐ │         │  │   │
│  │   │   └──────┬──────┘       │ │  Severity │ │         │  │   │
│  │   │          │              │ │  Mapping  │ │         │  │   │
│  │   │          ▼              │ └─────┬─────┘ │         │  │   │
│  │   │   ┌─────────────┐       │       ▼       │         │  │   │
│  │   │   │ Tracy Logs  │       │ ┌───────────┐ │         │  │   │
│  │   │   │  (files)    │       │ │  Sentry   │ │         │  │   │
│  │   │   └─────────────┘       │ │   API     │ │         │  │   │
│  │   │                         │ └───────────┘ │         │  │   │
│  │   └──────────────────────────────────────────────────┘  │   │
│  └─────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────┘

⚙️ Main Components

SentryLogger

The core class that implements Tracy's ILogger interface. It acts as a bridge between Tracy and Sentry.

Key Features:

Feature Description
register() Static method to automatically register the logger with Tracy
log() Main logging method that forwards to both Sentry and fallback logger
Severity Mapping Automatic conversion of Tracy levels to Sentry severity
Exception Handling Proper handling of both Throwable objects and string messages

Severity Level Mapping

The package automatically maps Tracy log levels to Sentry severity:

Tracy Level Sentry Severity
DEBUG debug
INFO info
WARNING warning
ERROR error
EXCEPTION error
CRITICAL fatal

Any unrecognized level defaults to fatal severity.

📦 Installation

It's best to use Composer for installation, and you can also find the package on Packagist and GitHub.

To install, simply use the command:

$ composer require baraja-core/tracy-sentry-bridge

You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework.

Requirements

  • PHP 8.0 or higher
  • Tracy ^2.8 or 3.0-dev
  • Sentry SDK ^3.1

🚀 Basic Usage

Quick Start with Nette Framework

In your project's Bootstrap.php file, initialize Sentry first and then register the bridge:

use Baraja\TracySentryBridge\SentryLogger;

public static function boot(): Configurator
{
    $configurator = new Configurator;

    // Initialize Sentry first
    if (\function_exists('\Sentry\init')) {
        \Sentry\init([
            'dsn' => 'https://your-sentry-dsn@sentry.io/project-id',
            'attach_stacktrace' => true,
        ]);
    }

    // Register the Tracy-Sentry bridge
    SentryLogger::register();

    return $configurator;
}

Standalone Usage

If you're not using Nette Framework, you can still use the package with standalone Tracy:

use Tracy\Debugger;
use Baraja\TracySentryBridge\SentryLogger;

// Enable Tracy
Debugger::enable();

// Initialize Sentry
\Sentry\init([
    'dsn' => 'https://your-sentry-dsn@sentry.io/project-id',
]);

// Register the bridge
SentryLogger::register();

// Now all Tracy logs will be sent to Sentry
Debugger::log('Something happened', Debugger::WARNING);

Manual Instantiation

For more control, you can manually create the logger instance:

use Tracy\Debugger;
use Baraja\TracySentryBridge\SentryLogger;

$originalLogger = Debugger::getLogger();
$sentryLogger = new SentryLogger($originalLogger);
Debugger::setLogger($sentryLogger);

🛡️ Error Handling

The package is designed to be fault-tolerant:

  1. Primary logging always works: The fallback (original Tracy) logger is called first, ensuring your local logs are always written.

  2. Sentry failures are captured: If the Sentry logging fails for any reason, the exception is logged to the fallback logger with CRITICAL level.

  3. Graceful degradation: If Sentry SDK is not properly loaded, the package will output a warning message but won't crash your application.

// Example of how errors are handled internally
public function log(mixed $value, mixed $level = self::INFO): void
{
    // Always log to fallback first (guaranteed to work)
    $this->fallback->log($value, $level);

    try {
        // Then attempt Sentry logging
        $this->logToSentry($value, $level);
    } catch (\Throwable $e) {
        // If Sentry fails, log the error locally
        $this->fallback->log($e, ILogger::CRITICAL);
    }
}

⚡ Advanced Configuration

Sentry Configuration Options

When initializing Sentry, you can customize various options:

\Sentry\init([
    'dsn' => 'https://your-sentry-dsn@sentry.io/project-id',
    'attach_stacktrace' => true,
    'environment' => 'production',
    'release' => 'my-app@1.0.0',
    'sample_rate' => 1.0,
    'traces_sample_rate' => 0.2,
    'send_default_pii' => false,
]);

Conditional Registration

You might want to only enable Sentry logging in production:

if (getenv('APP_ENV') === 'production' && \function_exists('\Sentry\init')) {
    \Sentry\init(['dsn' => getenv('SENTRY_DSN')]);
    SentryLogger::register();
}

Logging Different Types

The bridge handles different value types appropriately:

// Exceptions are captured with full stack trace
try {
    throw new \RuntimeException('Something went wrong');
} catch (\Throwable $e) {
    Debugger::log($e, Debugger::EXCEPTION);
}

// String messages are captured as messages
Debugger::log('User login failed', Debugger::WARNING);

// Arrays and objects are serialized
Debugger::log(['user_id' => 123, 'action' => 'failed_login'], Debugger::INFO);

🧪 Development & Testing

The package uses PHPStan for static analysis:

$ composer phpstan

This runs PHPStan at level 8 with strict rules and Nette-specific extensions.

👥 Author

Jan Barasek - https://baraja.cz

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

baraja-core/tracy-sentry-bridge is licensed under the MIT license. See the LICENSE file for more details.

baraja-core/tracy-sentry-bridge 适用场景与选型建议

baraja-core/tracy-sentry-bridge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.74k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 baraja-core/tracy-sentry-bridge 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2021-05-17