From a042c7b1f1a7f3b28c1e03840a47ed8d080e6803 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Mon, 21 Nov 2011 19:14:49 +0400 Subject: [PATCH] Logger classes added --- logger/CliLogger.php | 21 +++++++++++++++++++ logger/FileLogger.php | 45 ++++++++++++++++++++++++++++++++++++++++ logger/Logger.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 logger/CliLogger.php create mode 100644 logger/FileLogger.php create mode 100644 logger/Logger.php diff --git a/logger/CliLogger.php b/logger/CliLogger.php new file mode 100644 index 0000000..121cda1 --- /dev/null +++ b/logger/CliLogger.php @@ -0,0 +1,21 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Logger + * @since 21-11-2011 + * @user: agrebnev + */ + +class CliLogger extends Logger +{ + protected function concreteLog($message) + { + // Заменяем окончания строк на их символы + $message = str_replace(array("\r", "\n"), array('\r', '\n'), $message); + $out = microtime(true) . " \t: " . $this->pid . trim($message) . PHP_EOL; + print($out); + } +} + diff --git a/logger/FileLogger.php b/logger/FileLogger.php new file mode 100644 index 0000000..c14c52f --- /dev/null +++ b/logger/FileLogger.php @@ -0,0 +1,45 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Logger + * @since 21-11-2011 + * @user: agrebnev + */ + +class FileLogger extends Logger +{ + + protected $file_path = ''; + + protected $handler = null; + + protected function __construct() + { + $this->file_path = Config::get('Logger')->filepath; + } + + protected function concreteLog($message) + { + $out = microtime(true) . " \t: " . $this->pid . trim($message) . "\r\n"; + + if (!$this->handler) { + $this->handler = @fopen($this->file_path, "a"); + if (!$this->handler) { + throw new GeneralException('Could not open file ' . $this->file_path); + } + } + fwrite($this->handler, $out); + } + + public function __destruct() + { + if ($this->handler) { + if (fclose($this->handler)) { + $this->handler = null; + } + } + } +} + diff --git a/logger/Logger.php b/logger/Logger.php new file mode 100644 index 0000000..48e64f9 --- /dev/null +++ b/logger/Logger.php @@ -0,0 +1,57 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage Logger + * @since 21-11-2011 + * @user: agrebnev + */ + +abstract class Logger +{ + + protected static $_instance = null; + + /** + * pid текущего процесса + * @var string + */ + protected $pid = ''; + + protected function __construct() {} + + public static function getInstance() + { + if(static::$_instance === null) { + //$class = get_called_class(); + $class = Config::get('Logger')->logger; + static::$_instance = new $class(); + } + return static::$_instance; + } + /** + * Вывод лога + * @param string $message Сообщение + */ + public function log($message) + { + if (DEBUG) { + $this->concreteLog($message); + } + } + + /** + * Установить pid текущего процесса для вывода в лог + * @param int $pid + */ + public function setPid($pid) + { + if (!empty($pid)) { + $this->pid = ' <' . $pid . '> '; + } + } + + abstract protected function concreteLog($message); + +} \ No newline at end of file