logstash
This commit is contained in:
parent
c60a1aa9e4
commit
3eba9c8094
57
src/Logging/LogstashFormatter.php
Normal file
57
src/Logging/LogstashFormatter.php
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: dongwei
|
||||||
|
* Date: 2019/1/10
|
||||||
|
* Time: 3:18 PM
|
||||||
|
*/
|
||||||
|
namespace JsonRpc\Logging;
|
||||||
|
|
||||||
|
use Monolog\Formatter\NormalizerFormatter;
|
||||||
|
|
||||||
|
class LogstashFormatter extends NormalizerFormatter
|
||||||
|
{
|
||||||
|
protected $hostname;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
// logstash requires a ISO 8601 format date with optional millisecond precision.
|
||||||
|
parent::__construct('Y-m-d\TH:i:s.uP');
|
||||||
|
|
||||||
|
$this->hostname = gethostname();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function format(array $record)
|
||||||
|
{
|
||||||
|
$record = parent::format($record);
|
||||||
|
|
||||||
|
$message = array(
|
||||||
|
'@timestamp' => $record['datetime'],
|
||||||
|
'host' => $this->hostname,
|
||||||
|
'app' => env('APP_NAME'),
|
||||||
|
'env' => app()->environment(),
|
||||||
|
);
|
||||||
|
|
||||||
|
$request_id = app('request')->header('X-Request-Id');
|
||||||
|
if ($request_id) {
|
||||||
|
$message['request_id'] = $request_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (isset($record['channel'])) {
|
||||||
|
// $message['channel'] = $record['channel'];
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (isset($record['level_name'])) {
|
||||||
|
$message['level'] = $record['level_name'];
|
||||||
|
}
|
||||||
|
if (isset($record['message'])) {
|
||||||
|
$message['message'] = $record['message'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($record['context'])) {
|
||||||
|
$message['context'] = $this->toJson($record['context']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->toJson($message) . "\n";
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ namespace JsonRpc\Providers;
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use JsonRpc\Client;
|
use JsonRpc\Client;
|
||||||
|
use JsonRpc\Logging\LogstashFormatter;
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
@ -21,6 +22,7 @@ class ClientServiceProvider extends ServiceProvider
|
||||||
|
|
||||||
$config = config('rpc.client');
|
$config = config('rpc.client');
|
||||||
$stream = new StreamHandler($this->app->storagePath()."/logs/rpc_monitor_".date("Ymd").".log");
|
$stream = new StreamHandler($this->app->storagePath()."/logs/rpc_monitor_".date("Ymd").".log");
|
||||||
|
$stream->setFormatter(LogstashFormatter::class);
|
||||||
$logger = new Logger('RPC.LOGGER');
|
$logger = new Logger('RPC.LOGGER');
|
||||||
$logger->pushHandler($stream);
|
$logger->pushHandler($stream);
|
||||||
$this->app->singleton('rpc', function () use ($config, $logger) {
|
$this->app->singleton('rpc', function () use ($config, $logger) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user