首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google云堆栈驱动程序与monolog Symfony

Google云堆栈驱动程序与monolog Symfony
EN

Stack Overflow用户
提问于 2017-01-16 12:13:14
回答 1查看 3.7K关注 0票数 1

我正在使用Google (GKE),我想使用他们的系统进行日志和监视器(Stackdriver)。我的projet在php Symfony3下。我正在搜索如何登录到堆栈驱动程序,我的symfony项目的一些日志。

我看到有一个正式的自由:

https://github.com/GoogleCloudPlatform/google-cloud-php

和PSR-3级:

http://googlecloudplatform.github.io/google-cloud-php/#/docs/v0.20.1/logging/psrlogger

我的问题是,如何在我的config.yml中集成monolog?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-08 01:21:48

为此,我做了以下工作:

代码语言:javascript
复制
composer require "google/cloud":"~0.20"

在配置中,我使用了一个自定义处理程序:

代码语言:javascript
复制
monolog:
    handlers:
        main:
            type: service
            id:   stackdriver_handler

注册处理程序服务:

代码语言:javascript
复制
services:
    stackdriver_handler:
        class: Acme\MyBundle\Monolog\StackdriverHandler

下面是我使用的处理程序类:

代码语言:javascript
复制
<?php

namespace Acme\MyBundle\Monolog\Handler;

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;

    /**
     * StackdriverHandler constructor.
     *
     * @param LoggerInterface $projectId
     * @param bool            $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])) {
            $this->loggers[$channel] = $this->client->psrLogger($this->name, ['labels' => ['context' => $channel]]);
        }

        return $this->loggers[$channel];
    }
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41676196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档