2 commits - lib/Auth.php lib/kolab_api_controller.php lib/Log.php lib/User.php
Aleksander Machniak
machniak at kolabsys.com
Tue Aug 7 11:30:38 CEST 2012
lib/Auth.php | 3
lib/Log.php | 205 +++++++++++++++++++++++++++++++++++++++++++
lib/User.php | 3
lib/kolab_api_controller.php | 15 +--
4 files changed, 213 insertions(+), 13 deletions(-)
New commits:
commit 0e7b4b781ef57dce449afca8eb0995cae8f855c2
Merge: c9f97ff 9f852f6
Author: Aleksander Machniak <machniak at kolabsys.com>
Date: Tue Aug 7 11:30:32 2012 +0200
Merge branch 'master' of ssh://git.kolab.org/git/kolab-wap
commit c9f97ffed372bfaee1ff3fce3103a5506720efeb
Author: Aleksander Machniak <machniak at kolabsys.com>
Date: Tue Aug 7 11:29:56 2012 +0200
Add logging class
diff --git a/lib/Auth.php b/lib/Auth.php
index 549e046..6129bec 100644
--- a/lib/Auth.php
+++ b/lib/Auth.php
@@ -110,8 +110,7 @@ class Auth {
*/
public function authenticate($username, $password)
{
- // TODO: Log authentication request.
- //console("Authentication request for $username");
+ Log::info("Authentication request for $username");
if (strpos($username, '@')) {
// Case-sensitivity does not matter for strstr() on '@', which
diff --git a/lib/Log.php b/lib/Log.php
new file mode 100644
index 0000000..2ddb99b
--- /dev/null
+++ b/lib/Log.php
@@ -0,0 +1,205 @@
+<?php
+/*
+ +--------------------------------------------------------------------------+
+ | This file is part of the Kolab Web Admin Panel |
+ | |
+ | Copyright (C) 2011-2012, Kolab Systems AG |
+ | |
+ | This program is free software: you can redistribute it and/or modify |
+ | it under the terms of the GNU Affero General Public License as published |
+ | by the Free Software Foundation, either version 3 of the License, or |
+ | (at your option) any later version. |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public License |
+ | along with this program. If not, see <http://www.gnu.org/licenses/> |
+ +--------------------------------------------------------------------------+
+ | Author: Aleksander Machniak <machniak at kolabsys.com> |
+ +--------------------------------------------------------------------------+
+*/
+
+/**
+ * Class for logging debug/info/warning/error messages into log file(s)
+ */
+class Log
+{
+ const DEBUG = 8;
+ const INFO = 4;
+ const WARNING = 2;
+ const ERROR = 0;
+
+ private static $mode;
+
+
+ /**
+ * Logs debug message
+ *
+ * @param string $message Log message
+ * @param array $args Additional arguments ('file', 'line')
+ */
+ static function debug($message, $args = array())
+ {
+ if (self::mode() >= self::DEBUG) {
+ self::log_message(self::DEBUG, $message, $args);
+ }
+ }
+
+ /**
+ * Logs information message
+ *
+ * @param string $message Log message
+ * @param array $args Additional arguments ('file', 'line')
+ */
+ static function info($message, $args = array())
+ {
+ if (self::mode() >= self::INFO) {
+ self::log_message(self::INFO, $message, $args);
+ }
+ }
+
+ /**
+ * Logs warning message
+ *
+ * @param string $message Log message
+ * @param array $args Additional arguments ('file', 'line')
+ */
+ static function warning($message, $args = array())
+ {
+ if (self::mode() >= self::WARNING) {
+ self::log_message(self::WARNING, $message, $args);
+ }
+ }
+
+ /**
+ * Logs error message
+ *
+ * @param string $message Log message
+ * @param array $args Additional arguments ('file', 'line')
+ */
+ static function error($message, $args = array())
+ {
+ if (self::mode() >= self::ERROR) {
+ self::log_message(self::ERROR, $message, $args);
+ }
+ }
+
+ /**
+ * Message logger
+ *
+ * @param int $mode Message severity
+ * @param string $message Log message
+ * @param array $args Additional arguments ('file', 'line')
+ */
+ private static function log_message($mode, $message, $args = array())
+ {
+ $conf = Conf::get_instance();
+ $logfile = $conf->get('kolab_wap', 'log_file');
+
+ // if log_file is configured all logs will go to it
+ // otherwise use separate file for info/debug and warning/error
+ if (!$logfile) {
+ switch ($mode) {
+ case self::DEBUG:
+ case self::INFO:
+ $file = 'console';
+ break;
+ case self::WARNING:
+ case self::ERROR:
+ $file = 'errors';
+ break;
+ }
+
+ $logfile = dirname(__FILE__) . '/../logs/' . $file;
+ }
+
+ switch ($mode) {
+ case self::DEBUG:
+ $prefix = 'DEBUG';
+ break;
+ case self::INFO:
+ $prefix = 'INFO';
+ break;
+ case self::WARNING:
+ $prefix = 'WARNING';
+ break;
+ case self::ERROR:
+ $prefix = 'ERROR';
+ break;
+ }
+
+ if (!is_string($message)) {
+ $message = var_export($message, true);
+ }
+
+ $date = date('d-M-Y H:i:s O');
+ $sess_id = session_id();
+ $logline = sprintf("[%s]%s: [%s] %s\n",
+ $date, $sess_id ? "($sess_id)" : '', $prefix, $message);
+
+ if (!empty($args)) {
+ if ($args['file']) {
+ $logline .= ' in ' . $args['file'];
+ }
+ if ($args['line']) {
+ $logline .= ' on line ' . intval($args['line']);
+ }
+ }
+
+ if ($fp = @fopen($logfile, 'a')) {
+ fwrite($fp, $logline);
+ fflush($fp);
+ fclose($fp);
+ return;
+ }
+
+ if ($mode == self::ERROR) {
+ // send error to PHPs error handler if write to file didn't succeed
+ trigger_error($message, E_USER_ERROR);
+ }
+ }
+
+ /**
+ * Returns configured logging mode
+ *
+ * @return int Logging mode
+ */
+ static function mode()
+ {
+ if (isset(self::$mode)) {
+ return self::$mode;
+ }
+
+ $conf = Conf::get_instance();
+ $mode = $conf->get('kolab_wap', 'debug_mode');
+
+ switch ($mode) {
+ case self::DEBUG:
+ case 'debug':
+ case 'DEBUG':
+ self::$mode = self::DEBUG;
+ break;
+
+ case self::INFO:
+ case 'info':
+ case 'INFO':
+ self::$mode = self::INFO;
+ break;
+
+ case self::WARNING:
+ case 'warning':
+ case 'WARNING':
+ self::$mode = self::WARNING;
+ break;
+
+ case self::ERROR:
+ default:
+ self::$mode = self::ERROR;
+ }
+
+ return self::$mode;
+ }
+}
diff --git a/lib/User.php b/lib/User.php
index e0a2076..e7905cc 100644
--- a/lib/User.php
+++ b/lib/User.php
@@ -110,9 +110,6 @@ class User
public function groups()
{
- //console("Called " . __FUNCTION__ . " on line " . __LINE__ . " of " . __FILE__);
- //debug_print_backtrace();
-
if ($this->_groups || (is_array($this->_groups) && count($this->_groups) >= 1)) {
return $this->_groups;
}
diff --git a/lib/kolab_api_controller.php b/lib/kolab_api_controller.php
index bdbc707..e0dbf8b 100644
--- a/lib/kolab_api_controller.php
+++ b/lib/kolab_api_controller.php
@@ -79,7 +79,7 @@ class kolab_api_controller
public function add_service($service, $handler)
{
if ($this->services[$service]) {
- //console("Service $service is already registered.");
+ Log::warning("Service $service is already registered");
return false;
}
@@ -91,7 +91,7 @@ class kolab_api_controller
*/
public function get_service($service)
{
- //console("Obtaining service $service");
+ Log::debug("Obtaining service: $service");
// we are the system!
if ($service == 'system') {
@@ -108,8 +108,6 @@ class kolab_api_controller
}
}
- //console("Unknown service $service");
-
throw new Exception("Unknown service", 400);
}
@@ -140,7 +138,7 @@ class kolab_api_controller
$method = $this->request['method'];
$postdata = @json_decode($postdata, true);
- //console("Calling method " . $method . " on service " . $service);
+ Log::debug("Calling $service.$method");
// validate user session
if ($service != 'system' || $method != 'authenticate') {
@@ -188,7 +186,7 @@ class kolab_api_controller
$method = $this->request['method'];
$url = rtrim($url, '/') . '/' . $service . '.' . $method;
- //console("Proxying " . $url);
+ Log::debug("Proxying: $url");
$request = new HTTP_Request2();
$url = new Net_URL2($url);
@@ -272,7 +270,7 @@ class kolab_api_controller
*/
private function authenticate($request, $postdata)
{
- //console("Authenticating with postdata", $postdata);
+ Log::debug("Authenticating with postdata: " . json_encode($postdata));
$valid = false;
@@ -306,7 +304,8 @@ class kolab_api_controller
*/
private function capabilities()
{
- //console("system.capabilities called");
+ Log::debug("system.capabilities called");
+
$auth = Auth::get_instance();
$this->domains = $auth->list_domains();
More information about the commits
mailing list