定制 detain/tracy-monolog-adapter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

detain/tracy-monolog-adapter

Composer 安装命令:

composer require detain/tracy-monolog-adapter

包简介

Bridges the Tracy debugger with the Monolog logger so Tracy log output is routed through Monolog handlers and processors.

README 文档

README

Downloads this Month Stable version License

A small bridge that lets the Tracy debugger log through a Monolog logger so you can route Tracy's log calls (warnings, errors, exceptions) through any Monolog handler, processor or formatter you already use.

Includes:

  • Detain\TracyHasMono\TracyMonoLogger — a Tracy\ILogger implementation backed by a Monolog\Logger.
  • Detain\TracyHasMono\Processors\TracyExceptionProcessor — a Monolog processor that renders any Throwable found in a record's context as a Tracy BlueScreen HTML file.
  • Detain\TracyHasMono\Bridges\NetteDI\MonologExtension — an optional Nette DI compiler extension that wires everything into a container.
  • Nextras\TracyMonologAdapter\Logger — a legacy alias kept for backwards compatibility with code that still imports the old namespace.

Requirements

Dependency Version
PHP >=8.1
tracy/tracy ^2.10
monolog/monolog ^3.0
nette/di ^3.0 (only for the Nette DI bridge)

Installation

composer require detain/tracy-monolog-adapter

Usage

Plain PHP

Wire a Monolog logger and pass it to TracyMonoLogger, then register the result with Tracy's Debugger.

use Detain\TracyHasMono\Processors\TracyExceptionProcessor;
use Detain\TracyHasMono\TracyMonoLogger;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Tracy\BlueScreen;
use Tracy\Debugger;

require __DIR__ . '/vendor/autoload.php';

$logDir = __DIR__ . '/log';

Debugger::enable(Debugger::PRODUCTION, $logDir);

$monolog = new Logger('app');
$monolog->pushHandler(new RotatingFileHandler($logDir . '/app.log'));
$monolog->pushProcessor(new TracyExceptionProcessor($logDir, new BlueScreen()));

Debugger::setLogger(new TracyMonoLogger($monolog));

After this, every call Tracy makes to its ILogger (warnings, errors, uncaught exceptions, manual Debugger::log() calls) is routed through your Monolog stack.

Priority mapping

TracyMonoLogger maps Tracy priority strings to Monolog Level cases:

Tracy constant Monolog Level
ILogger::DEBUG Level::Debug
ILogger::INFO Level::Info
ILogger::WARNING Level::Warning
ILogger::ERROR Level::Error
ILogger::EXCEPTION Level::Critical
ILogger::CRITICAL Level::Critical

Unknown priorities fall back to Level::Error.

Throwable handling

When Debugger::log() is called with a Throwable, the throwable is moved into the Monolog record context under the exception key. If you have pushed TracyExceptionProcessor onto your logger, that processor will:

  1. Render the throwable to a Tracy BlueScreen HTML file in the configured log directory (file name exception--Y-m-d--H-i--{hash}.html), reusing an existing file if one for the same hash is already present.
  2. Add tracy_filename (just the basename) and tracy_created (true if this call wrote the file, false if it pre-existed) to the record context.
  3. Replace the record message with a human-readable summary of the throwable chain when the original message is empty.

Nette DI

Register the bundled compiler extension in your NEON config:

extensions:
    monolog: Detain\TracyHasMono\Bridges\NetteDI\MonologExtension

By default this registers:

  • monolog.handlerMonolog\Handler\RotatingFileHandler pointing at %logDir%/nette.log
  • monolog.tracyExceptionProcessorTracyExceptionProcessor pointing at %logDir%
  • monolog.monologLoggerMonolog\Logger channel nette with the handler and processor pre-pushed
  • monolog.tracyLoggerTracyMonoLogger wrapping the above and replacing the autowired tracy.logger service

After compilation the extension also injects \Tracy\Debugger::setLogger($this->getByType(\Tracy\ILogger::class)) into the container's initialize() method, so Tracy itself begins routing through Monolog as soon as the container boots.

To reuse an existing Monolog logger service instead of letting the extension create one, pass it via the extension config:

monolog:
    monolog: @myMonologLogger

Architecture

src/
├── Bridges/NetteDI/MonologExtension.php   # Nette DI compiler extension
├── Logger.php                             # Legacy Nextras\TracyMonologAdapter alias
├── Processors/TracyExceptionProcessor.php # Monolog processor → BlueScreen HTML
└── TracyMonoLogger.php                    # Tracy ILogger → Monolog adapter

Two namespaces are exposed:

  • Detain\TracyHasMono\ — the active namespace; use this in new code.
  • Nextras\TracyMonologAdapter\ — covers Logger.php only, kept as a drop-in replacement for the original Nextras package.

License

New BSD License. See the license file for details.

Credits

Originally based on the Nextras Tracy-Monolog Adapter by the Nextras contributors, with portions (BlueScreen rendering, exception file naming, exception summary formatting) adapted from Nette Tracy by David Grudl. Maintained for the Detain project by the Detain Project contributors.

detain/tracy-monolog-adapter 适用场景与选型建议

detain/tracy-monolog-adapter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.73k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2018-08-15