This commit is contained in:
dongwei 2019-01-10 15:22:28 +08:00
parent c60a1aa9e4
commit 3eba9c8094
2 changed files with 59 additions and 0 deletions

View 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";
}
}

View File

@ -4,6 +4,7 @@ namespace JsonRpc\Providers;
use Illuminate\Support\ServiceProvider;
use JsonRpc\Client;
use JsonRpc\Logging\LogstashFormatter;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
@ -21,6 +22,7 @@ class ClientServiceProvider extends ServiceProvider
$config = config('rpc.client');
$stream = new StreamHandler($this->app->storagePath()."/logs/rpc_monitor_".date("Ymd").".log");
$stream->setFormatter(LogstashFormatter::class);
$logger = new Logger('RPC.LOGGER');
$logger->pushHandler($stream);
$this->app->singleton('rpc', function () use ($config, $logger) {