HtmlFormatter should have option to include traces
tomsommer opened this issue · comments
Just like LineFormatter
, HtmlFormatter
should have the option to include the full ->getTraceAsString
. Perhaps even build it in to NormalizerFormatter
to make sure you can get the full trace details included (provided function-arguments etc.).
You should already get the trace in html output if you pass the exception like ['exception' => $e]
as context data. Can you not reproduce this?
<?php
use Monolog\Formatter\HtmlFormatter;
use Monolog\Handler\StreamHandler;
include 'vendor/autoload.php';
$m = new Monolog\Logger('foo');
$m->pushHandler($h = new StreamHandler('php://stdout'));
$h->setFormatter(new HtmlFormatter());
function foo ($m) {
$m->error('TEST', ['exception' => new RuntimeException('This is a test')]);
}
foo($m);
$ php test.php
<h1 style="background: #FD7E14;color: #ffffff;padding: 5px;" class="monolog-output">ERROR</h1><table cellspacing="1" width="100%" class="monolog-output"><tr style="padding: 4px;text-align: left;">
<th style="vertical-align: top;background: #ccc;color: #000" width="100">Message:</th>
<td style="padding: 4px;text-align: left;vertical-align: top;background: #eee;color: #000"><pre>TEST</pre></td>
</tr><tr style="padding: 4px;text-align: left;">
<th style="vertical-align: top;background: #ccc;color: #000" width="100">Time:</th>
<td style="padding: 4px;text-align: left;vertical-align: top;background: #eee;color: #000"><pre>2021-01-27T16:47:50.166097+01:00</pre></td>
</tr><tr style="padding: 4px;text-align: left;">
<th style="vertical-align: top;background: #ccc;color: #000" width="100">Channel:</th>
<td style="padding: 4px;text-align: left;vertical-align: top;background: #eee;color: #000"><pre>foo</pre></td>
</tr><tr style="padding: 4px;text-align: left;">
<th style="vertical-align: top;background: #ccc;color: #000" width="100">Context:</th>
<td style="padding: 4px;text-align: left;vertical-align: top;background: #eee;color: #000"><table cellspacing="1" width="100%"><tr style="padding: 4px;text-align: left;">
<th style="vertical-align: top;background: #ccc;color: #000" width="100">exception:</th>
<td style="padding: 4px;text-align: left;vertical-align: top;background: #eee;color: #000"><pre>{
"class": "RuntimeException",
"message": "This is a test",
"code": 0,
"file": "/var/www/transient/monolog/test.php:13",
"trace": [
"/var/www/transient/monolog/test.php:16"
]
}</pre></td>
</tr></table></td>
</tr></table>
See the trace at the bottom there.
\Monolog\ErrorHandler
does not do this, thus does not provide traces to HTML mails.
Also the trace does not include function-arguments etc.
function foo(string $foo) {
throw new \RuntimeException('foobar');
}
foo('this data is not to be found');
Yeah errorhandler does not do this for errors as they are not exceptions and thus have no stack traces attached.
But for uncaught exceptions it will log them correctly.
And function args are not logged that is correct, as this tends to result in insanely large traces, so isn't fit for prod use IMO.