6

目前我在 Google App Engine Flex 环境中使用 Symfony 4。

我正在尝试在我的日志条目中添加额外的“标签”以帮助调试,我一直在使用 Stackdriver 处理程序来记录事件,并且我尝试将新标签添加到在本地可以正常工作的 Stackdriver 处理程序,但不能当我部署到 GAE 时工作。

我的主要目标是为所有日志条目添加一个标识符,以帮助通过执行对条目进行分组。

有人能告诉我为什么我的 Stackdriver 处理程序中的标签没有传递到日志吗?

GAE 实例中的日志条目只有如下标签:

labels: {
  appengine.googleapis.com/trace_id:  "58a54e17ef1110b2720f0b103033187d"   
 }

独白.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];
    }
}
4

1 回答 1

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"

于 2019-04-22T08:12:33.727 回答