logger auf syslog umgestellt - läuft nicht korrekt

This commit is contained in:
bernd 2021-03-01 22:18:12 +01:00
parent 53133a18bc
commit 8ae028dfaf
2 changed files with 81 additions and 22 deletions

View file

@ -33,9 +33,10 @@ database=requests
user=besitzer der datenbank requests
password=total geheimes passwort
; Loglevel festlegen. Derzeit sind error, warn, info und debug gültige
; Werte. Die Angabe ist nicht Case-Sesitive.
loglevel=debug
; Loglevel festlegen. Die Funktion syslog() akzeptiert folgende Werte:
; LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE,
; LOG_INFO oder LOG_DEBUG.
loglevel=LOG_INFO
; Logdatei festlegen. Der Benutzer, unter dem der Dienst läuft braucht
; Schreibrechte für das Verzeichnis.

View file

@ -19,52 +19,110 @@ class Logger {
*/
private $levelnumbers = array(
"ERROR" => 10,
"WARN" => 20,
"INFO" => 30,
"DEBUG" => 40,
LOG_ALERT => 10,
LOG_CRIT => 20,
LOG_ERR => 30,
LOG_WARNING => 40,
LOG_NOTICE => 50,
LOG_INFO => 60,
LOG_DEBUG => 70,
);
public $loglevel = "INFO";
private $logfile = "log/register.log";
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("ERROR", $msg);
$this->logMsg(LOG_ERR, $msg);
}
public function w(string $msg) {
$this->logMsg("WARN", $msg);
$this->logMsg(LOG_WARN, $msg);
}
public function n(string $msg) {
$this->logMsg(LOG_NOTICE, $msg);
}
public function i(string $msg) {
$this->logMsg("INFO", $msg);
$this->logMsg(LOG_INFO, $msg);
}
public function d(string $msg) {
$this->logMsg("DEBUG", $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->writeLog("INFO", "Loglevel set to {$level}");
$this->d("Loglevel set to {$level}");
} else {
$this->writeLog("WARN", "{$level} is not a valid loglevel.");
echo "{$level} is not a valid loglevel.";
}
}
private function logMsg(string $label, string $msg) {
$msglevel = $this->levelnumbers[$label];
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) {
$this->writeLog($label, $msg);
$msg = $this->addLevel($level, $msg);
$this->sendToLog($level, $msg);
}
}
private function writeLog(string $label, string $msg) {
$app = $this->app;
$file = $this->logfile;
error_log(date("[Y-m-d H:i:s]")." [".$label."] [".$app."] ".$msg."\n", 3, $file);
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);
}
}