JSON Log in PHP and Cloud Watch(1)JSON Format in PHP

JSON Log in PHP and Cloud Watch(1)JSON Format in PHP

One way to powerful the search in CloudWatch Log is to use Pure JSON in log.

First Step Configure JSON Format Log in PHP
I am using this logging framework in PHP right now
https://github.com/katzgrau/KLogger

So the changes will be similar to this.
<?php
namespace JobConsumerPHP;

require __DIR__ . '/../../vendor/autoload.php';

use \Psr\Log\LogLevel;
use \Katzgrau\KLogger\Logger;

class LoggerUtil
{
    private $logger = null;
    private $rawJobLogger = null;
    private $jobRedisLogger = null;
    private $jobSolrLogger = null;
    private $jobBackupLogger = null;

    public function __construct($ioc)
    {
        $config = $ioc->getService("config");
       
        $logLevelConfig = $config["loggingLevel"];
        $logLevelSystem = getenv('LOGGING_LEVEL');
       
        // default is from config
        $logLevel = $logLevelConfig;
        if (!empty($logLevelSystem)) {
            $logLevel = $logLevelSystem;
        }

        $logLevelSetting = $this->getLogLevelSetting($logLevel);
        $options = array(
                        'appendContext' => false,
                        'logFormat' => json_encode([
                                'datetime' => '{date}',
                                'logLevel' => '{level}',
                                'message'  => '{message}',
                                'context'  => '{context}',])
            );

        $this->logger = new Logger('php://stdout', $logLevelSetting, $options);
        // 'php://stdout'
        $this->rawJobLogger = new Logger('php://stdout', $logLevelSetting, $options);
        $this->jobRedisLogger = new Logger('php://stdout', $logLevelSetting, $options);
        $this->jobSolrLogger = new Logger('php://stdout', $logLevelSetting, $options);
        $this->jobBackupLogger = new Logger('php://stdout', $logLevelSetting, $options);
    }

    public function getLogLevelSetting($logLevel)
    {
        $logLevelSetting = null;
       
        switch ($logLevel) {
            case 'DEBUG':
                $logLevelSetting = LogLevel::DEBUG;
                break;
            case 'ERROR':
                $logLevelSetting = LogLevel::ERROR;
                break;
            case 'INFO':
                $logLevelSetting = LogLevel::INFO;
                break;
            case 'WARN':
                $logLevelSetting = LogLevel::WARNING;
                break;
            default:
                $logLevelSetting = LogLevel::DEBUG;
        }      
        return $logLevelSetting;
    }
    public function getLogger()
    {
        return $this->logger;
    }
    public function getRawJobLogger()
    {
        return $this->rawJobLogger;
    }
    public function getJobRedisLogger()
    {
        return $this->jobRedisLogger;
    }
    public function getJobSolrLogger()
    {
        return $this->jobSolrLogger;
    }
    public function getJobBackupLogger()
    {
        return $this->jobBackupLogger;
    }
}
?>

When we use the logging in our PHP class, it will be similar to this
function convertUnix2DateTime($unixTime)
{
    $logger = $this->ioc->getService("logger");
    if (is_numeric($unixTime)) {
        $logger->debug("convert the unixTime to DateTime", array('unixTime' => $unixTime));
        $dateTime = date('Y-m-d\TH:i:s\Z', $unixTime);
    } else {
        $dateTime = $unixTime;
    }
    return $dateTime;
}

The output will be
{"datetime":"2017-12-29 21:29:47.276477","logLevel":"DEBUG","message":"convert the unixTime to DateTime","context":"{"unixTime":"1473216774"}"}
{"datetime":"2017-12-29 21:29:47.276699","logLevel":"DEBUG","message":"convert the unixTime to DateTime","context":"{"unixTime":1473216774}"}

Next Step is to deploy this and Try the Search on Cloud Watch

References:
https://github.com/katzgrau/KLogger
https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html

猜你喜欢

转载自sillycat.iteye.com/blog/2406205
今日推荐