0

Google Cloud (GKE)에서 작업 중이며 시스템을 로그 및 모니터 (스택 드라이버)로 사용하고 싶습니다. 내 projet은 Symfony3하에 있습니다. symfony 프로젝트의 일부 로그를 stackdriver에 기록하는 방법을 찾고 있습니다.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

내 질문에 내 설정에서 그것을 통합하는 방법이다

가 나는 offical 한 LIB이 있음을 보았다. yml과 monolog?

답변

3

내가 다음을 수행하여 이런 짓을 : 여기

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

입니다 :

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

핸들러 서비스를 등록는 config에서

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

을, 나는 사용자 정의 핸들러를 사용 핸들러 클래스 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]; 
    } 
}