StashFormatter   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.35%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 49
rs 10
c 1
b 0
f 1
ccs 28
cts 34
cp 0.8235
wmc 12
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D formatV1() 0 36 11
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2017
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      https://fastdlabs.com
8
 */
9
10
namespace FastD\Logger\Formatter;
11
12
use Monolog\Formatter\LogstashFormatter;
13
14
/**
15
 * Class StashFormatter.
16
 */
17
class StashFormatter extends LogstashFormatter
18
{
19 32
    public function __construct()
20
    {
21 32
        parent::__construct(app()->getName(), get_local_ip(), null, null, self::V1);
22 32
    }
23
24
    /**
25
     * @param array $record
26
     *
27
     * @return array
28
     */
29 10
    public function formatV1(array $record)
30
    {
31 10
        if (empty($record['datetime'])) {
32
            $record['datetime'] = gmdate('c');
33
        }
34
        $message = array(
35 10
            '@timestamp' => $record['datetime'],
36 10
            '@version' => version(),
37 10
            'host' => $this->systemName,
38 10
        );
39 10
        if (isset($record['message'])) {
40 10
            $message['message'] = $record['message'];
41 10
        }
42 10
        if (isset($record['channel'])) {
43 10
            $message['channel'] = $record['channel'];
44 10
        }
45 10
        if (isset($record['level_name'])) {
46 10
            $message['level'] = $record['level_name'];
47 10
        }
48 10
        if (!empty($record['extra'])) {
49
            foreach ($record['extra'] as $key => $val) {
50
                $message[$this->extraPrefix.$key] = $val;
51
            }
52
        }
53 10
        if (!empty($record['context'])) {
54 9
            foreach ($record['context'] as $key => $val) {
55 9
                $message[$this->contextPrefix.$key] = $val;
56 9
            }
57 9
        }
58
59 10
        if (isset($message['trace']) && is_array($message['trace'])) {
60 3
            $message['trace'] = implode(PHP_EOL, $message['trace']);
61 3
        }
62
63 10
        return $message;
64
    }
65
}
66