Your PocketMine logger seems too old? Not any longer with CustomLogger !
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.6 KiB

  1. <?php
  2. namespace Ad5001\CustomLogger;
  3. use pocketmine\utils\MainLogger;
  4. use pocketmine\utils\TextFormat;
  5. use pocketmine\utils\Terminal;
  6. use LogLevel;
  7. use pocketmine\Thread;
  8. use pocketmine\Worker;
  9. use pocketmine\Server;
  10. class CustomLogger extends MainLogger {
  11. /*
  12. Constructs the class
  13. */
  14. public function __construct($logFile, $logDebug = false, array $cfg = []) {
  15. MainLogger::$logger = $this;
  16. $class = new \ReflectionClass('pocketmine\\Server');
  17. $property = $class->getProperty('logger');
  18. $property->setAccessible(true);
  19. $property->setValue(Server::getInstance(), $this);
  20. $property->setAccessible(false);
  21. touch($logFile);
  22. $this->logFile = $logFile;
  23. $this->logDebug = (bool) $logDebug;
  24. $this->logStream = new \Threaded;
  25. $this->cfg = $cfg;
  26. $this->start();
  27. }
  28. // Modified version of the MainLogger
  29. protected function send($message, $level, $prefix, $color){
  30. $now = time();
  31. $thread = \Thread::getCurrentThread();
  32. if($thread === null){
  33. $threadName = "PocketMine thread";
  34. }elseif($thread instanceof Thread or $thread instanceof Worker){
  35. $threadName = $thread->getThreadName() . " thread";
  36. }else{
  37. $threadName = (new \ReflectionClass($thread))->getShortName() . " thread";
  38. }
  39. if($this->shouldRecordMsg){
  40. if((time() - $this->lastGet) >= 10) $this->shouldRecordMsg = false; // 10 secs timeout
  41. else{
  42. if(strlen($this->shouldSendMsg) >= 10000) $this->shouldSendMsg = "";
  43. $this->shouldSendMsg .= $color . "|" . $prefix . "|" . trim($message, "\r\n") . "\n";
  44. }
  45. }
  46. $message = TextFormat::toANSI($this->translateMsg($this->cfg["LoggerLook"], $message, $prefix, $color, $threadName));
  47. $cleanMessage = TextFormat::clean($message);
  48. if(!Terminal::hasFormattingCodes()){
  49. echo str_repeat("\010", strlen(TextFormat::clean(TextFormat::toANSI(MainLogger::$logger->translateMsg($this->cfg["LoggerPrefix"], "", "", "§f", $threadName)))));
  50. echo $cleanMessage . PHP_EOL;
  51. echo TextFormat::toANSI($this->translateMsg($this->cfg["LoggerPrefix"], "", "", $color, $threadName));
  52. }else{
  53. echo str_repeat("\010", strlen(TextFormat::clean(TextFormat::toANSI(MainLogger::$logger->translateMsg($this->cfg["LoggerPrefix"], "", "", "§f", $threadName)))));
  54. echo $message . PHP_EOL;
  55. echo TextFormat::toANSI($this->translateMsg($this->cfg["LoggerPrefix"], "", "", $color, $threadName));
  56. }
  57. if(isset($this->consoleCallback)){
  58. call_user_func($this->consoleCallback);
  59. }
  60. if($this->attachment instanceof \ThreadedLoggerAttachment){
  61. $this->attachment->call($level, $message);
  62. }
  63. $this->logStream[] = date("Y-m-d", $now) . " " . $cleanMessage . "\n";
  64. if($this->logStream->count() === 1){
  65. $this->synchronized(function(){
  66. $this->notify();
  67. });
  68. }
  69. }
  70. /*
  71. Translate the message from config.
  72. @param $message string
  73. */
  74. public function translateMsg(string $msg, string $message, string $prefix, string $color, string $threadName) {
  75. $msg = str_ireplace("{time}", date("H:i:s"), $msg);
  76. $msg = str_ireplace("{prefixLower}", strtolower($prefix), $msg);
  77. $msg = str_ireplace("{prefixUpper}", strtoupper($prefix), $msg);
  78. $msg = str_ireplace("{prefix}", $prefix, $msg);
  79. $msg = str_ireplace("{message}", $message, $msg);
  80. $msg = str_ireplace("{msg}", $message, $msg);
  81. $msg = str_ireplace("{color}", $color, $msg);
  82. $msg = str_ireplace("{thread}", $threadName, $msg);
  83. $msg = preg_replace("/{color-(.+?)}/", "§$1", $msg);
  84. return $msg;
  85. }
  86. }