当前,我在Google App Engine Flex环境中使用Symfony 4。
我正在尝试在日志条目中添加额外的“标签”以帮助调试,我一直在使用Stackdriver处理程序记录事件,并且尝试将新标签添加到在本地可以正常工作的Stackdriver处理程序中,但是在我部署到GAE时不起作用。
我的主要目标是为所有日志条目添加一个标识符,以通过执行来帮助对条目进行分组。
有人可以告诉我为什么我的Stackdriver处理程序中的标签没有传递到日志中吗?
GAE实例中的日志条目仅具有以下标签:
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];
}
}
答案 0 :(得分:0)
您可以使用Stackdriver Client Libraries直接读取/写入日志条目。 客户端库支持向条目添加自定义标签,可以这样实现:
# Write a log entry
entry = logging.entry
entry.payload = "Job started."
entry.log_name = "my_app_log"
entry.resource.type = "gae_app"
entry.resource.labels[:module_id] = "1"
entry.resource.labels[:version_id] = "20150925t173233"