Logger classes added
This commit is contained in:
21
logger/CliLogger.php
Normal file
21
logger/CliLogger.php
Normal file
@ -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
Normal file
45
logger/FileLogger.php
Normal file
@ -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
Normal file
57
logger/Logger.php
Normal file
@ -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);
|
||||
|
||||
}
|
Reference in New Issue
Block a user