Permalink
Browse files

Major refactoring to follow the Logbook model

  • Loading branch information...
1 parent 860194e commit 3fa6e4b91f8ce5dfdbad3560e716fb11f8c62fe1 @Seldaek committed Feb 20, 2011
View
@@ -5,28 +5,20 @@ Usage
-----
use Monolog\Logger;
- use Monolog\Log;
- use Monolog\Writer\FileWriter;
- use Monolog\Formatter\SimpleFormatter;
+ use Monolog\Handler\FileHandler;
- // create a log with writers/formatters
- $writer = new FileWriter('path/to/your.log');
- $writer->setFormatter(new SimpleFormatter());
- $log = new Log('name', Logger::WARN, $writer);
+ // create a log channel
+ $log = new Logger('name');
+ $log->pushHandler(new FileHandler('path/to/your.log', Logger::WARNING));
- // create a logger with one or more logs
- $logger = new Logger(array($log));
-
- // write to all logs
- $logger->addWarning('Foo');
-
- // write only to the 'name' log
- $logger->addError('Bar', 'name');
+ // add messages to the log
+ $log->addWarning('Foo');
+ $log->addError('Bar');
Todo
----
-- Log rotation for FileWriter
+- Log rotation for RotatingFileHandler
- FirePHP writer
- Syslog writer
@@ -43,4 +35,9 @@ Monolog is licensed under the MIT License - see the LICENSE file for details
Requirements
------------
-Any flavor of PHP5.3 should do
+Any flavor of PHP 5.3 should do
+
+Acknowledgements
+----------------
+
+This library is heavily inspired by Python's Logbook library, although it has been adapted to fit in PHP.
@@ -13,5 +13,5 @@
interface FormatterInterface
{
- function format($log, $message);
+ function format($message);
}
@@ -0,0 +1,23 @@
+<?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\Formatter;
+
+use Monolog\Logger;
+
+class JsonFormatter implements FormatterInterface
+{
+ public function format($message)
+ {
+ $message['message'] = json_encode($message['message']);
+ return $message;
+ }
+}
@@ -13,9 +13,9 @@
use Monolog\Logger;
-class SimpleFormatter implements FormatterInterface
+class LineFormatter implements FormatterInterface
{
- const SIMPLE_FORMAT = "[%date%] %log%.%level%: %message%\n";
+ const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message%\n";
const SIMPLE_DATE = "Y-m-d H:i:s";
protected $format;
@@ -27,25 +27,26 @@ public function __construct($format = null, $dateFormat = null)
$this->dateFormat = $dateFormat ?: self::SIMPLE_DATE;
}
- public function format($log, $message)
+ public function format($message)
{
- $defaults = array(
- 'log' => $log,
- 'level' => Logger::getLevelName($message['level']),
- 'date' => date($this->dateFormat),
- );
+ $vars = $message;
+ $vars['datetime'] = $vars['datetime']->format($this->dateFormat);
if (is_array($message['message'])) {
- $vars = array_merge($defaults, $message['message']);
- } else {
- $vars = $defaults;
- $vars['message'] = $message['message'];
+ unset($vars['message']);
+ $vars = array_merge($vars, $message['message']);
}
- $message = $this->format;
+ $output = $this->format;
foreach ($vars as $var => $val) {
- $message = str_replace('%'.$var.'%', $val, $message);
+ if (!is_array($val)) {
+ $output = str_replace('%'.$var.'%', $val, $output);
+ }
}
+ foreach ($vars['extra'] as $var => $val) {
+ $output = str_replace('%extra.'.$var.'%', $val, $output);
+ }
+ $message['message'] = $output;
return $message;
}
}
@@ -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();
+ }
+}
@@ -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);
+}
@@ -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)
+ {
+ }
+}
@@ -9,20 +9,13 @@
* file that was distributed with this source code.
*/
-namespace Monolog\Writer;
+namespace Monolog\Handler;
-class FileWriter extends StreamWriter
+class RotatingFileHandler extends StreamHandler
{
protected $rotation;
protected $maxAge;
- public function __construct($file, $rotation = null, $maxAge = null)
- {
- parent::__construct($file);
- $this->rotation = $rotation;
- $this->maxAge = $maxAge;
- }
-
public function close()
{
parent::close();
@@ -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;
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 3fa6e4b

Please sign in to comment.