1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-24 01:56:18 +02:00

Merge remote-tracking branch 'upstream/master' into couchdb

This commit is contained in:
Markus Bachmann
2012-11-21 19:11:25 +01:00
18 changed files with 200 additions and 59 deletions

View File

@@ -24,20 +24,24 @@ use Monolog\Logger;
class BufferHandler extends AbstractHandler
{
protected $handler;
protected $bufferSize;
protected $bufferSize = 0;
protected $bufferLimit;
protected $flushOnOverflow;
protected $buffer = array();
/**
* @param HandlerInterface $handler Handler.
* @param integer $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @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
* @param HandlerInterface $handler Handler.
* @param integer $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @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
* @param Boolean $flushOnOverflow If true, the buffer is flushed when the max size has been reached, by default oldest entries are discarded
*/
public function __construct(HandlerInterface $handler, $bufferSize = 0, $level = Logger::DEBUG, $bubble = true)
public function __construct(HandlerInterface $handler, $bufferSize = 0, $level = Logger::DEBUG, $bubble = true, $flushOnOverflow = false)
{
parent::__construct($level, $bubble);
$this->handler = $handler;
$this->bufferSize = $bufferSize;
$this->bufferLimit = (int) $bufferSize;
$this->flushOnOverflow = $flushOnOverflow;
// __destructor() doesn't get called on Fatal errors
register_shutdown_function(array($this, 'close'));
@@ -52,22 +56,36 @@ class BufferHandler extends AbstractHandler
return false;
}
$this->buffer[] = $record;
if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) {
array_shift($this->buffer);
if ($this->bufferLimit > 0 && $this->bufferSize === $this->bufferLimit) {
if ($this->flushOnOverflow) {
$this->flush();
} else {
array_shift($this->buffer);
$this->bufferSize--;
}
}
$this->buffer[] = $record;
$this->bufferSize++;
return false === $this->bubble;
}
public function flush()
{
if ($this->bufferSize === 0) {
return;
}
$this->handler->handleBatch($this->buffer);
$this->bufferSize = 0;
$this->buffer = array();
}
/**
* {@inheritdoc}
*/
public function close()
{
if ($this->buffer) {
$this->handler->handleBatch($this->buffer);
$this->buffer = array();
}
$this->flush();
}
}

View File

@@ -103,7 +103,7 @@ class CubeHandler extends AbstractProcessingHandler
{
$date = $record['datetime'];
$data = array('time' => $date->format('Y-m-d H:i:s'));
$data = array('time' => $date->format('Y-m-d\TH:i:s.u'));
unset($record['datetime']);
if (isset($record['context']['type'])) {
@@ -142,4 +142,4 @@ class CubeHandler extends AbstractProcessingHandler
return curl_exec($this->httpConnection);
}
}
}

View File

@@ -34,7 +34,7 @@ class FingersCrossedHandler extends AbstractHandler
protected $stopBuffering;
/**
* @param callback|HandlerInterface $handler Handler or factory callback($record, $fingersCrossedHandler).
* @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler).
* @param int|ActivationStrategyInterface $activationStrategy Strategy which determines when this handler takes action
* @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
@@ -79,10 +79,13 @@ class FingersCrossedHandler extends AbstractHandler
$this->buffering = false;
}
if (!$this->handler instanceof HandlerInterface) {
if (!is_callable($this->handler)) {
throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
}
$this->handler = call_user_func($this->handler, $record, $this);
}
if (!$this->handler instanceof HandlerInterface) {
throw new \RuntimeException("The factory callback should return a HandlerInterface");
if (!$this->handler instanceof HandlerInterface) {
throw new \RuntimeException("The factory callable should return a HandlerInterface");
}
}
$this->handler->handleBatch($this->buffer);
$this->buffer = array();

View File

@@ -25,6 +25,8 @@ interface HandlerInterface
*
* This is mostly done for performance reasons, to avoid calling processors for nothing.
*
* @param array $record
*
* @return Boolean
*/
public function isHandling(array $record);

View File

@@ -42,7 +42,7 @@ class NativeMailerHandler extends MailHandler
}
/**
* @param string|array $header Custom added headers
* @param string|array $headers Custom added headers
*/
public function addHeader($headers)
{

View File

@@ -190,7 +190,7 @@ class SocketHandler extends AbstractProcessingHandler
{
$seconds = floor($this->timeout);
$microseconds = round(($this->timeout - $seconds)*1e6);
return stream_set_timeout($this->resource, $seconds, $microseconds);
}

View File

@@ -25,7 +25,7 @@ class SwiftMailerHandler extends MailHandler
/**
* @param \Swift_Mailer $mailer The mailer to use
* @param callback|\Swift_Message $message An example message for real messages, only the body will be replaced
* @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced
* @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
*/
@@ -37,7 +37,7 @@ class SwiftMailerHandler extends MailHandler
$message = call_user_func($message);
}
if (!$message instanceof \Swift_Message) {
throw new \InvalidArgumentException('You must provide either a Swift_Message instance or a callback returning it');
throw new \InvalidArgumentException('You must provide either a Swift_Message instance or a callable returning it');
}
$this->message = $message;
}

View File

@@ -12,6 +12,8 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
/**
* Logs to syslog service.
@@ -64,8 +66,9 @@ class SyslogHandler extends AbstractProcessingHandler
* @param mixed $facility
* @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
* @param int $logopts Option flags for the openlog() call, defaults to LOG_PID
*/
public function __construct($ident, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true)
public function __construct($ident, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true, $logopts = LOG_PID)
{
parent::__construct($level, $bubble);
@@ -87,7 +90,7 @@ class SyslogHandler extends AbstractProcessingHandler
throw new \UnexpectedValueException('Unknown facility value "'.$facility.'" given');
}
if (!openlog($ident, LOG_PID, $facility)) {
if (!openlog($ident, $logopts, $facility)) {
throw new \LogicException('Can\'t open syslog for ident "'.$ident.'" and facility "'.$facility.'"');
}
}