Jordi Boggiano пре 15 година
комит
ed6b0e32a2

+ 19 - 0
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.

+ 15 - 0
phpunit.xml.dist

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<phpunit bootstrap="tests/bootstrap.php">
+    <testsuites>
+        <testsuite name="Monolog Test Suite">
+            <directory>tests/Monolog/</directory>
+        </testsuite>
+    </testsuites>
+
+    <filter>
+        <whitelist>
+            <directory suffix=".php">src/Monolog/</directory>
+        </whitelist>
+    </filter>
+</phpunit>

+ 17 - 0
src/Monolog/Formatter/FormatterInterface.php

@@ -0,0 +1,17 @@
+<?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;
+
+interface FormatterInterface
+{
+    function format($log, $level, $message);
+}

+ 48 - 0
src/Monolog/Formatter/SimpleFormatter.php

@@ -0,0 +1,48 @@
+<?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;
+
+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;
+    }
+}

+ 56 - 0
src/Monolog/Log.php

@@ -0,0 +1,56 @@
+<?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;
+
+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;
+    }
+}

+ 70 - 0
src/Monolog/Logger.php

@@ -0,0 +1,70 @@
+<?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;
+
+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);
+    }
+}

+ 41 - 0
src/Monolog/Writer/FileWriter.php

@@ -0,0 +1,41 @@
+<?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\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;
+    }
+}

+ 29 - 0
src/Monolog/Writer/NullWriter.php

@@ -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\Writer;
+
+use Monolog\Formatter\FormatterInterface;
+
+class NullWriter implements WriterInterface
+{
+    public function write($log, $level, $message)
+    {
+    }
+
+    public function close()
+    {
+    }
+
+    public function setFormatter(FormatterInterface $formatter)
+    {
+    }
+}

+ 48 - 0
src/Monolog/Writer/StreamWriter.php

@@ -0,0 +1,48 @@
+<?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\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;
+    }
+}

+ 21 - 0
src/Monolog/Writer/WriterInterface.php

@@ -0,0 +1,21 @@
+<?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\Writer;
+
+use Monolog\Formatter\FormatterInterface;
+
+interface WriterInterface
+{
+    function setFormatter(FormatterInterface $formatter);
+    function write($log, $level, $message);
+    function close();
+}

+ 19 - 0
tests/bootstrap.php

@@ -0,0 +1,19 @@
+<?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.
+ */
+
+spl_autoload_register(function($class)
+{
+    $file = __DIR__.'/../src/'.strtr($class, '\\', '/').'.php';
+    if (file_exists($file)) {
+        require $file;
+        return true;
+    }
+});