Completed
Push — logging-config-new-dev-vm ( 9273b8...96cac7 )
by
unknown
03:06
created

GelfMessageToStringFormatter::formatBatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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;
0 ignored issues
show
Bug introduced by
The property formatter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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