2021-03-01 16:56:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* file: logger.php
|
|
|
|
* date: 01.03.2021
|
|
|
|
* author: bernd@nr18.space
|
|
|
|
* desc: Simpler Logger
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
class Logger {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Einfache Klasse zum Loggen von Nachrichten. Schreibt seine
|
|
|
|
* Nachrichten in eine Datei. Eleganter wäre ein Weiterreichen an den
|
|
|
|
* Logging-Daemon des Betriebssystems.
|
|
|
|
* TODO: Er ist wenig Fehlertolerant. Es wird nicht geprüft, ob der
|
|
|
|
* angefragte Key im Array existiert.
|
|
|
|
*/
|
|
|
|
|
2021-03-01 22:18:12 +01:00
|
|
|
public $loglevel = LOG_INFO;
|
2021-03-01 16:56:53 +01:00
|
|
|
private $app = "matrix-register";
|
2021-03-01 22:18:12 +01:00
|
|
|
private $logopen = false;
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
2021-03-01 23:22:05 +01:00
|
|
|
if (openlog($this->app, LOG_PID | LOG_PERROR , LOG_SYSLOG) === true) {
|
2021-03-01 22:18:12 +01:00
|
|
|
$this->logopen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
|
|
|
$this->logopen = false;
|
|
|
|
closelog();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function a(string $msg) {
|
|
|
|
$this->logMsg(LOG_ALERT, $msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function c(string $msg) {
|
|
|
|
$this->logMsg(LOG_CRIT, $msg);
|
|
|
|
}
|
2021-03-01 16:56:53 +01:00
|
|
|
|
|
|
|
public function e(string $msg) {
|
2021-03-01 22:18:12 +01:00
|
|
|
$this->logMsg(LOG_ERR, $msg);
|
2021-03-01 16:56:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function w(string $msg) {
|
2021-03-01 22:18:12 +01:00
|
|
|
$this->logMsg(LOG_WARN, $msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function n(string $msg) {
|
|
|
|
$this->logMsg(LOG_NOTICE, $msg);
|
2021-03-01 16:56:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function i(string $msg) {
|
2021-03-01 22:18:12 +01:00
|
|
|
$this->logMsg(LOG_INFO, $msg);
|
2021-03-01 16:56:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function d(string $msg) {
|
2021-03-01 22:18:12 +01:00
|
|
|
$this->logMsg(LOG_DEBUG, $msg);
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:22:05 +01:00
|
|
|
public function setLogLevel(int $level) {
|
2021-03-01 22:18:12 +01:00
|
|
|
|
|
|
|
/**
|
2021-03-01 23:22:05 +01:00
|
|
|
* Funktion zum Setzen des Loglevels. Mögliche Werte sind
|
|
|
|
* LOG_EMERG(0), LOG_ALERT(1), LOG_CRIT(2), LOG_ERR(3),
|
|
|
|
* LOG_WARNING(4), LOG_NOTICE(5), LOG_INFO(6) oder LOG_DEBUG(7).
|
|
|
|
* Es wird geprüft, ob das übergebene Level gültig ist.
|
2021-03-01 22:18:12 +01:00
|
|
|
*/
|
|
|
|
|
2021-03-01 23:22:05 +01:00
|
|
|
if (in_array($level, array(1, 2, 3, 4, 5, 6, 7, 8)) === true) {
|
2021-03-01 16:56:53 +01:00
|
|
|
$this->loglevel = $level;
|
2021-03-01 23:22:05 +01:00
|
|
|
$this->d("Loglevel set to {$this->loglevel}");
|
2021-03-01 16:56:53 +01:00
|
|
|
} else {
|
2021-03-01 22:18:12 +01:00
|
|
|
echo "{$level} is not a valid loglevel.";
|
2021-03-01 16:56:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:22:05 +01:00
|
|
|
private function addLevel(int $level, string $msg): string {
|
2021-03-01 22:18:12 +01:00
|
|
|
|
|
|
|
/**
|
2021-03-01 23:22:05 +01:00
|
|
|
* Stellt der Meldung das Loglevel voran.
|
2021-03-01 22:18:12 +01:00
|
|
|
*/
|
|
|
|
|
2021-03-01 23:22:05 +01:00
|
|
|
$text = "[$level] ".$msg;
|
|
|
|
return $text;
|
2021-03-01 16:56:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 23:22:05 +01:00
|
|
|
private function logMsg(string $msglevel, string $msg) {
|
2021-03-01 22:18:12 +01:00
|
|
|
|
|
|
|
/**
|
2021-03-01 23:22:05 +01:00
|
|
|
* Übergibt die Meldung an die Funktion syslog. Vorher wird
|
|
|
|
* entschieden, ob eine Meldung ausgegeben wird oder nicht. Dazu
|
|
|
|
* wird geschaut, ob das Loglevel höher ist als das Messagelevel.
|
2021-03-01 22:18:12 +01:00
|
|
|
*/
|
|
|
|
|
2021-03-01 23:22:05 +01:00
|
|
|
$msg = $this->addLevel($msglevel, $msg);
|
|
|
|
if ($this->loglevel >= $msglevel) {
|
|
|
|
syslog($msglevel, $msg);
|
|
|
|
} else {
|
|
|
|
syslog($msglevel, $msg);
|
|
|
|
}
|
2021-03-01 16:56:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|