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β aTracy\ILoggerimplementation backed by aMonolog\Logger.Detain\TracyHasMono\Processors\TracyExceptionProcessorβ a Monolog processor that renders anyThrowablefound 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.
| Dependency | Version |
|---|---|
| PHP | >=8.1 |
tracy/tracy |
^2.10 |
monolog/monolog |
^3.0 |
nette/di |
^3.0 (only for the Nette DI bridge) |
composer require detain/tracy-monolog-adapterWire 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.
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.
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:
- 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. - Add
tracy_filename(just the basename) andtracy_created(trueif this call wrote the file,falseif it pre-existed) to the record context. - Replace the record message with a human-readable summary of the throwable chain when the original message is empty.
Register the bundled compiler extension in your NEON config:
extensions:
monolog: Detain\TracyHasMono\Bridges\NetteDI\MonologExtensionBy default this registers:
monolog.handlerβMonolog\Handler\RotatingFileHandlerpointing at%logDir%/nette.logmonolog.tracyExceptionProcessorβTracyExceptionProcessorpointing at%logDir%monolog.monologLoggerβMonolog\Loggerchannelnettewith the handler and processor pre-pushedmonolog.tracyLoggerβTracyMonoLoggerwrapping the above and replacing the autowiredtracy.loggerservice
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: @myMonologLoggersrc/
βββ 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\β coversLogger.phponly, kept as a drop-in replacement for the original Nextras package.
New BSD License. See the license file for details.
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.