-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathStackdriverHandler.php
105 lines (91 loc) · 3.3 KB
/
StackdriverHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace CodeInternetApplications\MonologStackdriver;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Google\Cloud\Logging\LoggingClient;
use Monolog\Handler\AbstractProcessingHandler;
/**
* StackdriverHandler
*
* @author Martin van Dam <[email protected]>
* @author Wouter Monkhorst <[email protected]>
*/
class StackdriverHandler extends AbstractProcessingHandler
{
/**
* The Stackdriver logger
* @var Google\Cloud\Logging\Logger
*/
private $logger;
/**
* A context array key used to take log entry options from
* @var string
*/
private $entryOptionsWrapper;
/**
* Log entry options (all but severity) as supported by Google\Cloud\Logging\Logger::entry
* @var array Entry options.
*/
private $entryOptions = [
'resource',
'httpRequest',
'labels',
'operation',
'insertId',
'timestamp',
'logName'
];
/**
* @param string $logName Name of your log
* @param array $loggingClientOptions Google\Cloud\Logging\LoggingClient valid options
* @param array $loggerOptions Google\Cloud\Logging\LoggingClient::logger valid options
* @param string $entryOptionsWrapper Array key used in the context array to take Google\Cloud\Logging\Entry options from
* @param string $lineFormat Monolog\Formatter\LineFormatter format
* @param int $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct($logName, $loggingClientOptions = [], $loggerOptions = [], $entryOptionsWrapper = 'stackdriver', $lineFormat = '%message%', $level = Logger::DEBUG, $bubble = true)
{
parent::__construct($level, $bubble);
$this->logger = (new LoggingClient($loggingClientOptions))->logger($logName, $loggerOptions);
$this->formatter = new LineFormatter($lineFormat);
$this->entryOptionsWrapper = $entryOptionsWrapper;
}
/**
* Writes the record down to the log
*
* @param array $record
* @return void
*/
protected function write(array $record): void
{
$options = $this->getOptionsFromRecord($record);
$data = [
'message' => $record['formatted'],
'data' => $record['context']
];
$entry = $this->logger->entry($data, $options);
$this->logger->write($entry);
}
/**
* Get the Google\Cloud\Logging\Entry options
*
* @param array $record by reference
* @return array $options
*/
private function getOptionsFromRecord(array &$record): array
{
$options = [
'severity' => $record['level_name']
];
if (isset($record['context'][$this->entryOptionsWrapper])) {
foreach ($this->entryOptions as $entryOption) {
if ($record['context'][$this->entryOptionsWrapper][$entryOption] ?? false) {
$options[$entryOption] = $record['context'][$this->entryOptionsWrapper][$entryOption];
}
}
unset($record['context'][$this->entryOptionsWrapper]);
}
return $options;
}
}