diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2015-02-02 01:45:32 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2015-02-12 00:12:19 +0300 |
commit | e122a4e37b3eef21fc6fc32ed7d31c56c38d673f (patch) | |
tree | 89aa9600a70200f1e94cf0454d62d037f0e361e2 /plugins/Monolog/Formatter | |
parent | 47c0eae3f8202430ea576e09cf0ded90f783eea1 (diff) |
Moved the monolog configuration and code into a Monolog plugin
Diffstat (limited to 'plugins/Monolog/Formatter')
-rw-r--r-- | plugins/Monolog/Formatter/LineMessageFormatter.php | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/plugins/Monolog/Formatter/LineMessageFormatter.php b/plugins/Monolog/Formatter/LineMessageFormatter.php new file mode 100644 index 0000000000..6b81f2daec --- /dev/null +++ b/plugins/Monolog/Formatter/LineMessageFormatter.php @@ -0,0 +1,75 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\Monolog\Formatter; + +use Monolog\Formatter\FormatterInterface; + +/** + * Formats a log message into a line of text using our custom Piwik log format. + */ +class LineMessageFormatter implements FormatterInterface +{ + /** + * The log message format string that turns a tag name, date-time and message into + * one string to log. + * + * @var string + */ + private $logMessageFormat; + + /** + * @param string $logMessageFormat + */ + public function __construct($logMessageFormat) + { + $this->logMessageFormat = $logMessageFormat; + } + + public function format(array $record) + { + $class = isset($record['extra']['class']) ? $record['extra']['class'] : ''; + $date = $record['datetime']->format('Y-m-d H:i:s'); + + $message = $this->prefixMessageWithRequestId($record); + + $message = str_replace( + array('%tag%', '%message%', '%datetime%', '%level%'), + array($class, $message, $date, $record['level_name']), + $this->logMessageFormat + ); + + $message = str_replace("\n", "\n ", $message); + + $message .= "\n"; + + return $message; + } + + public function formatBatch(array $records) + { + foreach ($records as $key => $record) { + $records[$key] = $this->format($record); + } + + return $records; + } + + private function prefixMessageWithRequestId(array $record) + { + $requestId = isset($record['extra']['request_id']) ? $record['extra']['request_id'] : ''; + + $message = trim($record['message']); + + if ($requestId) { + $message = '[' . $requestId . '] ' . $message; + } + + return $message; + } +} |