| 1 |
<?php |
| 2 |
|
| 3 |
namespace Liuggio\StatsdClient\Monolog\Formatter; |
| 4 |
|
| 5 |
use Monolog\Formatter\LineFormatter; |
| 6 |
|
| 7 |
/** |
| 8 |
* Formats incoming records in order to be a perfect StatsD key. |
| 9 |
* |
| 10 |
* This is especially useful for logging to files |
| 11 |
* |
| 12 |
* @author Jordi Boggiano <[email protected]> |
| 13 |
* @author Christophe Coevoet <[email protected]> |
| 14 |
* @author Giulio De Donato <[email protected]> |
| 15 |
*/ |
| 16 |
class StatsDFormatter extends LineFormatter |
| 17 |
{ |
| 18 |
const SIMPLE_FORMAT = "%channel%.%level_name%.%short_message%"; |
| 19 |
|
| 20 |
protected $numberOfWords; |
| 21 |
protected $logContext; |
| 22 |
protected $logExtra; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param string $format The format of the message |
| 26 |
* @param Boolean $logContext If true add multiple rows containing Context information |
| 27 |
* @param Boolean $logExtra If true add multiple rows containing Extra information |
| 28 |
* @param integer $numberOfWords The number of words to show. |
| 29 |
*/ |
| 30 |
public function __construct($format = null, $logContext = true, $logExtra = true, $numberOfWords = 2) |
| 31 |
{ |
| 32 |
$this->format = $format ? : static::SIMPLE_FORMAT; |
| 33 |
$this->numberOfWords = $numberOfWords; |
| 34 |
$this->logContext = $logContext; |
| 35 |
$this->logExtra = $logExtra; |
| 36 |
parent::__construct(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* This function converts a long message into a string with the first N-words. |
| 41 |
* eg. from: "Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener" |
| 42 |
* to: "Notified event" |
| 43 |
* |
| 44 |
* @param string $message The message to shortify. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function getFirstWords($message) |
| 49 |
{ |
| 50 |
$glue = '-'; |
| 51 |
$pieces = explode(' ', $message); |
| 52 |
array_splice($pieces, $this->numberOfWords); |
| 53 |
$shortMessage = preg_replace("/[^A-Za-z0-9?![:space:]]/", "-", implode($glue, $pieces)); |
| 54 |
|
| 55 |
return $shortMessage; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* {@inheritdoc} |
| 60 |
*/ |
| 61 |
public function format(array $record) |
| 62 |
{ |
| 63 |
$vars = $this->normalize($record); |
| 64 |
|
| 65 |
$firstRow = $this->format; |
| 66 |
$output = array(); |
| 67 |
|
| 68 |
$vars['short_message'] = $this->getFirstWords($vars['message']); |
| 69 |
foreach ($vars as $var => $val) { |
| 70 |
$firstRow = str_replace('%' . $var . '%', $this->convertToString($val), $firstRow); |
| 71 |
} |
| 72 |
$output[] = $firstRow; |
| 73 |
// creating more rows for context content |
| 74 |
if ($this->logContext && isset($vars['context'])) { |
| 75 |
foreach ($vars['context'] as $key => $parameter) { |
| 76 |
if (!is_string($parameter)) { |
| 77 |
$parameter = json_encode($parameter); |
| 78 |
} |
| 79 |
$output[] = sprintf("%s.context.%s.%s", $firstRow, $key, $parameter); |
| 80 |
} |
| 81 |
} |
| 82 |
// creating more rows for extra content |
| 83 |
if ($this->logExtra && isset($vars['extra'])) { |
| 84 |
foreach ($vars['extra'] as $key => $parameter) { |
| 85 |
if (!is_string($parameter)) { |
| 86 |
$parameter = json_encode($parameter); |
| 87 |
} |
| 88 |
$output[] = sprintf("%s.extra.%s.%s", $firstRow, $key, $parameter); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $output; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* {@inheritdoc} |
| 97 |
*/ |
| 98 |
public function formatBatch(array $records) |
| 99 |
{ |
| 100 |
$output = array(); |
| 101 |
foreach ($records as $record) { |
| 102 |
$output = array_merge($output, $this->format($record)); |
| 103 |
} |
| 104 |
|
| 105 |
return $output; |
| 106 |
} |
| 107 |
} |