Browse Source

Logger classes added

master
Anton Grebnev 13 years ago
parent
commit
a042c7b1f1
  1. 21
      logger/CliLogger.php
  2. 45
      logger/FileLogger.php
  3. 57
      logger/Logger.php

21
logger/CliLogger.php

@ -0,0 +1,21 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @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);
}
}

45
logger/FileLogger.php

@ -0,0 +1,45 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @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;
}
}
}
}

57
logger/Logger.php

@ -0,0 +1,57 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @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);
}
Loading…
Cancel
Save