3 namespace Liuggio\StatsdClient\Monolog\Handler;
6 use Monolog\Handler\AbstractProcessingHandler;
7 use Monolog\Formatter\FormatterInterface;
9 use Liuggio\StatsdClient\Monolog\Formatter\StatsDFormatter;
10 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
11 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
12 use Liuggio\StatsdClient\StatsdClientInterface;
15 * A processing handler for StatsD.
17 * @author Giulio De Donato <liuggio@gmail.com>
19 class StatsDHandler extends AbstractProcessingHandler
24 protected $buffer = array();
34 protected $statsDService;
39 protected $statsDFactory;
42 * @param StatsdClientInterface $statsDService The Service sends the packet
43 * @param StatsdDataFactoryInterface $statsDFactory The Factory creates the StatsDPacket
44 * @param string $prefix Statsd key prefix
45 * @param integer $level The minimum logging level at which this handler will be triggered
46 * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
48 public function __construct(StatsdClientInterface $statsDService, StatsdDataFactoryInterface $statsDFactory = null, $prefix, $level = Logger::DEBUG, $bubble = true)
50 parent::__construct($level, $bubble);
52 $this->statsDService = $statsDService;
53 $this->statsDFactory = $statsDFactory ? $statsDFactory : new StatsdDataFactory();
54 $this->prefix = $prefix;
60 public function close()
62 $this->statsDService->send($this->buffer);
68 protected function write(array $record)
70 $records = is_array($record['formatted']) ? $record['formatted'] : array($record['formatted']);
72 foreach ($records as $record) {
73 if (!empty($record)) {
74 $this->buffer[] = $this->statsDFactory->increment(sprintf('%s.%s', $this->getPrefix(), $record));
80 * Gets the default formatter.
82 * @return FormatterInterface
84 protected function getDefaultFormatter()
86 return new StatsDFormatter();
90 * @param string $prefix
92 public function setPrefix($prefix)
94 $this->prefix = $prefix;
100 public function getPrefix()
102 return $this->prefix;
106 * @param StatsdClientInterface $statsDService
108 public function setStatsDService(StatsdClientInterface $statsDService)
110 $this->statsDService = $statsDService;
114 * @param StatsdDataFactoryInterface $statsDFactory
116 public function setStatsDFactory(StatsdDataFactoryInterface $statsDFactory)
118 $this->statsDFactory = $statsDFactory;