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

本文介绍如何使用PHP的日志框架KLogger配置纯JSON格式的日志,并将这些日志集成到AWS CloudWatch中进行高效搜索。通过示例代码展示了如何设置不同类型的日志记录器并调整日志级别。
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值