0

Google Cloud(GKE)を使用していて、ログとモニタ用にシステムを使用したいと思っています(Stackdriver)。私のprojetはsymfony3の下にあります。 symfonyプロジェクトのいくつかのログをスタックドライバに記録する方法を探しています。Google Cloud Stackdriverとmonolog Symfony

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

そして、PSR-3クラス:

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

私の質問は、私の設定でそれを統合する方法、である

は私が公式のlibがあることを見ました。モノリスとyml?

答えて

3

私は次の操作を行って、これをしなかった:

設定で
composer require "google/cloud":"~0.20" 

、私はカスタムハンドラを使用:

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

ハンドラサービスを登録します。

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

はここですハンドラクラスI:

<?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]; 
    } 
} 
関連する問題