mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-04 12:17:35 +02:00
Added docblocks and fixed a couple tests
This commit is contained in:
@@ -11,6 +11,11 @@
|
|||||||
|
|
||||||
namespace Monolog\Formatter;
|
namespace Monolog\Formatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for formatters
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
interface FormatterInterface
|
interface FormatterInterface
|
||||||
{
|
{
|
||||||
function format($message);
|
function format($message);
|
||||||
|
@@ -13,11 +13,18 @@ namespace Monolog\Formatter;
|
|||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes whatever message data is passed to it as json
|
||||||
|
*
|
||||||
|
* This can be useful to log to databases or remote APIs
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
class JsonFormatter implements FormatterInterface
|
class JsonFormatter implements FormatterInterface
|
||||||
{
|
{
|
||||||
public function format($message)
|
public function format($message)
|
||||||
{
|
{
|
||||||
$message['message'] = json_encode($message['message']);
|
$message['message'] = json_encode($message);
|
||||||
return $message;
|
return $message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -13,6 +13,13 @@ namespace Monolog\Formatter;
|
|||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats incoming messages into a one-line string
|
||||||
|
*
|
||||||
|
* This is especially useful for logging to files
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
class LineFormatter implements FormatterInterface
|
class LineFormatter implements FormatterInterface
|
||||||
{
|
{
|
||||||
const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message%\n";
|
const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message%\n";
|
||||||
|
@@ -14,6 +14,13 @@ namespace Monolog\Handler;
|
|||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Monolog\Formatter\LineFormatter;
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base Handler class providing the Handler structure
|
||||||
|
*
|
||||||
|
* Classes extending it should (in most cases) only implement write($message)
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
abstract class AbstractHandler implements HandlerInterface
|
abstract class AbstractHandler implements HandlerInterface
|
||||||
{
|
{
|
||||||
protected $level;
|
protected $level;
|
||||||
|
@@ -14,7 +14,7 @@ namespace Monolog\Handler;
|
|||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FingersCrossedHandler buffers all messages until a certain level is reached
|
* Buffers all messages until a certain level is reached
|
||||||
*
|
*
|
||||||
* The advantage of this approach is that you don't get any clutter in your log files.
|
* The advantage of this approach is that you don't get any clutter in your log files.
|
||||||
* Only requests which actually trigger an error (or whatever your actionLevel is) will be
|
* Only requests which actually trigger an error (or whatever your actionLevel is) will be
|
||||||
@@ -51,7 +51,7 @@ class FingersCrossedHandler extends AbstractHandler
|
|||||||
* on, unless reset() is called, all messages are passed to the wrapped handler.
|
* on, unless reset() is called, all messages are passed to the wrapped handler.
|
||||||
*
|
*
|
||||||
* @param array $message Message
|
* @param array $message Message
|
||||||
* @return Boolean Whether the next handler in the stack should be called.
|
* @return Boolean Whether the message was handled
|
||||||
*/
|
*/
|
||||||
public function handle($message)
|
public function handle($message)
|
||||||
{
|
{
|
||||||
@@ -73,7 +73,10 @@ class FingersCrossedHandler extends AbstractHandler
|
|||||||
} else {
|
} else {
|
||||||
$this->handler->handle($message);
|
$this->handler->handle($message);
|
||||||
}
|
}
|
||||||
return false === $this->bubble;
|
if ($this->bubble && $this->parent) {
|
||||||
|
$this->parent->handle($originalMessage);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -11,6 +11,11 @@
|
|||||||
|
|
||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface that all Monolog Handlers must implement
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
interface HandlerInterface
|
interface HandlerInterface
|
||||||
{
|
{
|
||||||
public function getHandler($message);
|
public function getHandler($message);
|
||||||
|
@@ -13,14 +13,25 @@ namespace Monolog\Handler;
|
|||||||
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blackhole
|
||||||
|
*
|
||||||
|
* Any message it can handle will be thrown away. This can be used
|
||||||
|
* to put on top of an existing stack to override it temporarily.
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
class NullHandler extends AbstractHandler
|
class NullHandler extends AbstractHandler
|
||||||
{
|
{
|
||||||
public function handle($message)
|
public function handle($message)
|
||||||
{
|
{
|
||||||
if ($message['level'] < $this->level) {
|
if ($message['level'] < $this->level) {
|
||||||
return false;
|
return $this->parent ? $this->parent->handle($message) : false;
|
||||||
}
|
}
|
||||||
return false === $this->bubble;
|
if ($this->bubble && $this->parent) {
|
||||||
|
$this->parent->handle($originalMessage);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function write($message)
|
public function write($message)
|
||||||
|
@@ -11,6 +11,11 @@
|
|||||||
|
|
||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores logs to files that are rotated every n day/week/month
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
class RotatingFileHandler extends StreamHandler
|
class RotatingFileHandler extends StreamHandler
|
||||||
{
|
{
|
||||||
protected $rotation;
|
protected $rotation;
|
||||||
|
@@ -14,6 +14,13 @@ namespace Monolog\Handler;
|
|||||||
use Monolog\Formatter\SimpleFormatter;
|
use Monolog\Formatter\SimpleFormatter;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores to any stream resource
|
||||||
|
*
|
||||||
|
* Can be used to store into php://stderr, remote and local files, etc.
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
class StreamHandler extends AbstractHandler
|
class StreamHandler extends AbstractHandler
|
||||||
{
|
{
|
||||||
protected $stream;
|
protected $stream;
|
||||||
|
@@ -14,7 +14,7 @@ namespace Monolog\Handler;
|
|||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TestHandler is used for testing purposes.
|
* Used for testing purposes.
|
||||||
*
|
*
|
||||||
* It records all messages and gives you access to them for verification.
|
* It records all messages and gives you access to them for verification.
|
||||||
*
|
*
|
||||||
|
@@ -14,6 +14,14 @@ namespace Monolog;
|
|||||||
use Monolog\Handler\HandlerInterface;
|
use Monolog\Handler\HandlerInterface;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Monolog log channel
|
||||||
|
*
|
||||||
|
* It contains a stack of Handlers and a stack of Processors,
|
||||||
|
* and uses them to store messages that are added to it.
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
class Logger
|
class Logger
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@@ -11,6 +11,11 @@
|
|||||||
|
|
||||||
namespace Monolog\Processor;
|
namespace Monolog\Processor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Injects url/method and remote IP of the current web request in all messages
|
||||||
|
*
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
|
*/
|
||||||
class WebProcessor
|
class WebProcessor
|
||||||
{
|
{
|
||||||
public function __invoke($message, $handler)
|
public function __invoke($message, $handler)
|
||||||
|
@@ -18,13 +18,13 @@ class JsonFormatterTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testFormat()
|
public function testFormat()
|
||||||
{
|
{
|
||||||
$formatter = new JsonFormatter();
|
$formatter = new JsonFormatter();
|
||||||
$message = $formatter->format(array(
|
$message = $formatter->format($msg = array(
|
||||||
'level_name' => 'WARNING',
|
'level_name' => 'WARNING',
|
||||||
'channel' => 'log',
|
'channel' => 'log',
|
||||||
'message' => array('foo'),
|
'message' => array('foo'),
|
||||||
'datetime' => new \DateTime,
|
'datetime' => new \DateTime,
|
||||||
'extra' => array(),
|
'extra' => array(),
|
||||||
));
|
));
|
||||||
$this->assertEquals(json_encode(array('foo')), $message['message']);
|
$this->assertEquals(json_encode($msg), $message['message']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -27,12 +27,6 @@ class NullHandlerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($handler->handle($this->getMessage(Logger::DEBUG)));
|
$this->assertFalse($handler->handle($this->getMessage(Logger::DEBUG)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHandleBubbling()
|
|
||||||
{
|
|
||||||
$handler = new NullHandler(Logger::DEBUG, true);
|
|
||||||
$this->assertFalse($handler->handle($this->getMessage()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* No-op test for coverage
|
* No-op test for coverage
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user