1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupGateway\GatewayBundle\Monolog\Formatter; |
20
|
|
|
|
21
|
|
|
use Monolog\Formatter\GelfMessageFormatter; |
22
|
|
|
use Monolog\Formatter\FormatterInterface; |
23
|
|
|
|
24
|
|
|
class GelfMessageToStringFormatter implements FormatterInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param GelfMessageFormatter $formatter |
28
|
|
|
*/ |
29
|
|
|
public function __construct(GelfMessageFormatter $formatter) |
30
|
|
|
{ |
31
|
|
|
$this->formatter = $formatter; |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} the gelf message is an array which cannot be logged into a single line |
36
|
|
|
* By jsonencoding the message we can write it on a single line |
37
|
|
|
*/ |
38
|
|
|
public function format(array $record) |
39
|
|
|
{ |
40
|
|
|
$message = $this->formatter->format($record); |
41
|
|
|
|
42
|
|
|
// we need to keep the last new line, otherwise everything is appended on the same line :) |
43
|
|
|
$message->setFullMessage(str_replace("\n", ', ', $message->getFullMessage()) . "\n"); |
44
|
|
|
|
45
|
|
|
return json_encode($message->toArray()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Formats a set of log records. |
50
|
|
|
* |
51
|
|
|
* @param array $records A set of records to format |
52
|
|
|
* @return mixed The formatted set of records |
53
|
|
|
*/ |
54
|
|
|
public function formatBatch(array $records) |
55
|
|
|
{ |
56
|
|
|
return array_map([$this, 'format'], $records); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: