Permalink
Browse files

Add first prototype

  • Loading branch information...
chEbba committed Sep 25, 2011
0 parents commit b559a2a1e568c316a49681cfa5bd91122246fefe
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (C) 2011 by Kirill chEbba Chebunin
+
+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.
@@ -0,0 +1,32 @@
+<?php
+/*
+ * Copyright (c) 2011
+ * Kirill chEbba Cheunin <iam@chebba.org>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this package in the file LICENSE.
+ */
+
+namespace Che\LogStock\Adapter;
+
+use Che\LogStock\Logger;
+
+/**
+ * Description of DefaultLoggerAdapter
+ *
+ * @author Kirill chEbba Chebunin <iam@chebba.org>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+class DefaultLoggerAdapter implements LoggerAdapter
+{
+ public function log($level, $message, array $context = array())
+ {
+ $levels = Logger::getLevels();
+ if (!isset($levels[$level])) {
+ throw new \InvalidArgumentException('Unknown level');
+ }
+
+ $message = sprintf('[%s] %s | %s', $levels[$level], $message, json_encode($context));
+ error_log($message);
+ }
+}
@@ -0,0 +1,23 @@
+<?php
+/*
+ * License
+ */
+
+namespace Che\LogStock\Adapter;
+
+/**
+ * Description of LoggerAdapter
+ *
+ * @author Kirill chEbba Chebunin <iam at chebba.org>
+ */
+interface LoggerAdapter
+{
+ /**
+ * Log message
+ *
+ * @param int $level Log level one of the Logger::* constants
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function log($level, $message, array $context = array());
+}
@@ -0,0 +1,48 @@
+<?php
+/*
+ * Copyright (c) 2011
+ * Kirill chEbba Cheunin <iam@chebba.org>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this package in the file LICENSE.
+ */
+
+namespace Che\LogStock\Loader;
+
+/**
+ * Description of HierarchicalLoggerLoader
+ *
+ * @author Kirill chEbba Chebunin <iam@chebba.org>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+class HierarchicalLoggerLoader implements LoggerLoader
+{
+ private $loader;
+
+ private $separator;
+
+ public function __construct(LoggerLoader $loader, $separator = '\\')
+ {
+ $this->loader = $loader;
+ $this->separator = $separator;
+ }
+
+ public function load($name)
+ {
+ while ($name) {
+ $logger = $this->loader->load($name);
+ if ($logger) {
+ return $logger;
+ }
+
+ $name = $this->getParentName($name);
+ }
+
+ return null;
+ }
+
+ public function getParentName($name)
+ {
+ return (string) substr($name, 0, strrpos($name, $this->separator));
+ }
+}
@@ -0,0 +1,28 @@
+<?php
+/*
+ * Copyright (c) 2011
+ * Kirill chEbba Cheunin <iam@chebba.org>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this package in the file LICENSE.
+ */
+
+namespace Che\LogStock\Loader;
+
+/**
+ * Description of LoggerLoader
+ *
+ * @author Kirill chEbba Chebunin <iam@chebba.org>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+interface LoggerLoader
+{
+ /**
+ * Load logger by name
+ *
+ * @param string $name
+ *
+ * @return Logger|null Logger by name or null if no logger found
+ */
+ public function load($name);
+}
@@ -0,0 +1,25 @@
+<?php
+/*
+ * Copyright (c) 2011
+ * Kirill chEbba Cheunin <iam@chebba.org>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this package in the file LICENSE.
+ */
+
+namespace Che\LogStock\Loader;
+
+/**
+ * Description of NullLoggerLoader
+ *
+ * @author Kirill chEbba Chebunin <iam@chebba.org>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+class NullLoggerLoader implements LoggerLoader
+{
+ public function load($name)
+ {
+ return null;
+ }
+
+}
@@ -0,0 +1,174 @@
+<?php
+/*
+ * Copyright (c) 2011
+ * Kirill chEbba Cheunin <iam@chebba.org>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this package in the file LICENSE.
+ */
+
+namespace Che\LogStock;
+
+use Che\LogStock\Adapter\LoggerAdapter;
+
+/**
+ * Description of Logger
+ *
+ * @author Kirill chEbba Chebunin <iam@chebba.org>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+class Logger
+{
+ /** Emergency: system is unusable */
+ const EMERG = 0;
+ /** Alert: action must be taken immediately */
+ const ALERT = 1;
+ /** Critical: critical conditions */
+ const CRIT = 2;
+ /** Error: error conditions */
+ const ERR = 3;
+ /** Warning: warning conditions */
+ const WARN = 4;
+ /** Notice: normal but significant condition */
+ const NOTICE = 5;
+ /** Informational: informational messages */
+ const INFO = 6;
+ /** Debug: debug messages */
+ const DEBUG = 7;
+
+ private $adapter;
+
+ /**
+ * Constructor
+ *
+ * @param LoggerAdapter $adapter
+ */
+ public function __construct(LoggerAdapter $adapter)
+ {
+ $this->adapter = $adapter;
+ }
+
+ /**
+ * Get adapter for this logger
+ *
+ * @return LoggerAdapter
+ */
+ public function getAdapter()
+ {
+ return $this->adapter;
+ }
+
+ public static function getLevels()
+ {
+ return array(
+ Logger::EMERG => 'EMERG',
+ Logger::ALERT => 'ALERT',
+ Logger::CRIT => 'CRIT',
+ Logger::ERR => 'ERR',
+ Logger::WARN => 'WARN',
+ Logger::NOTICE => 'NOTICE',
+ Logger::INFO => 'INFO',
+ Logger::DEBUG => 'DEBUG'
+ );
+ }
+
+ /**
+ * Log message
+ *
+ * @param int $level Log level one of the Logger::* constants
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function log($level, $message, array $context = array())
+ {
+ $this->adapter->log($level, $message, $context);
+ }
+
+ /**
+ * Log message with Emergency level
+ *
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function emerg($message, array $context = array())
+ {
+ $this->log(self::EMERG, $message, $context);
+ }
+
+ /**
+ * Log message with Alert level
+ *
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function alert($message, array $context = array())
+ {
+ $this->log(self::ALERT, $message, $context);
+ }
+
+ /**
+ * Log message with Critical level
+ *
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function crit($message, array $context = array())
+ {
+ $this->log(self::CRIT, $message, $context);
+ }
+
+ /**
+ * Log message with Error level
+ *
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function err($message, array $context = array())
+ {
+ $this->log(self::ERR, $message, $context);
+ }
+
+ /**
+ * Log message with Warning level
+ *
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function warn($message, array $context = array())
+ {
+ $this->log(self::WARN, $message, $context);
+ }
+
+ /**
+ * Log message with Notice level
+ *
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function notice($message, array $context = array())
+ {
+ $this->log(self::NOTICE, $message, $context);
+ }
+
+ /**
+ * Log message with Informational level
+ *
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function info($message, array $context = array())
+ {
+ $this->log(self::INFO, $message, $context);
+ }
+
+ /**
+ * Log message with Debug level
+ *
+ * @param string $message Log message
+ * @param array $context An array of extra information
+ */
+ public function debug($message, array $context = array())
+ {
+ $this->log(self::DEBUG, $message, $context);
+ }
+}
Oops, something went wrong.

0 comments on commit b559a2a

Please sign in to comment.