Skip to content

Instantly share code, notes, and snippets.

@gander
Created February 3, 2022 12:23
Show Gist options
  • Save gander/05801b40ac58f1f8e74f94f479c52176 to your computer and use it in GitHub Desktop.
Save gander/05801b40ac58f1f8e74f94f479c52176 to your computer and use it in GitHub Desktop.
google-cloud-ops-agent logging
<?php
$formatter = new \Monolog\Formatter\JsonFormatter();
$handler = new \Monolog\Handler\StreamHandler('/www/logs/agent.log');
$handler->setFormatter($formatter);
$logger = new \Monolog\Logger('agent', [$handler]);
$handler->pushProcessor(function (array $record) {
return [
'timestamp' => ['seconds' => time(), 'nanos' => 0],
'logging.googleapis.com/severity' => 'ALERT',
'logging.googleapis.com/labels' => ['foo' => 'bar'],
'logging.googleapis.com/sourceLocation' => [
'file' => '/devil/entity.php',
'line' => 666,
'function' => 'die',
],
'lorem' => 'ipsum',
'record' => $record,
];
});
$logger->alert('WORKING EXAMPLE');
{
"insertId": "xyz",
"jsonPayload": {
"record": {
"channel": "agent",
"level": 550,
"context": [],
"message": "WORKING EXAMPLE",
"datetime": "2021-10-06T15:08:37.381344+02:00",
"extra": [],
"level_name": "ALERT"
},
"lorem": "ipsum"
},
"resource": {
"type": "gce_instance",
"labels": {
"zone": "europe-xyz",
"instance_id": "123",
"project_id": "321"
}
},
"timestamp": "2021-10-06T13:08:37Z",
"severity": "ALERT",
"labels": {
"foo": "bar"
},
"logName": "projects/321/logs/agent-test",
"sourceLocation": {
"file": "/devil/entity.php",
"line": "666",
"function": "die"
},
"receiveTimestamp": "2021-10-06T13:08:38.300034330Z"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment