Anton Grebnev
13 years ago
3 changed files with 123 additions and 0 deletions
@ -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); |
|||
} |
|||
} |
|||
|
@ -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; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
@ -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); |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue