mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-21 16:46:11 +02:00
Merge Logstach V1 logic into the main Logstash formatter, add a parameter to the constructor to specify version
Defaults to version '0'.
This commit is contained in:
@@ -21,6 +21,10 @@ namespace Monolog\Formatter;
|
||||
*/
|
||||
class LogstashFormatter extends NormalizerFormatter
|
||||
{
|
||||
|
||||
const V0 = 0;
|
||||
const V1 = 1;
|
||||
|
||||
/**
|
||||
* @var string the name of the system for the Logstash log message, used to fill the @source field
|
||||
*/
|
||||
@@ -41,13 +45,18 @@ class LogstashFormatter extends NormalizerFormatter
|
||||
*/
|
||||
protected $contextPrefix;
|
||||
|
||||
/**
|
||||
* @var integer LogStash format version to use
|
||||
*/
|
||||
protected $version;
|
||||
|
||||
/**
|
||||
* @param string $applicationName the application that sends the data, used as the "type" field of logstash
|
||||
* @param string $systemName the system/machine name, used as the "source" field of logstash, defaults to the hostname of the machine
|
||||
* @param string $extraPrefix prefix for extra keys inside logstash "fields"
|
||||
* @param string $contextPrefix prefix for context keys inside logstash "fields", defaults to ctxt_
|
||||
*/
|
||||
public function __construct($applicationName, $systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_')
|
||||
public function __construct($applicationName, $systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_', $version = self::V0)
|
||||
{
|
||||
//log stash requires a ISO 8601 format date
|
||||
parent::__construct('c');
|
||||
@@ -57,6 +66,8 @@ class LogstashFormatter extends NormalizerFormatter
|
||||
|
||||
$this->extraPrefix = $extraPrefix;
|
||||
$this->contextPrefix = $contextPrefix;
|
||||
|
||||
$this->version = $version;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,19 +76,36 @@ class LogstashFormatter extends NormalizerFormatter
|
||||
public function format(array $record)
|
||||
{
|
||||
$record = parent::format($record);
|
||||
|
||||
switch ($this->version) {
|
||||
case self::V1:
|
||||
$message = $this->formatV1($record);
|
||||
break;
|
||||
case self::V0:
|
||||
default:
|
||||
$message = $this->formatV0($record);
|
||||
break;
|
||||
}
|
||||
|
||||
return $this->toJson($message) . "\n";
|
||||
}
|
||||
|
||||
protected function formatV0(array $record)
|
||||
{
|
||||
$message = array(
|
||||
'@timestamp' => $record['datetime'],
|
||||
'@message' => $record['message'],
|
||||
'@tags' => array($record['channel']),
|
||||
'@source' => $this->systemName
|
||||
'@source' => $this->systemName,
|
||||
'@fields' => array(
|
||||
'channel' => $record['channel'],
|
||||
'level' => $record['level']
|
||||
)
|
||||
);
|
||||
|
||||
if ($this->applicationName) {
|
||||
$message['@type'] = $this->applicationName;
|
||||
}
|
||||
$message['@fields'] = array();
|
||||
$message['@fields']['channel'] = $record['channel'];
|
||||
$message['@fields']['level'] = $record['level'];
|
||||
|
||||
if (isset($record['extra']['server'])) {
|
||||
$message['@source_host'] = $record['extra']['server'];
|
||||
@@ -93,6 +121,33 @@ class LogstashFormatter extends NormalizerFormatter
|
||||
$message['@fields'][$this->contextPrefix . $key] = $val;
|
||||
}
|
||||
|
||||
return json_encode($message) . "\n";
|
||||
return $message;
|
||||
}
|
||||
|
||||
protected function formatV1(array $record)
|
||||
{
|
||||
$message = array(
|
||||
'@timestamp' => $record['datetime'],
|
||||
'@version' => 1,
|
||||
'message' => $record['message'],
|
||||
'host' => $this->systemName,
|
||||
'type' => $record['channel'],
|
||||
'channel' => $record['channel'],
|
||||
'level' => $record['level_name']
|
||||
);
|
||||
|
||||
if ($this->applicationName) {
|
||||
$message['type'] = $this->applicationName;
|
||||
}
|
||||
|
||||
foreach ($record['extra'] as $key => $val) {
|
||||
$message[$this->extraPrefix . $key] = $val;
|
||||
}
|
||||
|
||||
foreach ($record['context'] as $key => $val) {
|
||||
$message[$this->contextPrefix . $key] = $val;
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
|
@@ -1,92 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Formatter;
|
||||
|
||||
/**
|
||||
* Serializes a log message to Logstash Event Format
|
||||
*
|
||||
* @see http://logstash.net/
|
||||
* @see https://github.com/logstash/logstash/blob/master/lib/logstash/event.rb
|
||||
*
|
||||
* @author Tim Mower <timothy.mower@gmail.com>
|
||||
*/
|
||||
class LogstashV1Formatter extends NormalizerFormatter
|
||||
{
|
||||
/**
|
||||
* @var string the name of the system for the Logstash log message, used to fill the @source field
|
||||
*/
|
||||
protected $systemName;
|
||||
|
||||
/**
|
||||
* @var string an application name for the Logstash log message, used to fill the @type field
|
||||
*/
|
||||
protected $applicationName;
|
||||
|
||||
/**
|
||||
* @var string a prefix for 'extra' fields from the Monolog record (optional)
|
||||
*/
|
||||
protected $extraPrefix;
|
||||
|
||||
/**
|
||||
* @var string a prefix for 'context' fields from the Monolog record (optional)
|
||||
*/
|
||||
protected $contextPrefix;
|
||||
|
||||
/**
|
||||
* @param string $applicationName the application that sends the data, used as the "type" field of logstash
|
||||
* @param string $systemName the system/machine name, used as the "source" field of logstash, defaults to the hostname of the machine
|
||||
* @param string $extraPrefix prefix for extra keys inside logstash "fields"
|
||||
* @param string $contextPrefix prefix for context keys inside logstash "fields", defaults to ctxt_
|
||||
*/
|
||||
public function __construct($applicationName, $systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_')
|
||||
{
|
||||
//log stash requires a ISO 8601 format date
|
||||
parent::__construct('c');
|
||||
|
||||
$this->systemName = $systemName ?: gethostname();
|
||||
$this->applicationName = $applicationName;
|
||||
|
||||
$this->extraPrefix = $extraPrefix;
|
||||
$this->contextPrefix = $contextPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function format(array $record)
|
||||
{
|
||||
$record = parent::format($record);
|
||||
$message = array(
|
||||
'@timestamp' => $record['datetime'],
|
||||
'@version' => 1,
|
||||
'message' => $record['message'],
|
||||
'host' => $this->systemName,
|
||||
'type' => $record['channel'],
|
||||
'channel' => $record['channel'],
|
||||
'level' => $record['level_name']
|
||||
);
|
||||
|
||||
if ($this->applicationName) {
|
||||
$message['type'] = $this->applicationName;
|
||||
}
|
||||
|
||||
foreach ($record['extra'] as $key => $val) {
|
||||
$message[$this->extraPrefix . $key] = $val;
|
||||
}
|
||||
|
||||
foreach ($record['context'] as $key => $val) {
|
||||
$message[$this->contextPrefix . $key] = $val;
|
||||
}
|
||||
|
||||
return $this->toJson($message) . "\n";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user