-
-
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
Conversation
@krezreb please fix the PHPCS style check first. Referece: https://innocraft.atlassian.net/wiki/spaces/DE/pages/2395570181/Create+a+Github+action+to+check+if+code+formatting+is+as+per+PSR+standard |
{ | ||
$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 comment
The 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.
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) |
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.
public function __construct($logMessageFormat, $allowInlineLineBreaks = true, $excludePatterns = null, $customFunctionFile = null) | |
public function __construct($logMessageFormat, $allowInlineLineBreaks = true, array $excludePatterns = null, string $customFunctionFile = null) |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
with the constructor mandated type of the param, this can just be
if (gettype($this->excludePatterns) === "array") { | |
if (!empty($this->excludePatterns)) { |
} | ||
} | ||
|
||
if ($this->logMessageFormat == 'json') { |
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.
might be a tad safer
if ($this->logMessageFormat == 'json') { | |
if ('json' === $this->logMessageFormat) { |
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.
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 comment
The 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 comment
The 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
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
can we add typehints to the params and a return type?
"message" => $message, | ||
"level" => $record['level_name'], | ||
"trace" => $trace, | ||
"requestId" => $requestId |
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.
a trailing comma is preferred in case a new item is added to minimise git diff on unnecessary lines
"requestId" => $requestId | |
"requestId" => $requestId, |
"requestId" => $requestId | ||
]; | ||
|
||
if (gettype($this->customFunction) === "string") { |
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.
this might check for not being empty and also if it's actually callable
if (gettype($this->customFunction) === "string") { | |
if (is_callable($this->customFunction)) { |
plugins/Monolog/config/config.php
Outdated
$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 comment
The reason will be displayed to describe this comment to others. Learn more.
$xcl = []; | |
foreach (explode("|", $c->get('ini.log.exclude_patterns')) as $p) { | |
$xcl[] = trim($p); | |
} | |
return $xcl; | |
$excl = []; | |
foreach (explode('|', $c->get('ini.log.exclude_patterns')) as $p) { | |
$excl[] = trim($p); | |
} | |
return $excl; |
plugins/Monolog/config/config.php
Outdated
$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 comment
The 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 comment
The 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?
plugins/Monolog/config/config.php
Outdated
'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 comment
The reason will be displayed to describe this comment to others. Learn more.
seems like a duplicated section
'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; | |
}), |
if ($c->has('ini.log.enable_json')) { | ||
return 'json'; | ||
} |
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.
Can we not pass this just via ini.log.string_message_format
below?
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.
@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.enable_json')) { | ||
return 'json'; | ||
} |
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.
Can we not pass it via ini.log.string_message_format_trace
below?
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.
same as above, looks like a dead feature
Description:
Please include a description of this change and which issue it fixes. If no issue exists yet please include context and what problem it solves.
Review
Depends on plugin PR matomo-org/plugin-LogViewer#99
To test json_logs
config/config.ini.php
set the format as json in (see example below)index.php?module=LogViewer
logs should appear correctly in the interfaceTo test exclude_pattens
config/config.ini.php
set one or more exclude_patterns, separated by pipes(see example below)To test custom_function_file
config/config.ini.php
specify custom_function_file (see example below)custom.php