-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The GelfMessageToStringFormatter is used from other steup projects. It logs in json format to syslog.
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Surfnet/AzureMfa/Infrastructure/Monolog/Formatter/GelfMessageToStringFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* Copyright 2020 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Surfnet\AzureMfa\Infrastructure\Monolog\Formatter; | ||
|
||
|
||
use Monolog\Formatter\FormatterInterface; | ||
use Monolog\Formatter\GelfMessageFormatter; | ||
|
||
class GelfMessageToStringFormatter implements FormatterInterface | ||
{ | ||
/** | ||
* @var GelfMessageFormatter | ||
*/ | ||
private $formatter; | ||
|
||
/** | ||
* @param GelfMessageFormatter $formatter | ||
*/ | ||
public function __construct(GelfMessageFormatter $formatter) | ||
{ | ||
$this->formatter = $formatter; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} the gelf message is an array which cannot be logged into a single line | ||
* By jsonencoding the message we can write it on a single line | ||
*/ | ||
public function format(array $record): string | ||
{ | ||
$message = $this->formatter->format($record); | ||
|
||
// we need to keep the last new line, otherwise everything is appended on the same line :) | ||
$message->setFullMessage(str_replace("\n", ', ', $message->getFullMessage()) . "\n"); | ||
|
||
return json_encode($message->toArray()); | ||
} | ||
|
||
/** | ||
* Formats a set of log records. | ||
* | ||
* @param array $records A set of records to format | ||
* @return mixed The formatted set of records | ||
*/ | ||
public function formatBatch(array $records): array | ||
{ | ||
return array_map([$this, 'format'], $records); | ||
} | ||
} |