death-notifier/src/main/php/com/fwdekker/deathnotifier/LoggerUtil.php

43 lines
1.1 KiB
PHP

<?php
namespace com\fwdekker\deathnotifier;
use Monolog\ErrorHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
/**
* Helps with creating loggers.
*
* Contains global state, but reduces unnecessary parameter passing and unused fields.
*/
class LoggerUtil
{
/**
* @var Logger|null the main logger instance that other loggers are derived from, or `null` if the main logger has
* not been created yet
*/
private static ?Logger $main_logger = null;
/**
* Returns a logger with the given name.
*
* @param string $name the name of the logger to return
* @return Logger a logger with the given name
*/
public static function with_name(string $name = "main"): Logger
{
if (self::$main_logger === null) {
$config = Config::get()["logger"];
self::$main_logger = new Logger("main");
self::$main_logger->pushHandler(new StreamHandler($config["file"], $config["level"]));
ErrorHandler::register(self::$main_logger);
}
return self::$main_logger->withName($name);
}
}