fix: Created log file if files does not exist

This commit is contained in:
Aerex
2020-06-11 11:10:45 -05:00
parent c302c4653a
commit 728fce1b78
2 changed files with 13 additions and 4 deletions

View File

@@ -9,16 +9,25 @@ class Logger {
private $configs = ['enabled' => false];
function __construct($configs, $tag) {
if (isset($configs['logger'])) {
$this->configs = $configs['logger'];
if (isset($configs['general']) && isset($configs['general']['logger'])) {
$this->configs = $configs['general']['logger'];
}
if ($this->configs['enabled']) {
$this->createLoggerFile();
$this->logger = new Monolog($tag);
$logLevel = Monolog::getLevels()[$this->configs['level']];
$this->logger->pushHandler(new StreamHandler($this->configs['file'], $logLevel));
}
}
private function createLoggerFile() {
if (!file_exists($this->configs['file'])) {
if (!fopen($this->configs['file'], 'w')) {
throw new \Exception(sprintf('Could not create logger file %s', $this->configs['file']));
}
}
}
public function debug($message) {
if ($this->configs['enabled']) {
$this->logger->debug($message);