Repository navigation
Expand file tree
/
Copy pathPsrLogger.php
More file actions
560 lines (521 loc) · 17.5 KB
/
Copy pathPsrLogger.php
File metadata and controls
560 lines (521 loc) · 17.5 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Logging;
use Google\Cloud\Core\Batch\BatchRunner;
use Google\Cloud\Core\Batch\BatchTrait;
use Google\Cloud\Core\Batch\ClosureSerializerInterface;
use Google\Cloud\Core\Report\MetadataProviderInterface;
use Google\Cloud\Core\Report\MetadataProviderUtils;
use Google\Cloud\Core\Timestamp;
use Monolog\Formatter\NormalizerFormatter;
use Monolog\Processor\PsrLogMessageProcessor;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
/**
* A PSR-3 compliant logger used to write entries to Google Stackdriver Logging.
*
* Example:
* ```
* use Google\Cloud\Logging\LoggingClient;
*
* $logging = new LoggingClient();
*
* $psrLogger = $logging->psrLogger('my-log');
* ```
*
* ```
* // Write entries with background batching.
* use Google\Cloud\Logging\LoggingClient;
*
* $logging = new LoggingClient();
*
* $psrLogger = $logging->psrLogger('my-log', [
* 'batchEnabled' => true
* ]);
* ```
*
* @see http://www.php-fig.org/psr/psr-3/#psrlogloggerinterface Psr\Log\LoggerInterface
*/
class PsrLogger implements LoggerInterface, \Serializable
{
use BatchTrait;
const ID_TEMPLATE = 'stackdriver-logging-%s';
/**
* @var array
*/
private static $loggers = [];
/**
* @var Logger The logger used to write entries.
*/
protected $logger;
/**
* @var string The key to use for messages logged in the `jsonPayload`.
*/
private $messageKey;
/**
* @var bool
*/
private $batchEnabled;
/**
* @var MetadataProviderInterface
*/
private $metadataProvider;
/**
* @var string
*/
private $logName;
/**
* @param Logger $logger The logger used to write entries.
* @param string $messageKey The key in the `jsonPayload` used to contain
* the logged message. **Defaults to** `message`.
* @param array $options [optional] {
* Configuration options.
*
* @type MetadataProviderInterface $metadataProvider **Defaults to** An
* automatically chosen provider, based on detected environment
* settings.
* @type bool $batchEnabled Determines whether or not to use background
* batching. **Defaults to** `false`. Note that this option is
* currently considered **experimental** and is subject to change.
* @type resource $debugOutputResource A resource to output debug output
* to.
* @type bool $debugOutput Whether or not to output debug information.
* Please note debug output currently only applies in CLI based
* applications. **Defaults to** `false`. Applies only when
* `batchEnabled` is set to `true`.
* @type array $batchOptions A set of options for a BatchJob.
* {@see \Google\Cloud\Core\Batch\BatchJob::__construct()} for
* more details.
* **Defaults to** ['batchSize' => 1000,
* 'callPeriod' => 2.0,
* 'numWorkers' => 2]. Applies only when
* `batchEnabled` is set to `true`.
* @type array $clientConfig Configuration options for the Logging client
* used to handle processing of batch items. For valid options
* please see
* {@see \Google\Cloud\Logging\LoggingClient::__construct()}.
* Applies only when `batchEnabled` is set to `true`.
* @type BatchRunner $batchRunner A BatchRunner object. Mainly used for
* the tests to inject a mock. **Defaults to** a newly created
* BatchRunner. Applies only when `batchEnabled` is set to `true`.
* @type ClosureSerializerInterface $closureSerializer An implementation
* responsible for serializing closures used in the
* `$clientConfig`. This is especially important when using the
* batch daemon. **Defaults to**
* {@see Google\Cloud\Core\Batch\OpisClosureSerializer} if the
* `opis/closure` library is installed.
* }
*/
public function __construct(
Logger $logger,
$messageKey = null,
array $options = []
) {
$this->logger = $logger;
$this->logName = $logger->name();
$this->messageKey = $messageKey ?: 'message';
$this->metadataProvider = isset($options['metadataProvider'])
? $options['metadataProvider']
: MetadataProviderUtils::autoSelect($_SERVER);
if (isset($options['batchEnabled']) && $options['batchEnabled'] === true) {
$this->batchEnabled = true;
$this->setCommonBatchProperties($options + [
'identifier' => sprintf(self::ID_TEMPLATE, $this->logger->name()),
'batchMethod' => 'writeBatch'
]);
}
}
/**
* Log an emergency entry.
*
* Example:
* ```
* $psrLogger->emergency('emergency message');
* ```
*
* @param string $message The message to log.
* @param array $context [optional] Please see {@see Google\Cloud\Logging\PsrLogger::log()}
* for the available options.
* @return void
*/
public function emergency($message, array $context = [])
{
$this->log(Logger::EMERGENCY, $message, $context);
}
/**
* Log an alert entry.
*
* Example:
* ```
* $psrLogger->alert('alert message');
* ```
*
* @param string $message The message to log.
* @param array $context [optional] Please see {@see Google\Cloud\Logging\PsrLogger::log()}
* for the available options.
* @return void
*/
public function alert($message, array $context = [])
{
$this->log(Logger::ALERT, $message, $context);
}
/**
* Log a critical entry.
*
* Example:
* ```
* $psrLogger->critical('critical message');
* ```
*
* @param string $message The message to log.
* @param array $context [optional] Please see {@see Google\Cloud\Logging\PsrLogger::log()}
* for the available options.
* @return void
*/
public function critical($message, array $context = [])
{
$this->log(Logger::CRITICAL, $message, $context);
}
/**
* Log an error entry.
*
* Example:
* ```
* $psrLogger->error('error message');
* ```
*
* @param string $message The message to log.
* @param array $context [optional] Please see {@see Google\Cloud\Logging\PsrLogger::log()}
* for the available options.
* @return void
*/
public function error($message, array $context = [])
{
$this->log(Logger::ERROR, $message, $context);
}
/**
* Log a warning entry.
*
* Example:
* ```
* $psrLogger->warning('warning message');
* ```
*
* @param string $message The message to log.
* @param array $context [optional] Please see {@see Google\Cloud\Logging\PsrLogger::log()}
* for the available options.
* @return void
*/
public function warning($message, array $context = [])
{
$this->log(Logger::WARNING, $message, $context);
}
/**
* Log a notice entry.
*
* Example:
* ```
* $psrLogger->notice('notice message');
* ```
*
* @param string $message The message to log.
* @param array $context [optional] Please see {@see Google\Cloud\Logging\PsrLogger::log()}
* for the available options.
* @return void
*/
public function notice($message, array $context = [])
{
$this->log(Logger::NOTICE, $message, $context);
}
/**
* Log an info entry.
*
* Example:
* ```
* $psrLogger->info('info message');
* ```
*
* @param string $message The message to log.
* @param array $context [optional] Please see {@see Google\Cloud\Logging\PsrLogger::log()}
* for the available options.
* @return void
*/
public function info($message, array $context = [])
{
$this->log(Logger::INFO, $message, $context);
}
/**
* Log a debug entry.
*
* Example:
* ```
* $psrLogger->debug('debug message');
* ```
*
* @param string $message The message to log.
* @param array $context [optional] Please see {@see Google\Cloud\Logging\PsrLogger::log()}
* for the available options.
* @return void
*/
public function debug($message, array $context = [])
{
$this->log(Logger::DEBUG, $message, $context);
}
/**
* Write a log entry.
*
* Example:
* ```
* use Google\Cloud\Logging\Logger;
*
* $psrLogger->log(Logger::ALERT, 'alert message');
* ```
*
* ```
* // Write a log entry using the context array with placeholders.
* use Google\Cloud\Logging\Logger;
*
* $psrLogger->log(Logger::ALERT, 'alert: {message}', [
* 'message' => 'my alert message'
* ]);
* ```
*
* ```
* // Log information regarding an HTTP request
* use Google\Cloud\Logging\Logger;
*
* $psrLogger->log(Logger::ALERT, 'alert message', [
* 'stackdriverOptions' => [
* 'httpRequest' => [
* 'requestMethod' => 'GET'
* ]
* ]
* ]);
* ```
*
* @param string|int $level The severity of the log entry.
* @param string $message The message to log.
* @param array $context {
* Context is an associative array which can include placeholders to be
* used in the `$message`. Placeholders must be delimited with a single
* opening brace `{` and a single closing brace `}`. The context will be
* added as additional information on the `jsonPayload`. Please note
* that the key `stackdriverOptions` is reserved for logging Google
* Stackdriver specific data.
*
* @type array $stackdriverOptions['resource'] The
* [monitored resource](https://cloud.google.com/logging/docs/api/reference/rest/v2/MonitoredResource)
* to associate this log entry with. **Defaults to** type global.
* @type array $stackdriverOptions['httpRequest'] Information about the
* HTTP request associated with this log entry, if applicable.
* Please see
* [the API docs](https://cloud.google.com/logging/docs/api/reference/rest/v2/LogEntry#httprequest)
* for more information.
* @type array $stackdriverOptions['labels'] A set of user-defined
* (key, value) data that provides additional information about
* the log entry.
* @type array $stackdriverOptions['operation'] Additional information
* about a potentially long-running operation with which a log
* entry is associated. Please see
* [the API docs](https://cloud.google.com/logging/docs/api/reference/rest/v2/LogEntry#logentryoperation)
* for more information.
* @type string $stackdriverOptions['insertId'] A unique identifier for
* the log entry.
* @type \DateTimeInterface|Timestamp|string|null $stackdriverOptions['timestamp'] The
* timestamp associated with this entry. If providing a string it
* must be in RFC3339 UTC "Zulu" format. Example:
* "2014-10-02T15:01:23.045123456Z". If explicitly set to `null`
* the timestamp will be generated by the server at the moment the
* entry is received (with nanosecond precision). **Defaults to**
* the current time, generated by the client with microsecond
* precision.
* }
* @return void
* @throws InvalidArgumentException
*/
public function log($level, $message, array $context = [])
{
$this->validateLogLevel($level);
$options = [];
if (isset($context['exception'])
&& ($context['exception'] instanceof \Exception || $context['exception'] instanceof \Throwable)) {
$context['exception'] = (string) $context['exception'];
}
if (isset($context['stackdriverOptions'])) {
$options = $context['stackdriverOptions'];
unset($context['stackdriverOptions']);
}
$formatter = new NormalizerFormatter();
$processor = new PsrLogMessageProcessor();
$processedData = $processor([
'message' => (string) $message,
'context' => $formatter->format($context)
]);
$jsonPayload = [$this->messageKey => $processedData['message']];
// Adding labels for log request correlation.
$labels = $this->getLabels();
if (!empty($labels)) {
$options['labels'] =
(isset($options['labels'])
? $options['labels']
: []) + $labels;
// Copy over the value for 'appengine.googleapis.com/trace_id' to
// `trace` option too.
if (isset($labels['appengine.googleapis.com/trace_id'])) {
$options['trace'] =
$labels['appengine.googleapis.com/trace_id'];
}
}
// Adding MonitoredResource
$resource = $this->metadataProvider->monitoredResource();
if (!empty($resource)) {
$options['resource'] =
(isset($options['resource'])
? $options['resource']
: []) + $resource;
}
$entry = $this->logger->entry(
$jsonPayload + $processedData['context'],
$options + [
'severity' => $level
]
);
$this->sendEntry($entry);
}
/**
* Return the MetadataProvider.
*
* @return MetadataProviderInterface
*/
public function getMetadataProvider()
{
return $this->metadataProvider;
}
/**
* Serializes data.
*
* @return string
* @access private
*/
public function serialize()
{
return serialize($this->__serialize());
}
/**
* Unserializes data.
*
* @param string
* @access private
*/
public function unserialize($data)
{
$this->__unserialize(unserialize($data));
}
public function __serialize()
{
$debugOutputResource = null;
if (is_resource($this->debugOutputResource)) {
$metadata = stream_get_meta_data($this->debugOutputResource);
$debugOutputResource = [
'uri' => $metadata['uri'],
'mode' => $metadata['mode']
];
}
return [
$this->messageKey,
$this->batchEnabled,
$this->metadataProvider,
$this->debugOutput,
$this->clientConfig,
$this->batchMethod,
$this->logName,
$debugOutputResource
];
}
public function __unserialize(array $data)
{
list(
$this->messageKey,
$this->batchEnabled,
$this->metadataProvider,
$this->debugOutput,
$this->clientConfig,
$this->batchMethod,
$this->logName,
$debugOutputResource
) = $data;
if (is_array($debugOutputResource)) {
$this->debugOutputResource = fopen(
$debugOutputResource['uri'],
$debugOutputResource['mode']
);
}
}
/**
* Returns an array representation of a callback which will be used to write
* batch items.
*
* @return array
*/
protected function getCallback()
{
if (!array_key_exists($this->logName, self::$loggers)) {
$c = new LoggingClient($this->getUnwrappedClientConfig());
self::$loggers[$this->logName] = $c->logger($this->logName);
}
return [self::$loggers[$this->logName], $this->batchMethod];
}
/**
* Validates whether or not the provided log level exists.
*
* @param string|int $level The severity of the log entry.
* @return bool
* @throws InvalidArgumentException
*/
private function validateLogLevel($level)
{
$map = Logger::getLogLevelMap();
$level = (string) $level;
if (isset($map[$level]) || isset(array_flip($map)[strtoupper($level)])) {
return true;
}
throw new InvalidArgumentException("Severity level '$level' is not defined.");
}
/**
* Send the given entry.
*
* @param Entry $entry
*/
private function sendEntry(Entry $entry)
{
if ($this->batchEnabled) {
$this->batchRunner->submitItem($this->identifier, $entry);
return;
}
$this->logger->write($entry);
}
/**
* Return additional labels. Now it returns labels for log request
* correlation.
*
* @return array
*/
private function getLabels()
{
return $this->metadataProvider->labels();
}
}