commit ed6b0e32a262b57ccdacf1145c225350f6e68eb9 Author: Jordi Boggiano Date: Thu Feb 17 02:08:00 2011 +0100 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..5df1c397 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) Jordi Boggiano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 00000000..9521feb4 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,15 @@ + + + + + + tests/Monolog/ + + + + + + src/Monolog/ + + + diff --git a/src/Monolog/Formatter/FormatterInterface.php b/src/Monolog/Formatter/FormatterInterface.php new file mode 100644 index 00000000..309f0fec --- /dev/null +++ b/src/Monolog/Formatter/FormatterInterface.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Formatter; + +interface FormatterInterface +{ + function format($log, $level, $message); +} diff --git a/src/Monolog/Formatter/SimpleFormatter.php b/src/Monolog/Formatter/SimpleFormatter.php new file mode 100644 index 00000000..bf0ee287 --- /dev/null +++ b/src/Monolog/Formatter/SimpleFormatter.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Formatter; + +class SimpleFormatter implements FormatterInterface +{ + const SIMPLE_FORMAT = "[%date%] %log%.%level%: %message%\n"; + const SIMPLE_DATE = "Y-m-d H:i:s"; + + protected $format; + protected $dateFormat; + + public function __construct($format = null, $dateFormat = null) + { + $this->format = $format ?: self::SIMPLE_FORMAT; + $this->dateFormat = $dateFormat ?: self::SIMPLE_DATE; + } + + public function format($log, $level, $message) + { + $defaults = array( + 'log' => $log, + 'level' => $level, + 'date' => date($this->dateFormat), + ); + + if (is_array($message)) { + $vars = array_merge($defaults, $message); + } else { + $vars = $defaults; + $vars['message'] = $message; + } + + foreach ($vars as $var => $val) { + $message = str_replace('%'.$var.'%', $val, $message); + } + return $message; + } +} diff --git a/src/Monolog/Log.php b/src/Monolog/Log.php new file mode 100644 index 00000000..dfba48fd --- /dev/null +++ b/src/Monolog/Log.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog; + +use Monolog\Writer\WriterInterface; + +class Log +{ + protected $name; + protected $writers; + + public function __construct($name, $writers = array()) + { + $this->name = $name; + $this->writers = $writers; + } + + public function getName() + { + return $this->name; + } + + public function addWriter(WriterInterface $writer) + { + $this->writers[] = $writer; + } + + public function log($level, $message) + { + if ($level < $this->level) { + return; + } + foreach ($this->writers as $writer) { + $writer->write($this->name, $level, $message); + } + } + + public function setLevel($level) + { + $this->level = $level; + } + + public function getLevel() + { + return $level; + } +} diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php new file mode 100644 index 00000000..39929cb5 --- /dev/null +++ b/src/Monolog/Logger.php @@ -0,0 +1,70 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog; + +class Logger +{ + const DEBUG = 1; + const INFO = 5; + const WARN = 10; + const ERROR = 15; + const FATAL = 20; + + protected $logs; + + public function __construct($logs = array()) + { + $this->logs = $logs; + } + + public function addLog(Log $log) + { + $this->logs[$log->getName()] = $log; + } + + public function log($level, $message, $log = null) + { + if (null === $log) { + $logs = $this->logs; + } else { + $logs = (array) $log; + } + foreach ($logs as $log => $dummy) { + $this->logs[$log]->log($level, $message); + } + } + + public function debug($message, $log = null) + { + $this->log(self::DEBUG, $message, $log); + } + + public function info($message, $log = null) + { + $this->log(self::INFO, $message, $log); + } + + public function warn($message, $log = null) + { + $this->log(self::WARN, $message, $log); + } + + public function error($message, $log = null) + { + $this->log(self::ERROR, $message, $log); + } + + public function fatal($message, $log = null) + { + $this->log(self::FATAL, $message, $log); + } +} \ No newline at end of file diff --git a/src/Monolog/Writer/FileWriter.php b/src/Monolog/Writer/FileWriter.php new file mode 100644 index 00000000..032e7033 --- /dev/null +++ b/src/Monolog/Writer/FileWriter.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Writer; + +class FileWriter extends StreamWriter +{ + 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(); + // TODO rotation + } + + public function setRotation($rotation) + { + $this->rotation = $rotation; + } + + public function setMaxAge($maxAge) + { + $this->maxAge = $maxAge; + } +} \ No newline at end of file diff --git a/src/Monolog/Writer/NullWriter.php b/src/Monolog/Writer/NullWriter.php new file mode 100644 index 00000000..c4d096cf --- /dev/null +++ b/src/Monolog/Writer/NullWriter.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Writer; + +use Monolog\Formatter\FormatterInterface; + +class NullWriter implements WriterInterface +{ + public function write($log, $level, $message) + { + } + + public function close() + { + } + + public function setFormatter(FormatterInterface $formatter) + { + } +} \ No newline at end of file diff --git a/src/Monolog/Writer/StreamWriter.php b/src/Monolog/Writer/StreamWriter.php new file mode 100644 index 00000000..faae5697 --- /dev/null +++ b/src/Monolog/Writer/StreamWriter.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Writer; + +use Monolog\Formatter\FormatterInterface; + +class StreamWriter implements WriterInterface +{ + protected $formatter; + protected $stream; + protected $url; + + public function __construct($streamUrl) + { + if (is_resource($streamUrl)) { + $this->stream = $streamUrl; + } else { + $this->url = $streamUrl; + } + } + + public function write($log, $level, $message) + { + if (null === $this->stream) { + $this->stream = fopen($this->url, 'a'); + } + fwrite($this->stream, $this->formatter->format($log, $level, $message)); + } + + public function close() + { + fclose($this->stream); + } + + public function setFormatter(FormatterInterface $formatter) + { + $this->formatter = $formatter; + } +} \ No newline at end of file diff --git a/src/Monolog/Writer/WriterInterface.php b/src/Monolog/Writer/WriterInterface.php new file mode 100644 index 00000000..cd6ad5d5 --- /dev/null +++ b/src/Monolog/Writer/WriterInterface.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Writer; + +use Monolog\Formatter\FormatterInterface; + +interface WriterInterface +{ + function setFormatter(FormatterInterface $formatter); + function write($log, $level, $message); + function close(); +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..d6eaa6b4 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +spl_autoload_register(function($class) +{ + $file = __DIR__.'/../src/'.strtr($class, '\\', '/').'.php'; + if (file_exists($file)) { + require $file; + return true; + } +});