-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dev json logs #22994
base: 5.x-dev
Are you sure you want to change the base?
Dev json logs #22994
Changes from 6 commits
55d1f1b
882f41c
4ad7b33
19852de
b93f56f
718d399
a5201fa
e59eeda
f572fd4
8f121ca
70d884b
66c096f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,17 +23,22 @@ class LineMessageFormatter implements FormatterInterface | |||||
* @var string | ||||||
*/ | ||||||
private $logMessageFormat; | ||||||
|
||||||
private $excludePatterns; | ||||||
private $customFunction; | ||||||
private $allowInlineLineBreaks; | ||||||
|
||||||
/** | ||||||
* @param string $logMessageFormat | ||||||
* @param bool $allowInlineLineBreaks If disabled, a log message will be created for each line | ||||||
*/ | ||||||
public function __construct($logMessageFormat, $allowInlineLineBreaks = true) | ||||||
public function __construct($logMessageFormat, $allowInlineLineBreaks = true, $excludePatterns = null, $customFunctionFile = null) | ||||||
{ | ||||||
$this->logMessageFormat = $logMessageFormat; | ||||||
$this->allowInlineLineBreaks = $allowInlineLineBreaks; | ||||||
$this->excludePatterns = $excludePatterns; | ||||||
if (gettype($customFunctionFile) === "string") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can add typehint to the constructor param and don't need to check for it being a string, just not empty. |
||||||
$this->customFunction = include_once $customFunctionFile; | ||||||
} | ||||||
} | ||||||
|
||||||
public function format(array $record) | ||||||
|
@@ -43,6 +48,21 @@ public function format(array $record) | |||||
|
||||||
$message = trim($record['message']); | ||||||
|
||||||
if (gettype($this->excludePatterns) === "array") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with the constructor mandated type of the param, this can just be
Suggested change
|
||||||
foreach ($this->excludePatterns as $p) { | ||||||
if (strpos($message, $p) !== false) { | ||||||
return; | ||||||
} | ||||||
if (strpos($class, $p) !== false) { | ||||||
return; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
if ($this->logMessageFormat == 'json') { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be a tad safer
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree about the === but is there a reason to put json first? legibility? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's the Yoda condition style, in general preventing situations when someone forgets an equal sign and accidentally assigns the value to the variable instead of doing a comparison. With the string or number first that can't happen. It's not a strict thing we check for in CS yet but might be at some point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michalkleiner hmm..... first time I've seen this, it is. Modify the line, I will |
||||||
return $this->jsonMessage($class, $message, $date, $record); | ||||||
} | ||||||
|
||||||
if ($this->allowInlineLineBreaks) { | ||||||
$message = str_replace("\n", "\n ", $message); // intend lines | ||||||
$messages = array($message); | ||||||
|
@@ -62,6 +82,27 @@ public function format(array $record) | |||||
return $total; | ||||||
} | ||||||
|
||||||
private function jsonMessage($class, $message, $date, $record) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add typehints to the params and a return type? |
||||||
{ | ||||||
$trace = isset($record['context']['trace']) ? self::formatTrace($record['context']['trace']) : ''; | ||||||
$requestId = isset($record['extra']['request_id']) ? $record['extra']['request_id'] : ''; | ||||||
|
||||||
$message = [ | ||||||
"tag" => $class, | ||||||
"datetime" => $date, | ||||||
"message" => $message, | ||||||
"level" => $record['level_name'], | ||||||
"trace" => $trace, | ||||||
"requestId" => $requestId | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a trailing comma is preferred in case a new item is added to minimise git diff on unnecessary lines
Suggested change
|
||||||
]; | ||||||
|
||||||
if (gettype($this->customFunction) === "string") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might check for not being empty and also if it's actually callable
Suggested change
|
||||||
$message = call_user_func($this->customFunction, $message); | ||||||
} | ||||||
|
||||||
return json_encode($message) . "\n"; | ||||||
} | ||||||
|
||||||
private function formatMessage($class, $message, $date, $record) | ||||||
{ | ||||||
$trace = isset($record['context']['trace']) ? self::formatTrace($record['context']['trace']) : ''; | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ | |||||||||||||||||||||
|
||||||||||||||||||||||
'log.handler.classes' => array( | ||||||||||||||||||||||
'file' => 'Piwik\Plugins\Monolog\Handler\FileHandler', | ||||||||||||||||||||||
'jsonfile' => 'Piwik\Plugins\Monolog\Handler\FileHandler', | ||||||||||||||||||||||
'screen' => 'Piwik\Plugins\Monolog\Handler\WebNotificationHandler', | ||||||||||||||||||||||
'database' => 'Piwik\Plugins\Monolog\Handler\DatabaseHandler', | ||||||||||||||||||||||
'errorlog' => 'Piwik\Plugins\Monolog\Handler\ErrorLogHandler', | ||||||||||||||||||||||
|
@@ -226,22 +227,70 @@ | |||||||||||||||||||||
}), | ||||||||||||||||||||||
|
||||||||||||||||||||||
'Piwik\Plugins\Monolog\Formatter\LineMessageFormatter' => Piwik\DI::create('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter') | ||||||||||||||||||||||
->constructor(Piwik\DI::get('log.short.format')), | ||||||||||||||||||||||
->constructor(Piwik\DI::get('log.short.format')), | ||||||||||||||||||||||
'log.lineMessageFormatter' => Piwik\DI::create('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter') | ||||||||||||||||||||||
->constructor(Piwik\DI::get('log.short.format')), | ||||||||||||||||||||||
|
||||||||||||||||||||||
'log.lineMessageFormatter.file' => Piwik\DI::autowire('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter') | ||||||||||||||||||||||
->constructor(Piwik\DI::get('log.trace.format')) | ||||||||||||||||||||||
->constructorParameter('allowInlineLineBreaks', false), | ||||||||||||||||||||||
->constructorParameter('allowInlineLineBreaks', false) | ||||||||||||||||||||||
->constructorParameter('customFunctionFile', Piwik\DI::get('log.custom_function_file')) | ||||||||||||||||||||||
->constructorParameter('excludePatterns', Piwik\DI::get('log.exclude_patterns')), | ||||||||||||||||||||||
|
||||||||||||||||||||||
'log.exclude_patterns' => Piwik\DI::factory(function (Container $c) { | ||||||||||||||||||||||
if ($c->has('ini.log.exclude_patterns')) { | ||||||||||||||||||||||
$xcl = []; | ||||||||||||||||||||||
foreach (explode("|", $c->get('ini.log.exclude_patterns')) as $p) { | ||||||||||||||||||||||
$xcl[] = trim($p); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return $xcl; | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
} | ||||||||||||||||||||||
return null; | ||||||||||||||||||||||
}), | ||||||||||||||||||||||
|
||||||||||||||||||||||
'log.custom_function_file' => Piwik\DI::factory(function (Container $c) { | ||||||||||||||||||||||
if ($c->has('ini.log.custom_function_file')) { | ||||||||||||||||||||||
$path = $c->get('ini.log.custom_function_file'); | ||||||||||||||||||||||
if (!file_exists($path)) { | ||||||||||||||||||||||
return null; | ||||||||||||||||||||||
throw new \Exception('Specified path to custom_function_file does not exist: ' . $path); | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wont' be thrown if we return above it. Similar for the readability check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michalkleiner oh yeah that was test code I forgot to go back over. What do you think / what is our general philosophy in this case? Should the code fail silently or throw an exception and break logging? |
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (!is_readable($path)) { | ||||||||||||||||||||||
return null; | ||||||||||||||||||||||
throw new \Exception('Specified path to custom_function_file file is not readable: ' . $path); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
return $path; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return null; | ||||||||||||||||||||||
}), | ||||||||||||||||||||||
|
||||||||||||||||||||||
'log.exclude_patterns' => Piwik\DI::factory(function (Container $c) { | ||||||||||||||||||||||
if ($c->has('ini.log.exclude_patterns')) { | ||||||||||||||||||||||
$xcl = []; | ||||||||||||||||||||||
foreach (explode("|", $c->get('ini.log.exclude_patterns')) as $p) { | ||||||||||||||||||||||
$xcl[] = trim($p); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return $xcl; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return null; | ||||||||||||||||||||||
}), | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like a duplicated section
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
'log.short.format' => Piwik\DI::factory(function (Container $c) { | ||||||||||||||||||||||
if ($c->has('ini.log.enable_json')) { | ||||||||||||||||||||||
return 'json'; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
Comment on lines
+267
to
+269
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not pass this just via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michalkleiner I considered that but I found adding a new option is probably easier for users to understand. Also, in the viewer code https://github.com/matomo-org/plugin-LogViewer/blob/5a7687b3591566aa5cdd1235241e06fa9891415b/Log/Parser/Piwik.php#L25 doesn't use the format so might be a dead feature anyway |
||||||||||||||||||||||
if ($c->has('ini.log.string_message_format')) { | ||||||||||||||||||||||
return $c->get('ini.log.string_message_format'); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return '%level% %tag%[%datetime%] %message%'; | ||||||||||||||||||||||
}), | ||||||||||||||||||||||
|
||||||||||||||||||||||
'log.trace.format' => Piwik\DI::factory(function (Container $c) { | ||||||||||||||||||||||
if ($c->has('ini.log.enable_json')) { | ||||||||||||||||||||||
return 'json'; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
Comment on lines
+277
to
+279
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not pass it via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above, looks like a dead feature |
||||||||||||||||||||||
if ($c->has('ini.log.string_message_format_trace')) { | ||||||||||||||||||||||
return $c->get('ini.log.string_message_format_trace'); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.