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";
/** Shows basic server version information and exits */
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
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])){
Terminal::init(true);
}elseif(isset($opts[BootstrapOptions::DISABLE_ANSI])){
@ -325,8 +325,12 @@ JIT_WARNING
}else{
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);

View File

@ -39,12 +39,12 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
private bool $useFormattingCodes = false;
private string $mainThreadName;
private string $timezone;
private MainLoggerThread $logWriterThread;
private ?MainLoggerThread $logWriterThread = null;
/**
* @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();
$this->logDebug = $logDebug;
@ -52,8 +52,10 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
$this->mainThreadName = $mainThreadName;
$this->timezone = $timezone->getName();
$this->logWriterThread = new MainLoggerThread($logFile, $logArchiveDir);
$this->logWriterThread->start(NativeThread::INHERIT_NONE);
if($logFile !== null){
$this->logWriterThread = new MainLoggerThread($logFile, $logArchiveDir);
$this->logWriterThread->start(NativeThread::INHERIT_NONE);
}
}
/**
@ -166,10 +168,12 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
}
public function shutdownLogWriterThread() : void{
if(NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){
$this->logWriterThread->shutdown();
}else{
throw new \LogicException("Only the creator thread can shutdown the logger thread");
if($this->logWriterThread !== null){
if(NativeThread::getCurrentThreadId() === $this->logWriterThread->getCreatorId()){
$this->logWriterThread->shutdown();
}else{
throw new \LogicException("Only the creator thread can shutdown the logger thread");
}
}
}
@ -193,7 +197,9 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
$this->synchronized(function() use ($message, $level, $time) : void{
Terminal::writeLine($message);
$this->logWriterThread->write($time->format("Y-m-d") . " " . TextFormat::clean($message) . PHP_EOL);
if($this->logWriterThread !== null){
$this->logWriterThread->write($time->format("Y-m-d") . " " . TextFormat::clean($message) . PHP_EOL);
}
/**
* @var ThreadSafeLoggerAttachment $attachment
@ -205,11 +211,11 @@ class MainLogger extends AttachableThreadSafeLogger implements \BufferedLogger{
}
public function syncFlushBuffer() : void{
$this->logWriterThread->syncFlushBuffer();
$this->logWriterThread?->syncFlushBuffer();
}
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();
}
}

View File

@ -45,7 +45,7 @@ class AsyncPoolTest extends TestCase{
public function setUp() : void{
@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());
}