10, LOG_CRIT => 20, LOG_ERR => 30, LOG_WARNING => 40, LOG_NOTICE => 50, LOG_INFO => 60, LOG_DEBUG => 70, ); public $loglevel = LOG_INFO; private $app = "matrix-register"; private $logopen = false; public function __construct() { if (openlog($this->app, LOG_NDELAY | LOG_PID, LOG_SYSLOG) === true) { $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); } public function e(string $msg) { $this->logMsg(LOG_ERR, $msg); } public function w(string $msg) { $this->logMsg(LOG_WARN, $msg); } public function n(string $msg) { $this->logMsg(LOG_NOTICE, $msg); } public function i(string $msg) { $this->logMsg(LOG_INFO, $msg); } public function d(string $msg) { $this->logMsg(LOG_DEBUG, $msg); } private function addLevel(string $prio, string $msg): string { /** * Ich möchte die Priorität der Meldung mit im Log haben. Die * Funktion setzt ein Flag vor die Meldung. */ $text = "[$prio] "."$msg"; return $text; } public function setLogLevel(string $level) { /** * Funktion zum Setzen des Loglevels. Mögliche Werte sind LOG_EMERG, * LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO * oder LOG_DEBUG. Es wird geprüft, ob das übergebene Level gültig * ist. */ if (array_key_exists($level, $this->levelnumbers) === true) { $this->loglevel = $level; $this->d("Loglevel set to {$level}"); } else { echo "{$level} is not a valid loglevel."; } } private function logMsg(string $level, string $msg) { /** * Entscheidet, ob eine Meldung ausgegeben wird oder nicht. Dazu * wird geschaut, ob das Loglevel höher ist als das Messagelevel. */ $msglevel = $this->levelnumbers[$level]; $loglevel = $this->levelnumbers[$this->loglevel]; if ($loglevel >= $msglevel) { $msg = $this->addLevel($level, $msg); $this->sendToLog($level, $msg); } } private function sendToLog(string $level, string $msg) { /** * Schickt die Logmeldungen an das Syslog. Der erste Parameter ist * das Loglevel. Der zweite * Parameter ist die Logmeldung. */ syslog($level, $msg); } } ?>