Skip to content

Instantly share code, notes, and snippets.

@bordeux
Created January 4, 2018 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bordeux/d205b0a2f65446f4b3374ed92153d9ea to your computer and use it in GitHub Desktop.
Save bordeux/d205b0a2f65446f4b3374ed92153d9ea to your computer and use it in GitHub Desktop.
CloudWatchFormatter for Monolog
<?php
namespace YourBundle\OwnNamespace\Monolog\Formatter;
use Monolog\Formatter\NormalizerFormatter;
/**
* Class CloudWatchFormatter
* @author Chris Bednarczyk <bordeux>
* @package YourBundle\OwnNamespace\Monolog\Formatter
*/
class CloudWatchFormatter 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;
/**
* CloudWatchFormatter constructor.
* @param $applicationName
* @param null $systemName
* @param null $extraPrefix
*/
public function __construct(
$applicationName,
$systemName = null,
$extraPrefix = null,
$contextPrefix = 'ctxt_'
)
{
parent::__construct('Y-m-d\TH:i:s.uP');
$this->systemName = $systemName ?: gethostname();
$this->applicationName = $applicationName;
$this->extraPrefix = $extraPrefix;
$this->contextPrefix = $contextPrefix;
}
/**
* {@inheritdoc}
*/
public function format(array $record)
{
$message = $this->formatToJson(
parent::format($record)
);
return $message['timestamp'].' '.$this->toJson($message) . "\n";
}
/**
* @param mixed[] $record
* @return mixed[]
* @author Chris Bednarczyk <chris@tourradar.com>
*/
protected function formatToJson(array $record)
{
if (empty($record['datetime'])) {
$record['datetime'] = gmdate('c');
}
$message = array(
'timestamp' => $record['datetime'],
'source' => $this->systemName,
'fields' => array(),
);
if (isset($record['message'])) {
$message['message'] = $record['message'];
}
if (isset($record['channel'])) {
$message['tags'] = array($record['channel']);
$message['fields']['channel'] = $record['channel'];
}
if (isset($record['level'])) {
$message['fields']['level'] = $record['level'];
}
if ($this->applicationName) {
$message['type'] = $this->applicationName;
}
if (isset($record['extra']['server'])) {
$message['source_host'] = $record['extra']['server'];
}
if (isset($record['extra']['url'])) {
$message['source_path'] = $record['extra']['url'];
}
if (!empty($record['extra'])) {
foreach ($record['extra'] as $key => $val) {
$message['fields'][$this->extraPrefix . $key] = $val;
}
}
if (!empty($record['context'])) {
foreach ($record['context'] as $key => $val) {
$message['fields'][$this->contextPrefix . $key] = $val;
}
}
return $message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment