Add logging facilities for Storage classes as requested by T469. Not finished yet...

This commit is contained in:
Thomas Bruederli 2015-06-14 17:17:08 +02:00
parent 9cd117d7a2
commit 0a24a09a73
4 changed files with 46 additions and 28 deletions

View file

@ -341,6 +341,7 @@ class kolab_2fa extends rcube_plugin
);
$this->storage->set_username($for);
$this->storage->set_logger(new \Kolab2FA\Log\RcubeLogger());
// set user properties from active session
if (!empty($_SESSION['kolab_dn'])) {

View file

@ -23,10 +23,14 @@
namespace Kolab2FA\Storage;
use \Kolab2FA\Log;
abstract class Base
{
public $username = null;
protected $config = array();
protected $logger;
/**
*
@ -63,6 +67,24 @@ abstract class Base
public function init(array $config)
{
$this->config = array_merge($this->config, $config);
// use syslog logger by default
$this->set_logger(new Log\Syslog());
}
/**
*
*/
public function set_logger(Log\Logger $logger)
{
$this->logger = $logger;
if ($this->config['debug']) {
$this->logger->set_level(LOG_DEBUG);
}
else if ($this->config['loglevel']) {
$this->logger->set_level($this->config['loglevel']);
}
}
/**
@ -73,6 +95,16 @@ abstract class Base
$this->username = $username;
}
/**
* Send messager to the logging system
*/
protected function log($level, $message)
{
if ($this->logger) {
$this->logger->log($level, $message);
}
}
/**
* List keys holding settings for 2-factor-authentication
*/

View file

@ -24,6 +24,7 @@
namespace Kolab2FA\Storage;
use \Net_LDAP3;
use \Kolab2FA\Log\Logger;
class LDAP extends Base
{
@ -345,30 +346,4 @@ class LDAP extends Base
return strtr($str, $replaces);
}
/**
* Prints debug/error info to the log
*/
public function log($level, $msg)
{
$msg = implode("\n", $msg);
switch ($level) {
case LOG_DEBUG:
case LOG_INFO:
case LOG_NOTICE:
if ($this->config['debug'] && class_exists('\\rcube', false)) {
\rcube::write_log('ldap', $msg);
}
break;
case LOG_EMERGE:
case LOG_ALERT:
case LOG_CRIT:
case LOG_ERR:
case LOG_WARNING:
$this->error = $msg;
// throw new Exception("LDAP storage error: " . $msg);
break;
}
}
}

View file

@ -65,7 +65,7 @@ class RcubeUser extends Base
{
if (!isset($this->cache[$key])) {
$factors = $this->get_factors();
console('READ', $key, $factors);
$this->log(LOG_DEBUG, 'RcubeUser::read() ' . $key);
$this->cache[$key] = $factors[$key];
}
@ -77,6 +77,8 @@ class RcubeUser extends Base
*/
public function write($key, $value)
{
$this->log(LOG_DEBUG, 'RcubeUser::write() ' . @json_encode($value));
if ($user = $this->get_user($this->username)) {
$this->cache[$key] = $value;
@ -109,7 +111,11 @@ class RcubeUser extends Base
);
}
return $user->save_prefs($save_data, true);
$success = $user->save_prefs($save_data, true);
if (!$success) {
$this->log(LOG_WARNING, sprintf('Failed to save prefs for user %s', $this->username));
}
}
return false;
@ -150,6 +156,10 @@ class RcubeUser extends Base
$this->user = rcube_user::query($username, $this->config['hostname']);
}
if (!$this->user) {
$this->log(LOG_WARNING, sprintf('No user record found for %s @ %s', $username, $this->config['hostname']));
}
return $this->user;
}