Currently I'm using Symfony 4 on the Google App Engine Flex environment.
I'm trying to add extra 'labels' to my log entries to help with debugging, I've been using a Stackdriver handler to log events, and I tried adding the new labels to the Stackdriver handler which works fine locally, but does not work when I deploy to GAE.
My main goal is to add an identifier to all log entries to help group entries by execution.
Can somebody tell me why the labels from my Stackdriver handler are not passed to the log?
Log entries from the GAE instance only have labels like:
labels: {
appengine.googleapis.com/trace_id: "58a54e17ef1110b2720f0b103033187d"
}
monolog.yaml
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_404s:
# regex: exclude all 404 errors from the logs
- ^/
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
stackdriver:
type: service
id: stackdriver_handler
level: debug
StackdriverHandler.php
<?php
namespace App\Bundle\Monolog;
use Google\Cloud\Logging\LoggingClient;
use Monolog\Handler\PsrHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
class StackdriverHandler extends PsrHandler
{
/**
* @var LoggerInterface[]
*/
protected $loggers;
/**
* @var LoggingClient
*/
protected $client;
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $transactionId;
/**
* StackdriverHandler constructor.
*
* @param string $projectId
* @param string $name
* @param bool|int $level
* @param bool $bubble
*/
public function __construct($projectId, $name, $level = Logger::DEBUG, $bubble = true)
{
$this->client = new LoggingClient(
[
'projectId' => $projectId,
]
);
$this->name = $name;
$this->level = $level;
$this->bubble = $bubble;
}
/**
* {@inheritdoc}
*/
public function handle(array $record)
{
if (!$this->isHandling($record)) {
return false;
}
$this->getLogger($record['channel'])->log(strtolower($record['level_name']), $record['message'], $record['context']);
return false === $this->bubble;
}
/**
* @param $channel
*
* @return LoggerInterface
*/
protected function getLogger($channel)
{
if (!isset($this->loggers[$channel]))
{
$params = ['labels' => ['context' => $channel, 'transaction_id'=> $this->transactionId] ];
$this->loggers[$channel] = $this->client->psrLogger($this->name, $params);
}
return $this->loggers[$channel];
}
}