summaryrefslogtreecommitdiff
path: root/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2015-06-04 07:31:04 +0200
committerPierre Schmitz <pierre@archlinux.de>2015-06-04 07:58:39 +0200
commitf6d65e533c62f6deb21342d4901ece24497b433e (patch)
treef28adf0362d14bcd448f7b65a7aaf38650f923aa /vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog
parentc27b2e832fe25651ef2410fae85b41072aae7519 (diff)
Update to MediaWiki 1.25.1
Diffstat (limited to 'vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog')
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatter.php101
-rw-r--r--vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php120
2 files changed, 221 insertions, 0 deletions
diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatter.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatter.php
new file mode 100644
index 00000000..6311112a
--- /dev/null
+++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Formatter/StatsDFormatter.php
@@ -0,0 +1,101 @@
+<?php
+
+namespace Liuggio\StatsdClient\Monolog\Formatter;
+
+use Monolog\Formatter\LineFormatter;
+
+/**
+ * Formats incoming records in order to be a perfect StatsD key.
+ *
+ * This is especially useful for logging to files
+ *
+ * @author Jordi Boggiano <j.boggiano@seld.be>
+ * @author Christophe Coevoet <stof@notk.org>
+ * @author Giulio De Donato <liuggio@gmail.com>
+ */
+class StatsDFormatter extends LineFormatter
+{
+ const SIMPLE_FORMAT = "%channel%.%level_name%.%short_message%";
+
+ protected $numberOfWords;
+ protected $logContext;
+ protected $logExtra;
+
+ /**
+ * @param string $format The format of the message
+ * @param Boolean $logContext If true add multiple rows containing Context information
+ * @param Boolean $logExtra If true add multiple rows containing Extra information
+ * @param integer $numberOfWords The number of words to show.
+ */
+ public function __construct($format = null, $logContext = true, $logExtra = true, $numberOfWords = 2)
+ {
+ $this->format = $format ? : static::SIMPLE_FORMAT;
+ $this->numberOfWords = $numberOfWords;
+ $this->logContext = $logContext;
+ $this->logExtra = $logExtra;
+ parent::__construct();
+ }
+
+ /**
+ * This function converts a long message into a string with the first N-words.
+ * eg. from: "Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener"
+ * to: "Notified event"
+ *
+ * @param string $message The message to shortify.
+ *
+ * @return string
+ */
+ public function getFirstWords($message)
+ {
+ $glue = '-';
+ $pieces = explode(' ', $message);
+ array_splice($pieces, $this->numberOfWords);
+ $shortMessage = preg_replace("/[^A-Za-z0-9?![:space:]]/", "-", implode($glue, $pieces));
+
+ return $shortMessage;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function format(array $record)
+ {
+ $vars = $this->normalize($record);
+
+ $firstRow = $this->format;
+ $output = array();
+
+ $vars['short_message'] = $this->getFirstWords($vars['message']);
+ foreach ($vars as $var => $val) {
+ $firstRow = str_replace('%' . $var . '%', $this->convertToString($val), $firstRow);
+ }
+ $output[] = $firstRow;
+ // creating more rows for context content
+ if ($this->logContext && isset($vars['context'])) {
+ foreach ($vars['context'] as $key => $parameter) {
+ $output[] = sprintf("%s.context.%s.%s", $firstRow, $key, $parameter);
+ }
+ }
+ // creating more rows for extra content
+ if ($this->logExtra && isset($vars['extra'])) {
+ foreach ($vars['extra'] as $key => $parameter) {
+ $output[] = sprintf("%s.extra.%s.%s", $firstRow, $key, $parameter);
+ }
+ }
+
+ return $output;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function formatBatch(array $records)
+ {
+ $output = array();
+ foreach ($records as $record) {
+ $output = array_merge($output, $this->format($record));
+ }
+
+ return $output;
+ }
+} \ No newline at end of file
diff --git a/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php
new file mode 100644
index 00000000..86c6d399
--- /dev/null
+++ b/vendor/liuggio/statsd-php-client/src/Liuggio/StatsdClient/Monolog/Handler/StatsDHandler.php
@@ -0,0 +1,120 @@
+<?php
+
+namespace Liuggio\StatsdClient\Monolog\Handler;
+
+use Monolog\Logger;
+use Monolog\Handler\AbstractProcessingHandler;
+use Monolog\Formatter\FormatterInterface;
+
+use Liuggio\StatsdClient\Monolog\Formatter\StatsDFormatter;
+use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
+use Liuggio\StatsdClient\Factory\StatsdDataFactory;
+use Liuggio\StatsdClient\StatsdClientInterface;
+
+/**
+ * A processing handler for StatsD.
+ *
+ * @author Giulio De Donato <liuggio@gmail.com>
+ */
+class StatsDHandler extends AbstractProcessingHandler
+{
+ /**
+ * @var array
+ */
+ protected $buffer = array();
+
+ /**
+ * @var string
+ */
+ protected $prefix;
+
+ /**
+ * @var statsDService
+ */
+ protected $statsDService;
+
+ /**
+ * @var statsDFactory
+ */
+ protected $statsDFactory;
+
+ /**
+ * @param StatsdClientInterface $statsDService The Service sends the packet
+ * @param StatsdDataFactoryInterface $statsDFactory The Factory creates the StatsDPacket
+ * @param string $prefix Statsd key prefix
+ * @param integer $level The minimum logging level at which this handler will be triggered
+ * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
+ */
+ public function __construct(StatsdClientInterface $statsDService, StatsdDataFactoryInterface $statsDFactory = null, $prefix, $level = Logger::DEBUG, $bubble = true)
+ {
+ parent::__construct($level, $bubble);
+
+ $this->statsDService = $statsDService;
+ $this->statsDFactory = $statsDFactory ? $statsDFactory : new StatsdDataFactory();
+ $this->prefix = $prefix;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function close()
+ {
+ $this->statsDService->send($this->buffer);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function write(array $record)
+ {
+ $records = is_array($record['formatted']) ? $record['formatted'] : array($record['formatted']);
+
+ foreach ($records as $record) {
+ if (!empty($record)) {
+ $this->buffer[] = $this->statsDFactory->increment(sprintf('%s.%s', $this->getPrefix(), $record));
+ }
+ }
+ }
+
+ /**
+ * Gets the default formatter.
+ *
+ * @return FormatterInterface
+ */
+ protected function getDefaultFormatter()
+ {
+ return new StatsDFormatter();
+ }
+
+ /**
+ * @param string $prefix
+ */
+ public function setPrefix($prefix)
+ {
+ $this->prefix = $prefix;
+ }
+
+ /**
+ * @return string
+ */
+ public function getPrefix()
+ {
+ return $this->prefix;
+ }
+
+ /**
+ * @param StatsdClientInterface $statsDService
+ */
+ public function setStatsDService(StatsdClientInterface $statsDService)
+ {
+ $this->statsDService = $statsDService;
+ }
+
+ /**
+ * @param StatsdDataFactoryInterface $statsDFactory
+ */
+ public function setStatsDFactory(StatsdDataFactoryInterface $statsDFactory)
+ {
+ $this->statsDFactory = $statsDFactory;
+ }
+}