Added --no-log-file command line option

while this would be more user-friendly as a config option, configs are a pain because they aren't initialized until after the server log has already been set up.
In any case, I foresee that people will likely want to bake this into Dockerfiles directly anyway.
This commit is contained in:
Dylan K. Taylor 2024-03-15 17:53:50 +00:00
parent 7148c7a222
commit f527a4c8fe
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 26 additions and 14 deletions

View File

@ -47,4 +47,6 @@ final class BootstrapOptions{
public const DATA = "data"; public const DATA = "data";
/** Shows basic server version information and exits */ /** Shows basic server version information and exits */
public const VERSION = "version"; public const VERSION = "version";
/** Disables writing logs to server.log */
public const NO_LOG_FILE = "no-log-file";
} }

View File

@ -317,7 +317,7 @@ JIT_WARNING
//Logger has a dependency on timezone //Logger has a dependency on timezone
Timezone::init(); Timezone::init();
$opts = getopt("", [BootstrapOptions::NO_WIZARD, BootstrapOptions::ENABLE_ANSI, BootstrapOptions::DISABLE_ANSI]); $opts = getopt("", [BootstrapOptions::NO_WIZARD, BootstrapOptions::ENABLE_ANSI, BootstrapOptions::DISABLE_ANSI, BootstrapOptions::NO_LOG_FILE]);
if(isset($opts[BootstrapOptions::ENABLE_ANSI])){ if(isset($opts[BootstrapOptions::ENABLE_ANSI])){
Terminal::init(true); Terminal::init(true);
}elseif(isset($opts[BootstrapOptions::DISABLE_ANSI])){ }elseif(isset($opts[BootstrapOptions::DISABLE_ANSI])){
@ -325,8 +325,12 @@ JIT_WARNING
}else{ }else{
Terminal::init(); Terminal::init();
} }
$logFile = isset($opts[BootstrapOptions::NO_LOG_FILE]) ? null : Path::join($dataPath, "server.log");
$logger = new MainLogger(Path::join($dataPath, "server.log"), Path::join($dataPath, "log_archive"), Terminal::hasFormattingCodes(), "Server", new \DateTimeZone(Timezone::get())); $logger = new MainLogger($logFile, Path::join($dataPath, "log_archive"), Terminal::hasFormattingCodes(), "Server", new \DateTimeZone(Timezone::get()));
if($logFile === null){
$logger->notice("Logging to file disabled. Ensure logs are collected by other means (e.g. Docker logs).");
}
\GlobalLogger::set($logger); \GlobalLogger::set($logger);

View File

@ -39,12 +39,12 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
private bool $useFormattingCodes = false; private bool $useFormattingCodes = false;
private string $mainThreadName; private string $mainThreadName;
private string $timezone; private string $timezone;
private MainLoggerThread $logWriterThread; private ?MainLoggerThread $logWriterThread = null;
/** /**
* @throws \RuntimeException * @throws \RuntimeException
*/ */
public function __construct(string $logFile, string $logArchiveDir, bool $useFormattingCodes, string $mainThreadName, \DateTimeZone $timezone, bool $logDebug = false){ public function __construct(?string $logFile, string $logArchiveDir, bool $useFormattingCodes, string $mainThreadName, \DateTimeZone $timezone, bool $logDebug = false){
parent::__construct(); parent::__construct();
$this->logDebug = $logDebug; $this->logDebug = $logDebug;
@ -52,9 +52,11 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
$this->mainThreadName = $mainThreadName; $this->mainThreadName = $mainThreadName;
$this->timezone = $timezone->getName(); $this->timezone = $timezone->getName();
if($logFile !== null){
$this->logWriterThread = new MainLoggerThread($logFile, $logArchiveDir); $this->logWriterThread = new MainLoggerThread($logFile, $logArchiveDir);
$this->logWriterThread->start(NativeThread::INHERIT_NONE); $this->logWriterThread->start(NativeThread::INHERIT_NONE);
} }
}
/** /**
* Returns the current logger format used for console output. * Returns the current logger format used for console output.
@ -166,12 +168,14 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
} }
public function shutdownLogWriterThread() : void{ public function shutdownLogWriterThread() : void{
if($this->logWriterThread !== null){
if(NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){ if(NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){
$this->logWriterThread->shutdown(); $this->logWriterThread->shutdown();
}else{ }else{
throw new \LogicException("Only the creator thread can shutdown the logger thread"); throw new \LogicException("Only the creator thread can shutdown the logger thread");
} }
} }
}
protected function send(string $message, string $level, string $prefix, string $color) : void{ protected function send(string $message, string $level, string $prefix, string $color) : void{
$time = new \DateTime('now', new \DateTimeZone($this->timezone)); $time = new \DateTime('now', new \DateTimeZone($this->timezone));
@ -193,7 +197,9 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
$this->synchronized(function() use ($message, $level, $time) : void{ $this->synchronized(function() use ($message, $level, $time) : void{
Terminal::writeLine($message); Terminal::writeLine($message);
if($this->logWriterThread !== null){
$this->logWriterThread->write($time->format("Y-m-d") . " " . TextFormat::clean($message) . PHP_EOL); $this->logWriterThread->write($time->format("Y-m-d") . " " . TextFormat::clean($message) . PHP_EOL);
}
/** /**
* @var ThreadSafeLoggerAttachment $attachment * @var ThreadSafeLoggerAttachment $attachment
@ -205,11 +211,11 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
} }
public function syncFlushBuffer() : void{ public function syncFlushBuffer() : void{
$this->logWriterThread->syncFlushBuffer(); $this->logWriterThread?->syncFlushBuffer();
} }
public function __destruct(){ public function __destruct(){
if(!$this->logWriterThread->isJoined() && NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){ if($this->logWriterThread !== null && !$this->logWriterThread->isJoined() && NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){
$this->shutdownLogWriterThread(); $this->shutdownLogWriterThread();
} }
} }

View File

@ -45,7 +45,7 @@ class AsyncPoolTest extends TestCase{
public function setUp() : void{ public function setUp() : void{
@define('pocketmine\\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 3) . '/vendor/autoload.php'); @define('pocketmine\\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 3) . '/vendor/autoload.php');
$this->mainLogger = new MainLogger(tempnam(sys_get_temp_dir(), "pmlog"), sys_get_temp_dir(), false, "Main", new \DateTimeZone('UTC')); $this->mainLogger = new MainLogger(null, sys_get_temp_dir(), false, "Main", new \DateTimeZone('UTC'));
$this->pool = new AsyncPool(2, 1024, new ThreadSafeClassLoader(), $this->mainLogger, new SleeperHandler()); $this->pool = new AsyncPool(2, 1024, new ThreadSafeClassLoader(), $this->mainLogger, new SleeperHandler());
} }