Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKate Butler <kate@innocraft.com>2019-10-10 02:34:35 +0300
committerThomas Steur <tsteur@users.noreply.github.com>2019-10-10 02:34:35 +0300
commit3cbd95ebd2dab23546bac80139bce28cd2899d20 (patch)
tree3eac86dd30d5f6987bd663f7df4579c8e1c64cc5 /plugins/Monolog
parent42f9f8cbcc350f9cbcd8a085c38f2ae9155c42d3 (diff)
Log stack traces for PHP warnings etc (#14961)
* Allow stack traces to be logged to file for PHP warnings * Use debug_backtrace() to generate an abbreviated stack trace * Change trace format, fix LogTest * smaller trace by not including unimportant doc root makes it also easier for users to share the trace since they don't need to remove any paths * Update ErrorHandler.php
Diffstat (limited to 'plugins/Monolog')
-rw-r--r--plugins/Monolog/Formatter/LineMessageFormatter.php22
-rw-r--r--plugins/Monolog/config/config.php24
-rw-r--r--plugins/Monolog/tests/Integration/LogTest.php1
3 files changed, 36 insertions, 11 deletions
diff --git a/plugins/Monolog/Formatter/LineMessageFormatter.php b/plugins/Monolog/Formatter/LineMessageFormatter.php
index 306838d34d..f8e1dbc99f 100644
--- a/plugins/Monolog/Formatter/LineMessageFormatter.php
+++ b/plugins/Monolog/Formatter/LineMessageFormatter.php
@@ -61,16 +61,32 @@ class LineMessageFormatter implements FormatterInterface
private function formatMessage($class, $message, $date, $record)
{
+ $trace = isset($record['context']['trace']) ? self::formatTrace($record['context']['trace']) : '';
$message = str_replace(
- array('%tag%', '%message%', '%datetime%', '%level%'),
- array($class, $message, $date, $record['level_name']),
+ array('%tag%', '%message%', '%datetime%', '%level%', '%trace%'),
+ array($class, $message, $date, $record['level_name'], $trace),
$this->logMessageFormat
);
- $message .= "\n";
+ $message = trim($message) . "\n";
return $message;
}
+ private static function formatTrace(array $trace, $numLevels = 10)
+ {
+ $strTrace = '';
+ for ($i = 0; $i < $numLevels; $i++) {
+ $level = $trace[$i];
+ if (isset($level['file'], $level['line'])) {
+ $levelTrace = '#' . $i . (str_replace(PIWIK_DOCUMENT_ROOT, '', $level['file'])) . '(' . $level['line'] . ')';
+ } else {
+ $levelTrace = '[internal function]: ' . $level['class'] . $level['type'] . $level['function'] . '()';
+ }
+ $strTrace .= $levelTrace . ",";
+ }
+ return trim($strTrace, ",");
+ }
+
public function formatBatch(array $records)
{
foreach ($records as $key => $record) {
diff --git a/plugins/Monolog/config/config.php b/plugins/Monolog/config/config.php
index 18e07c5c33..9d48f603cc 100644
--- a/plugins/Monolog/config/config.php
+++ b/plugins/Monolog/config/config.php
@@ -97,16 +97,13 @@ return array(
->constructor(DI\get('log.file.filename'), DI\get('log.level.file'))
->method('setFormatter', DI\get('log.lineMessageFormatter.file')),
- 'log.lineMessageFormatter.file' => DI\object('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')
- ->constructorParameter('allowInlineLineBreaks', false),
-
'Piwik\Plugins\Monolog\Handler\DatabaseHandler' => DI\object()
->constructor(DI\get('log.level.database'))
- ->method('setFormatter', DI\get('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')),
+ ->method('setFormatter', DI\get('log.lineMessageFormatter')),
'Piwik\Plugins\Monolog\Handler\WebNotificationHandler' => DI\object()
->constructor(DI\get('log.level.screen'))
- ->method('setFormatter', DI\get('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')),
+ ->method('setFormatter', DI\get('log.lineMessageFormatter')),
'log.level' => DI\factory(function (ContainerInterface $c) {
if ($c->has('ini.log.log_level')) {
@@ -175,16 +172,27 @@ return array(
return $logPath;
}),
- 'Piwik\Plugins\Monolog\Formatter\LineMessageFormatter' => DI\object()
- ->constructor(DI\get('log.format')),
+ 'log.lineMessageFormatter' => DI\object('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')
+ ->constructor(DI\get('log.short.format')),
- 'log.format' => DI\factory(function (ContainerInterface $c) {
+ 'log.lineMessageFormatter.file' => DI\object('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter')
+ ->constructor(DI\get('log.trace.format'))
+ ->constructorParameter('allowInlineLineBreaks', false),
+
+ 'log.short.format' => DI\factory(function (ContainerInterface $c) {
if ($c->has('ini.log.string_message_format')) {
return $c->get('ini.log.string_message_format');
}
return '%level% %tag%[%datetime%] %message%';
}),
+ 'log.trace.format' => DI\factory(function (ContainerInterface $c) {
+ if ($c->has('ini.log.string_message_format_trace')) {
+ return $c->get('ini.log.string_message_format_trace');
+ }
+ return '%level% %tag%[%datetime%] %message% %trace%';
+ }),
+
'archiving.performance.handlers' => function (ContainerInterface $c) {
$logFile = trim($c->get('ini.Debug.archive_profiling_log'));
if (empty($logFile)) {
diff --git a/plugins/Monolog/tests/Integration/LogTest.php b/plugins/Monolog/tests/Integration/LogTest.php
index 69d84379fc..0f2c32ec84 100644
--- a/plugins/Monolog/tests/Integration/LogTest.php
+++ b/plugins/Monolog/tests/Integration/LogTest.php
@@ -257,6 +257,7 @@ class LogTest extends IntegrationTestCase
'ini.log.log_writers' => array($backend),
'ini.log.log_level' => $level,
'ini.log.string_message_format' => self::STRING_MESSAGE_FORMAT,
+ 'ini.log.string_message_format_trace' => self::STRING_MESSAGE_FORMAT,
'ini.log.logger_file_path' => self::getLogFileLocation(),
'Psr\Log\LoggerInterface' => \DI\get('Monolog\Logger'),
'Tests.log.allowAllHandlers' => true,