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
|
37 |
|
public function __construct() |
20
|
|
|
{ |
21
|
37 |
|
parent::__construct(app()->getName(), get_local_ip(), null, null, self::V1); |
22
|
37 |
|
} |
23
|
|
|
|
24
|
16 |
|
public function formatV1(array $record) |
25
|
|
|
{ |
26
|
16 |
|
if (empty($record['datetime'])) { |
27
|
|
|
$record['datetime'] = gmdate('c'); |
28
|
|
|
} |
29
|
|
|
$message = array( |
30
|
16 |
|
'@timestamp' => $record['datetime'], |
31
|
16 |
|
'@version' => version(), |
32
|
16 |
|
'host' => $this->systemName, |
33
|
16 |
|
); |
34
|
16 |
|
if (isset($record['message'])) { |
35
|
16 |
|
$message['message'] = $record['message']; |
36
|
16 |
|
} |
37
|
16 |
|
if (isset($record['channel'])) { |
38
|
16 |
|
$message['channel'] = $record['channel']; |
39
|
16 |
|
} |
40
|
16 |
|
if (isset($record['level_name'])) { |
41
|
16 |
|
$message['level'] = $record['level_name']; |
42
|
16 |
|
} |
43
|
16 |
|
if (!empty($record['extra'])) { |
44
|
|
|
foreach ($record['extra'] as $key => $val) { |
45
|
|
|
$message[$this->extraPrefix.$key] = $val; |
46
|
|
|
} |
47
|
|
|
} |
48
|
16 |
|
if (!empty($record['context'])) { |
49
|
15 |
|
foreach ($record['context'] as $key => $val) { |
50
|
15 |
|
$message[$this->contextPrefix.$key] = $val; |
51
|
15 |
|
} |
52
|
15 |
|
} |
53
|
|
|
|
54
|
16 |
|
if (isset($message['trace']) && is_array($message['trace'])) { |
55
|
12 |
|
$message['trace'] = implode(PHP_EOL, $message['trace']); |
56
|
12 |
|
} |
57
|
|
|
|
58
|
16 |
|
return $message; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|