Google Cloud Stackdriver和monolog Symfony [英] Google Cloud Stackdriver and monolog Symfony

查看:83
本文介绍了Google Cloud Stackdriver和monolog Symfony的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Cloud(GKE),并且希望将其系统用于日志和监视(Stackdriver).我的项目在php Symfony3下.我正在搜索如何将symfony项目的一些日志记录到stackdriver.

I'm working with Google Cloud (GKE) and I want to use their system for log and monitor (Stackdriver). My projet is under php Symfony3. I'm searching how to log to stackdriver some logs of my symfony project.

我看到有一个官方库:

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

还有PSR-3类:

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

我的问题是,如何将其与monolog集成到config.yml中?

My question is, how to integrate that in my config.yml with monolog ?

推荐答案

我是通过以下操作做到这一点的:

I did this by doing the following:

composer require "google/cloud":"~0.20"

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

In the config, I used a custom handler:

monolog:
    handlers:
        main:
            type: service
            id:   stackdriver_handler

注册处理程序服务:

services:
    stackdriver_handler:
        class: Acme\MyBundle\Monolog\StackdriverHandler

这是我使用的处理程序类:

Here's the handler class I used:

<?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];
    }
}

这篇关于Google Cloud Stackdriver和monolog Symfony的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆