mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 00:33:59 +00:00
Made logging better, plugin loggers can be attached by others
This commit is contained in:
parent
aa85993634
commit
43635b501a
@ -25,10 +25,29 @@ use Logger;
|
||||
use LogLevel;
|
||||
use pocketmine\utils\MainLogger;
|
||||
|
||||
class PluginLogger implements Logger{
|
||||
class PluginLogger implements \AttachableLogger{
|
||||
|
||||
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
|
||||
*/
|
||||
@ -71,5 +90,8 @@ class PluginLogger implements Logger{
|
||||
|
||||
public function log($level, $message){
|
||||
MainLogger::getLogger()->log($level, $this->pluginName . $message);
|
||||
foreach($this->attachments as $attachment){
|
||||
$attachment->log($level, $message);
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ namespace pocketmine\utils;
|
||||
|
||||
use LogLevel;
|
||||
|
||||
class MainLogger extends \ThreadedLogger{
|
||||
class MainLogger extends \AttachableThreadedLogger{
|
||||
protected $logFile;
|
||||
protected $logStream;
|
||||
protected $shutdown;
|
||||
@ -61,38 +61,38 @@ class MainLogger extends \ThreadedLogger{
|
||||
}
|
||||
|
||||
public function emergency($message){
|
||||
$this->send(TextFormat::RED . "[EMERGENCY] " . $message);
|
||||
$this->send(TextFormat::RED . "[EMERGENCY] " . $message, \LogLevel::EMERGENCY);
|
||||
}
|
||||
|
||||
public function alert($message){
|
||||
$this->send(TextFormat::RED . "[ALERT] " . $message);
|
||||
$this->send(TextFormat::RED . "[ALERT] " . $message, \LogLevel::ALERT);
|
||||
}
|
||||
|
||||
public function critical($message){
|
||||
$this->send(TextFormat::RED . "[CRITICAL] " . $message);
|
||||
$this->send(TextFormat::RED . "[CRITICAL] " . $message, \LogLevel::CRITICAL);
|
||||
}
|
||||
|
||||
public function error($message){
|
||||
$this->send(TextFormat::DARK_RED . "[ERROR] " . $message);
|
||||
$this->send(TextFormat::DARK_RED . "[ERROR] " . $message, \LogLevel::ERROR);
|
||||
}
|
||||
|
||||
public function warning($message){
|
||||
$this->send(TextFormat::YELLOW . "[WARNING] " . $message);
|
||||
$this->send(TextFormat::YELLOW . "[WARNING] " . $message, \LogLevel::WARNING);
|
||||
}
|
||||
|
||||
public function notice($message){
|
||||
$this->send(TextFormat::AQUA . "[NOTICE] " . $message);
|
||||
$this->send(TextFormat::AQUA . "[NOTICE] " . $message, \LogLevel::NOTICE);
|
||||
}
|
||||
|
||||
public function info($message){
|
||||
$this->send(TextFormat::WHITE . "[INFO] " . $message);
|
||||
$this->send(TextFormat::WHITE . "[INFO] " . $message, \LogLevel::INFO);
|
||||
}
|
||||
|
||||
public function debug($message){
|
||||
if($this->logDebug === false){
|
||||
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;
|
||||
}
|
||||
|
||||
protected function send($message){
|
||||
protected function send($message, $level = -1){
|
||||
$now = time();
|
||||
$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));
|
||||
@ -147,7 +147,7 @@ class MainLogger extends \ThreadedLogger{
|
||||
}
|
||||
|
||||
if($this->attachment instanceof \ThreadedLoggerAttachment){
|
||||
$this->attachment->call($message);
|
||||
$this->attachment->call($level, $message);
|
||||
}
|
||||
|
||||
$this->logStream .= date("Y-m-d", $now) . " " . $cleanMessage;
|
||||
|
@ -22,13 +22,9 @@
|
||||
interface LoggerAttachment{
|
||||
|
||||
/**
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
*/
|
||||
public function log($message);
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function call($message);
|
||||
public function log($level, $message);
|
||||
|
||||
}
|
@ -25,12 +25,13 @@ abstract class ThreadedLoggerAttachment extends \Threaded implements \LoggerAtta
|
||||
protected $attachment = null;
|
||||
|
||||
/**
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
*/
|
||||
public final function call($message){
|
||||
$this->log($message);
|
||||
public final function call($level, $message){
|
||||
$this->log($level, $message);
|
||||
if($this->attachment instanceof \ThreadedLoggerAttachment){
|
||||
$this->attachment->call($message);
|
||||
$this->attachment->call($level, $message);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user