GelfMessageToStringFormatter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2015 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\FormatterInterface;
22
use Monolog\Formatter\GelfMessageFormatter;
23
use Monolog\LogRecord;
24
25
class GelfMessageToStringFormatter implements FormatterInterface
26
{
27
    /**
28
     * @var GelfMessageFormatter
29
     */
30
    private $formatter;
31
32
    /**
33
     * @param GelfMessageFormatter $formatter
34
     */
35
    public function __construct(GelfMessageFormatter $formatter)
36
    {
37
        $this->formatter = $formatter;
38
    }
39
40
    /**
41
     * {@inheritdoc} the gelf message is an array which cannot be logged into a single line
42
     * By jsonencoding the message we can write it on a single line
43
     */
44
    public function format(array|LogRecord $record)
45
    {
46
        $message = $this->formatter->format($record);
0 ignored issues
show
Bug introduced by
$record of type array is incompatible with the type Monolog\LogRecord expected by parameter $record of Monolog\Formatter\GelfMessageFormatter::format(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        $message = $this->formatter->format(/** @scrutinizer ignore-type */ $record);
Loading history...
47
48
        // we need to keep the last new line, otherwise everything is appended on the same line :)
49
        $message->setFullMessage(str_replace("\n", ', ', $message->getFullMessage()) . "\n");
50
51
        return json_encode($message->toArray());
52
    }
53
54
    /**
55
     * Formats a set of log records.
56
     *
57
     * @param  array $records A set of records to format
58
     * @return mixed The formatted set of records
59
     */
60
    public function formatBatch(array $records)
61
    {
62
        return array_map([$this, 'format'], $records);
63
    }
64
}
65