summaryrefslogtreecommitdiffstats
path: root/lib/Log.php
blob: 2705912b18d6f47a81e0c0187b6f6ddf4c374c9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?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@kolabsys.com>                      |
 +--------------------------------------------------------------------------+
*/

/**
 * Class for logging debug/info/warning/error messages into log file(s)
 */
class Log
{
    const TRACE   = 16; // use for protocol tracking: sql queries, ldap commands, etc.
    const DEBUG   = 8;
    const INFO    = 4;  // use to log entry creation/update/delete etc.
    const WARNING = 2;
    const ERROR   = 0;

    private static $mode;


    /**
     * Logs tracing message
     *
     * @param string $message Log message
     * @param array  $args    Additional arguments ('file', 'line')
     */
    static function trace($message, $args = array())
    {
        if (self::mode() >= self::TRACE) {
            self::log_message(self::TRACE, $message, $args);
        }
    }

    /**
     * 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::TRACE:
            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::TRACE:
            $prefix = 'TRACE';
            break;
        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 (is_array($args)) {
                if (array_key_exists('file', $args)) {
                    $logline .= ' in ' . $args['file'];
                    unset($args['file']);
                }

                if (array_key_exists('line', $args)) {
                    $logline .= ' on line ' . intval($args['line']);
                    unset($args['line']);
                }
            }

            if (!empty($args)) {
                $logline .= var_export($args, TRUE);
            }
        }

        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::TRACE:
        case 'trace':
        case 'TRACE':
            self::$mode = self::TRACE;
            break;

        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;
    }
}