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 all 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
53 changes: 49 additions & 4 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, bool $allowInlineLineBreaks = true, ?array $excludePatterns = null, ?string $customFunctionFile = null)
{
$this->logMessageFormat = $logMessageFormat;
$this->allowInlineLineBreaks = $allowInlineLineBreaks;
$this->excludePatterns = $excludePatterns;
if ($customFunctionFile !== null) {
$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 ($this->excludePatterns !== null) {
foreach ($this->excludePatterns as $p) {
if (strpos($message, $p) !== false) {
return;
}
if (strpos($class, $p) !== false) {
return;
}
}
}

if ('json' === $this->logMessageFormat) {
return $this->jsonMessage($class, $message, $date, $record);
}

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

private function formatMessage($class, $message, $date, $record)
private function jsonMessage(string $class, string $message, string $date, array $record): ?string
{
$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,
];

if (is_callable($this->customFunction)) {
$message = call_user_func($this->customFunction, $message);
if ($message === null) {
# allow for custom function to filter out messages by returning null
return null;
}
}

return json_encode($message) . "\n";
}

private function formatMessage(string $class, string $message, string $date, array $record): ?string
{
$trace = isset($record['context']['trace']) ? self::formatTrace($record['context']['trace']) : '';
$message = str_replace(
Expand All @@ -75,7 +120,7 @@ private function formatMessage($class, $message, $date, $record)
return $message;
}

private static function formatTrace(array $trace, $numLevels = 10)
private static function formatTrace(array $trace, int $numLevels = 10): string
{
$strTrace = '';
for ($i = 0; $i < $numLevels; $i++) {
Expand Down
39 changes: 37 additions & 2 deletions plugins/Monolog/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,57 @@
}),

'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.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;
}

if (!is_readable($path)) {
return null;
}

return $path;
}
return null;
}),

'log.exclude_patterns' => Piwik\DI::factory(function (Container $c) {
if ($c->has('ini.log.exclude_patterns')) {
$excl = [];
foreach (explode("|", $c->get('ini.log.exclude_patterns')) as $p) {
$excl[] = trim($p);
}
return $excl;
}
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