kubernetesgoogle-kubernetes-enginestackdrivergoogle-cloud-stackdriverfuelphp

How to output log to StackDriver logging in fuelPHP


I deployed app is created by fuelPHP on GKE.

GKE Cluster is enabled Stackdriver Kubernetes Engine Monitoring. https://cloud.google.com/monitoring/kubernetes-engine

But fuelPHP's Log::xxxx() is not output to stackdriver logging. Then I modified config.php following.

//'log_path' =>APPPATH. 'logs',
'log_path' => '/dev/stdout',

It's not work.(error occur '/dev/stdout does not exist').

How to output logs to StackDriver logging?


Solution

  • I override core/Log class as app/Log.

    app/classes/log.php

    <?php
    
    class Log extends \Fuel\Core\Log
    {
        public static function initialize()
        {
    
            //$stream = new \Monolog\Handler\StreamHandler($filename, \Monolog\Logger::DEBUG);
            $stream = new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG);
            $formatter = new \Monolog\Formatter\LineFormatter("%level_name% - %datetime% --> %message%".PHP_EOL, "Y-m-d H:i:s");
            $stream->setFormatter($formatter);
            static::$monolog->pushHandler($stream);
        }
    }
    

    and in app/bootstrap.php. Add as in the example

    // Example: 'Log' => APPPATH.'classes/log.php',