mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-24 18:16:10 +02:00
Major refactoring to follow the Logbook model
This commit is contained in:
121
src/Monolog/Handler/AbstractHandler.php
Normal file
121
src/Monolog/Handler/AbstractHandler.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Monolog\Formatter\LineFormatter;
|
||||
|
||||
abstract class AbstractHandler implements HandlerInterface
|
||||
{
|
||||
protected $level;
|
||||
protected $bubble;
|
||||
protected $parent;
|
||||
|
||||
protected $formatter;
|
||||
protected $processor;
|
||||
|
||||
public function __construct($level = Logger::DEBUG, $bubble = false)
|
||||
{
|
||||
$this->level = $level;
|
||||
$this->bubble = $bubble;
|
||||
}
|
||||
|
||||
public function handle($message)
|
||||
{
|
||||
if ($message['level'] < $this->level) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->processor) {
|
||||
$message = call_user_func($this->processor, $message, $this);
|
||||
}
|
||||
|
||||
if (!$this->formatter) {
|
||||
$this->formatter = $this->getDefaultFormatter();
|
||||
}
|
||||
$message = $this->formatter->format($message);
|
||||
|
||||
$this->write($message);
|
||||
return false === $this->bubble;
|
||||
}
|
||||
|
||||
abstract public function write($message);
|
||||
|
||||
public function close()
|
||||
{
|
||||
}
|
||||
|
||||
public function setProcessor($callback)
|
||||
{
|
||||
$this->processor = $callback;
|
||||
}
|
||||
|
||||
public function getProcessor()
|
||||
{
|
||||
return $this->processor;
|
||||
}
|
||||
|
||||
public function setFormatter($formatter)
|
||||
{
|
||||
$this->formatter = $formatter;
|
||||
}
|
||||
|
||||
public function getFormatter()
|
||||
{
|
||||
return $this->formatter;
|
||||
}
|
||||
|
||||
public function setLevel($level)
|
||||
{
|
||||
$this->level = $level;
|
||||
}
|
||||
|
||||
public function getLevel()
|
||||
{
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
public function setBubble($bubble)
|
||||
{
|
||||
$this->bubble = $bubble;
|
||||
}
|
||||
|
||||
public function getBubble()
|
||||
{
|
||||
return $this->bubble;
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return $this->parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the parent handler
|
||||
*
|
||||
* @param Monolog\Handler\HandlerInterface
|
||||
*/
|
||||
public function setParent(HandlerInterface $parent)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->close();
|
||||
}
|
||||
|
||||
protected function getDefaultFormatter()
|
||||
{
|
||||
return new LineFormatter();
|
||||
}
|
||||
}
|
||||
29
src/Monolog/Handler/HandlerInterface.php
Normal file
29
src/Monolog/Handler/HandlerInterface.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
interface HandlerInterface
|
||||
{
|
||||
public function handle($message);
|
||||
|
||||
public function setLevel($level);
|
||||
|
||||
public function getLevel();
|
||||
|
||||
public function setBubble($bubble);
|
||||
|
||||
public function getBubble();
|
||||
|
||||
public function getParent();
|
||||
|
||||
public function setParent(HandlerInterface $parent);
|
||||
}
|
||||
29
src/Monolog/Handler/NullHandler.php
Normal file
29
src/Monolog/Handler/NullHandler.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Logger;
|
||||
|
||||
class NullHandler extends AbstractHandler
|
||||
{
|
||||
public function handle($message)
|
||||
{
|
||||
if ($message['level'] < $this->level) {
|
||||
return false;
|
||||
}
|
||||
return false === $this->bubble;
|
||||
}
|
||||
|
||||
public function write($message)
|
||||
{
|
||||
}
|
||||
}
|
||||
34
src/Monolog/Handler/RotatingFileHandler.php
Normal file
34
src/Monolog/Handler/RotatingFileHandler.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
class RotatingFileHandler extends StreamHandler
|
||||
{
|
||||
protected $rotation;
|
||||
protected $maxAge;
|
||||
|
||||
public function close()
|
||||
{
|
||||
parent::close();
|
||||
// TODO rotation
|
||||
}
|
||||
|
||||
public function setRotation($rotation)
|
||||
{
|
||||
$this->rotation = $rotation;
|
||||
}
|
||||
|
||||
public function setMaxAge($maxAge)
|
||||
{
|
||||
$this->maxAge = $maxAge;
|
||||
}
|
||||
}
|
||||
53
src/Monolog/Handler/StreamHandler.php
Normal file
53
src/Monolog/Handler/StreamHandler.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Formatter\SimpleFormatter;
|
||||
use Monolog\Logger;
|
||||
|
||||
class StreamHandler extends AbstractHandler
|
||||
{
|
||||
protected $stream;
|
||||
protected $url;
|
||||
|
||||
public function __construct($stream, $level = Logger::DEBUG, $bubble = true)
|
||||
{
|
||||
parent::__construct($level, $bubble);
|
||||
if (is_resource($stream)) {
|
||||
$this->stream = $stream;
|
||||
} else {
|
||||
$this->url = $stream;
|
||||
}
|
||||
}
|
||||
|
||||
public function write($message)
|
||||
{
|
||||
if (null === $this->stream) {
|
||||
if (!$this->url) {
|
||||
throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().');
|
||||
}
|
||||
$this->stream = fopen($this->url, 'a');
|
||||
if (!is_resource($this->stream)) {
|
||||
throw new \UnexpectedValueException('The stream could not be opened, "'.$this->url.'" may be an invalid url.');
|
||||
}
|
||||
}
|
||||
fwrite($this->stream, (string) $message['message']);
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
if (null !== $this->stream) {
|
||||
fclose($this->stream);
|
||||
$this->stream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user