Skip to content
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

Open
wants to merge 12 commits into
base: 5.x-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions plugins/Monolog/Formatter/LineMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function __construct($logMessageFormat, $allowInlineLineBreaks = true, $excludePatterns = null, $customFunctionFile = null)
public function __construct($logMessageFormat, $allowInlineLineBreaks = true, array $excludePatterns = null, string $customFunctionFile = null)

{
$this->logMessageFormat = $logMessageFormat;
$this->allowInlineLineBreaks = $allowInlineLineBreaks;
$this->excludePatterns = $excludePatterns;
if (gettype($customFunctionFile) === "string") {
Copy link
Contributor

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.

$this->customFunction = include_once $customFunctionFile;
}
}

public function format(array $record)
Expand All @@ -43,6 +48,21 @@ public function format(array $record)

$message = trim($record['message']);

if (gettype($this->excludePatterns) === "array") {
Copy link
Contributor

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

Suggested change
if (gettype($this->excludePatterns) === "array") {
if (!empty($this->excludePatterns)) {

foreach ($this->excludePatterns as $p) {
if (strpos($message, $p) !== false) {
return;
}
if (strpos($class, $p) !== false) {
return;
}
}
}

if ($this->logMessageFormat == 'json') {
Copy link
Contributor

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

Suggested change
if ($this->logMessageFormat == 'json') {
if ('json' === $this->logMessageFormat) {

Copy link
Author

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?

Copy link
Contributor

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.

Copy link
Author

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

return $this->jsonMessage($class, $message, $date, $record);
}

if ($this->allowInlineLineBreaks) {
$message = str_replace("\n", "\n ", $message); // intend lines
$messages = array($message);
Expand All @@ -62,6 +82,27 @@ public function format(array $record)
return $total;
}

private function jsonMessage($class, $message, $date, $record)
Copy link
Contributor

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?

{
$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
Copy link
Contributor

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

Suggested change
"requestId" => $requestId
"requestId" => $requestId,

];

if (gettype($this->customFunction) === "string") {
Copy link
Contributor

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

Suggested change
if (gettype($this->customFunction) === "string") {
if (is_callable($this->customFunction)) {

$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']) : '';
Expand Down
53 changes: 51 additions & 2 deletions plugins/Monolog/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$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;

}
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);
Copy link
Contributor

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.

Copy link
Author

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?

}

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;
}),
Copy link
Contributor

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

Suggested change
'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;
}),


'log.short.format' => Piwik\DI::factory(function (Container $c) {
if ($c->has('ini.log.enable_json')) {
return 'json';
}
Comment on lines +267 to +269
Copy link
Contributor

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?

Copy link
Author

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.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
Copy link
Contributor

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?

Copy link
Author

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

if ($c->has('ini.log.string_message_format_trace')) {
return $c->get('ini.log.string_message_format_trace');
}
Expand Down
Loading