Made logging better, plugin loggers can be attached by others

This commit is contained in:
Shoghi Cervantes 2014-07-02 23:33:29 +02:00
parent aa85993634
commit 43635b501a
4 changed files with 40 additions and 21 deletions

View File

@ -25,10 +25,29 @@ use Logger;
use LogLevel; use LogLevel;
use pocketmine\utils\MainLogger; use pocketmine\utils\MainLogger;
class PluginLogger implements Logger{ class PluginLogger implements \AttachableLogger{
private $pluginName; private $pluginName;
/** @var \LoggerAttachment[] */
private $attachments = [];
public function addAttachment(\LoggerAttachment $attachment){
$this->attachments[spl_object_hash($attachment)] = $attachment;
}
public function removeAttachment(\LoggerAttachment $attachment){
unset($this->attachments[spl_object_hash($attachment)]);
}
public function removeAttachments(){
$this->attachments = [];
}
public function getAttachments(){
return $this->attachments;
}
/** /**
* @param Plugin $context * @param Plugin $context
*/ */
@ -71,5 +90,8 @@ class PluginLogger implements Logger{
public function log($level, $message){ public function log($level, $message){
MainLogger::getLogger()->log($level, $this->pluginName . $message); MainLogger::getLogger()->log($level, $this->pluginName . $message);
foreach($this->attachments as $attachment){
$attachment->log($level, $message);
}
} }
} }

View File

@ -23,7 +23,7 @@ namespace pocketmine\utils;
use LogLevel; use LogLevel;
class MainLogger extends \ThreadedLogger{ class MainLogger extends \AttachableThreadedLogger{
protected $logFile; protected $logFile;
protected $logStream; protected $logStream;
protected $shutdown; protected $shutdown;
@ -61,38 +61,38 @@ class MainLogger extends \ThreadedLogger{
} }
public function emergency($message){ public function emergency($message){
$this->send(TextFormat::RED . "[EMERGENCY] " . $message); $this->send(TextFormat::RED . "[EMERGENCY] " . $message, \LogLevel::EMERGENCY);
} }
public function alert($message){ public function alert($message){
$this->send(TextFormat::RED . "[ALERT] " . $message); $this->send(TextFormat::RED . "[ALERT] " . $message, \LogLevel::ALERT);
} }
public function critical($message){ public function critical($message){
$this->send(TextFormat::RED . "[CRITICAL] " . $message); $this->send(TextFormat::RED . "[CRITICAL] " . $message, \LogLevel::CRITICAL);
} }
public function error($message){ public function error($message){
$this->send(TextFormat::DARK_RED . "[ERROR] " . $message); $this->send(TextFormat::DARK_RED . "[ERROR] " . $message, \LogLevel::ERROR);
} }
public function warning($message){ public function warning($message){
$this->send(TextFormat::YELLOW . "[WARNING] " . $message); $this->send(TextFormat::YELLOW . "[WARNING] " . $message, \LogLevel::WARNING);
} }
public function notice($message){ public function notice($message){
$this->send(TextFormat::AQUA . "[NOTICE] " . $message); $this->send(TextFormat::AQUA . "[NOTICE] " . $message, \LogLevel::NOTICE);
} }
public function info($message){ public function info($message){
$this->send(TextFormat::WHITE . "[INFO] " . $message); $this->send(TextFormat::WHITE . "[INFO] " . $message, \LogLevel::INFO);
} }
public function debug($message){ public function debug($message){
if($this->logDebug === false){ if($this->logDebug === false){
return; return;
} }
$this->send(TextFormat::GRAY . "[DEBUG] " . $message); $this->send(TextFormat::GRAY . "[DEBUG] " . $message, \LogLevel::DEBUG);
} }
/** /**
@ -135,7 +135,7 @@ class MainLogger extends \ThreadedLogger{
$this->shutdown = true; $this->shutdown = true;
} }
protected function send($message){ protected function send($message, $level = -1){
$now = time(); $now = time();
$message = TextFormat::toANSI(TextFormat::AQUA . date("H:i:s", $now) . TextFormat::RESET . " " . $message . TextFormat::RESET . PHP_EOL); $message = TextFormat::toANSI(TextFormat::AQUA . date("H:i:s", $now) . TextFormat::RESET . " " . $message . TextFormat::RESET . PHP_EOL);
$cleanMessage = TextFormat::clean(preg_replace('/\x1b\[[0-9;]*m/', "", $message)); $cleanMessage = TextFormat::clean(preg_replace('/\x1b\[[0-9;]*m/', "", $message));
@ -147,7 +147,7 @@ class MainLogger extends \ThreadedLogger{
} }
if($this->attachment instanceof \ThreadedLoggerAttachment){ if($this->attachment instanceof \ThreadedLoggerAttachment){
$this->attachment->call($message); $this->attachment->call($level, $message);
} }
$this->logStream .= date("Y-m-d", $now) . " " . $cleanMessage; $this->logStream .= date("Y-m-d", $now) . " " . $cleanMessage;

View File

@ -22,13 +22,9 @@
interface LoggerAttachment{ interface LoggerAttachment{
/** /**
* @param mixed $level
* @param string $message * @param string $message
*/ */
public function log($message); public function log($level, $message);
/**
* @param string $message
*/
public function call($message);
} }

View File

@ -25,12 +25,13 @@ abstract class ThreadedLoggerAttachment extends \Threaded implements \LoggerAtta
protected $attachment = null; protected $attachment = null;
/** /**
* @param mixed $level
* @param string $message * @param string $message
*/ */
public final function call($message){ public final function call($level, $message){
$this->log($message); $this->log($level, $message);
if($this->attachment instanceof \ThreadedLoggerAttachment){ if($this->attachment instanceof \ThreadedLoggerAttachment){
$this->attachment->call($message); $this->attachment->call($level, $message);
} }
} }