Browse Source

Use syslog for logging

tags/1.0.0
Victor Laborie 1 year ago
parent
commit
862719006a

+ 1
- 4
config/config-sample.ini View File

@@ -11,6 +11,7 @@
[global]
name = "Evoadmin Mail XXX";
mail = "evoadmin-mail@example.com"
log_level = debug

[ldap]
host = "127.0.0.1"
@@ -19,7 +20,3 @@ base = "dc=example,dc=com"
admin_dn = "cn=admin,dc=example,dc=com"
admin_pass = "xxxxxxx"
superadmin[] = "evoadmin"

[log]
file = "../../log/evoadmin-mail.log"
level = debug

+ 8
- 8
htdocs/lib/class.ldapserver.php View File

@@ -79,15 +79,15 @@ class LdapServer {
$sr=ldap_search($this->conn, self::getBaseDN($this), "(&(uid=".$this->login.")(isAdmin=TRUE))");
$info = ldap_get_entries($this->conn, $sr);
if (!$info['count']) {
Logger::error('invalid login '.$this->login);
Logger::error('Invalid login for '.$this->login);
throw new Exception("Échec de l'authentification, utilisateur ou mot de passe incorrect.");
}

if (!@ldap_bind($this->conn, $info[0]['dn'], $password)) {
Logger::error('invalid password for user '.$this->login);
Logger::error('Invalid password for '.$this->login);
throw new Exception("Échec de l'authentification, utilisateur ou mot de passe incorrect.");
}
Logger::info($this->login.' successfully logged in');
Logger::info('Accepted login for '.$this->login.'');
}

public function getDomains() {
@@ -113,10 +113,10 @@ class LdapServer {

if (!@ldap_add($this->conn, LdapDomain::getBaseDN($this, $name), $info)) {
$error = ldap_error($this->conn);
Logger::error('error when adding domain '.$name, $this->login);
Logger::error('Error when adding domain '.$name, $this->login);
throw new Exception("Erreur dans l'ajout du domaine : $error");
}
Logger::info('domain '.$name.' added', $this->login);
Logger::info('Domain '.$name.' added', $this->login);
MailNotify::addDomain($name);
}

@@ -134,12 +134,12 @@ class LdapServer {
$dn = LdapDomain::getBaseDN($this, $name);
if (!ldap_delete($this->conn, $dn)) {
$error = ldap_error($this->conn);
Logger::error('error when delete domain '.$name, $this->login);
Logger::error('Error when delete domain '.$name, $this->login);
throw new Exception("Erreur dans la suppression du domaine $dn : $error");
}
Logger::info('domain '.$name.' deleted ', $this->login);
Logger::info('Domain '.$name.' deleted ', $this->login);
} else {
Logger::error('trying to delete an unknow domain '.$name, $this->login);
Logger::error('Trying to delete an unknow domain '.$name, $this->login);
throw new Exception("Ce domaine n'existe pas !");
}
}

+ 12
- 19
htdocs/lib/class.logger.php View File

@@ -8,11 +8,10 @@ class Logger {
const ERROR = 3;
const CRITICAL = 4;

private static $file,$level;
private static $level;

public static function configure(array $config) {
self::$file = $config['file'];
switch ($config['level']) {
public static function configure($loglevel) {
switch ($loglevel) {
case 'critical':
self::$level = self::CRITICAL;
case 'error':
@@ -29,40 +28,34 @@ class Logger {
}

private static function write_log($txt, $level, $user, $var) {
$date = date("Y-m-d H:i:s");
if (empty($user)) {
$log = '['.$date.'] '.self::LEVEL[$level].': '.$txt.' ['.$_SERVER['REMOTE_ADDR'].']'.PHP_EOL;
} else {
$log = '['.$date.'] '.self::LEVEL[$level].': '.$txt.' [by '.$user.']'.PHP_EOL;
}
file_put_contents(self::$file, $log, FILE_APPEND | LOCK_EX);
if (self::$level <= self::DEBUG && !empty($var)) {
file_put_contents(self::$file, var_dump($var).PHP_EOL , FILE_APPEND | LOCK_EX);
}
$facility = empty($user) ? LOG_AUTH : LOG_LOCAL0;
$id = empty($user) ? $_SERVER['REMOTE_ADDR'] : $user;
openlog("evoadmin-mail", LOG_PID | LOG_PERROR, $facility);
syslog($level, "$txt [$id]");
closelog();
}

public static function critical($txt, $user=NULL, $var=NULL) {
if (self::$level <= self::CRITICAL ) {
self::write_log($txt, self::CRITICAL, $user, $var);
self::write_log($txt, LOG_CRIT, $user, $var);
}
}

public static function error($txt, $user=NULL, $var=NULL) {
if (self::$level <= self::ERROR ) {
self::write_log($txt, self::ERROR, $user, $var);
self::write_log($txt, LOG_ERR, $user, $var);
}
}

public static function warning($txt, $user=NULL, $var=NULL) {
if (self::$level <= self::WARNING) {
self::write_log($txt, self::WARNING, $user, $var);
self::write_log($txt, LOG_WARNING, $user, $var);
}
}

public static function info($txt, $user=NULL, $var=NULL) {
if (self::$level <= self::INFO) {
self::write_log($txt, self::INFO, $user, $var);
self::write_log($txt, LOG_INFO, $user, $var);
}
}
}

+ 1
- 1
htdocs/lib/config.php View File

@@ -13,5 +13,5 @@ require_once 'Twig/autoload.php';

$config = parse_ini_file('../config/config.ini', true);

Logger::configure($config['log']);
Logger::configure($config['global']['log_level']);
MailNotify::configure($config['global']);

+ 1
- 5
scripts/config-migrate.php View File

@@ -19,6 +19,7 @@ require_once("/home/evoadmin-mail/www/htdocs/config/connect.php");
[global]
name = "<?php echo $conf['html']['title']; ?>";
mail = "<?php echo $conf['admin']['mail']; ?>"
log_level = error

[ldap]
host = "127.0.0.1"
@@ -31,8 +32,3 @@ foreach ($conf['admin']['logins'] as $admin) {
echo "superadmin[] = \"$admin\"";
}
?>


[log]
file = "../../log/evoadmin-mail.log"
level = error

Loading…
Cancel
Save