/
FroxlorLogger.php
291 lines (271 loc) · 6.73 KB
/
FroxlorLogger.php
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you can also view it online at
* https://files.froxlor.org/misc/COPYING.txt
*
* @copyright the authors
* @author Froxlor team <team@froxlor.org>
* @license https://files.froxlor.org/misc/COPYING.txt GPLv2
*/
namespace Froxlor;
use Froxlor\System\MysqlHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogHandler;
use Monolog\Logger;
/**
* Class FroxlorLogger
*/
class FroxlorLogger
{
const USR_ACTION = '10';
const RES_ACTION = '20';
const ADM_ACTION = '30';
const CRON_ACTION = '40';
const LOGIN_ACTION = '50';
const LOG_ERROR = '99';
/**
* current \Monolog\Logger object
*
* @var Logger
*/
private static $ml = null;
/**
* LogTypes Array
*
* @var array
*/
private static $logtypes = null;
/**
* whether to output log-messages to STDOUT (cron)
*
* @var bool
*/
private static $crondebug_flag = false;
/**
* user info of logged in user
*
* @var array
*/
private static $userinfo = [];
/**
* whether the logger object has already been initialized
*
* @var bool
*/
private static $is_initialized = false;
/**
* Class constructor.
*/
protected function __construct($userinfo = [])
{
$this->initMonolog();
self::$userinfo = $userinfo;
self::$logtypes = [];
if ((Settings::Get('logger.logtypes') == null || Settings::Get('logger.logtypes') == '') && (Settings::Get('logger.enabled') !== null && Settings::Get('logger.enabled'))) {
self::$logtypes[0] = 'syslog';
self::$logtypes[1] = 'mysql';
} else {
if (Settings::Get('logger.logtypes') !== null && Settings::Get('logger.logtypes') != '') {
self::$logtypes = explode(',', Settings::Get('logger.logtypes'));
} else {
self::$logtypes = null;
}
}
if (self::$is_initialized == false) {
foreach (self::$logtypes as $logger) {
switch ($logger) {
case 'syslog':
self::$ml->pushHandler(new SyslogHandler('froxlor', LOG_USER, Logger::DEBUG));
break;
case 'file':
$logger_logfile = FileDir::makeCorrectFile(Froxlor::getInstallDir() . '/logs/' . Settings::Get('logger.logfile'));
// is_writable needs an existing file to check if it's actually writable
@touch($logger_logfile);
if (empty($logger_logfile) || !is_writable($logger_logfile)) {
Settings::Set('logger.logfile', 'froxlor.log');
$logger_logfile = FileDir::makeCorrectFile(Froxlor::getInstallDir() . '/logs/froxlor.log');
@touch($logger_logfile);
if (empty($logger_logfile) || !is_writable($logger_logfile)) {
// not writable in our own directory? Skip
break;
}
}
self::$ml->pushHandler(new StreamHandler($logger_logfile, Logger::DEBUG));
break;
case 'mysql':
self::$ml->pushHandler(new MysqlHandler(Logger::DEBUG));
break;
}
}
self::$is_initialized = true;
}
}
/**
* initiate monolog object
*
* @return Logger
*/
private function initMonolog()
{
if (empty(self::$ml)) {
// get Theme object
self::$ml = new Logger('froxlor');
}
return self::$ml;
}
/**
* return FroxlorLogger instance
*
* @param array $userinfo
*
* @return FroxlorLogger
*/
public static function getInstanceOf($userinfo = [])
{
if (empty($userinfo)) {
$userinfo = [
'loginname' => 'system'
];
}
return new FroxlorLogger($userinfo);
}
/**
* logs a given text to all enabled logger-facilities
*
* @param int $action
* @param int $type
* @param string $text
*/
public function logAction($action = FroxlorLogger::USR_ACTION, $type = LOG_NOTICE, $text = null)
{
// not logging normal stuff if not set to "paranoid" logging
if (!self::$crondebug_flag && Settings::Get('logger.severity') == '1' && $type > LOG_NOTICE) {
return;
}
if (empty(self::$ml)) {
$this->initMonolog();
}
if (self::$crondebug_flag || ($action == FroxlorLogger::CRON_ACTION && $type <= LOG_WARNING)) {
echo "[" . $this->getLogLevelDesc($type) . "] " . $text . PHP_EOL;
}
// warnings, errors and critical messages WILL be logged
if (Settings::Get('logger.log_cron') == '0' && $action == FroxlorLogger::CRON_ACTION && $type > LOG_WARNING) {
return;
}
$logExtra = [
'source' => $this->getActionTypeDesc($action),
'action' => $action,
'user' => self::$userinfo['loginname']
];
switch ($type) {
case LOG_DEBUG:
self::$ml->addDebug($text, $logExtra);
break;
case LOG_INFO:
self::$ml->addInfo($text, $logExtra);
break;
case LOG_NOTICE:
self::$ml->addNotice($text, $logExtra);
break;
case LOG_WARNING:
self::$ml->addWarning($text, $logExtra);
break;
case LOG_ERR:
self::$ml->addError($text, $logExtra);
break;
default:
self::$ml->addDebug($text, $logExtra);
}
}
public function getLogLevelDesc($type)
{
switch ($type) {
case LOG_INFO:
$_type = 'information';
break;
case LOG_NOTICE:
$_type = 'notice';
break;
case LOG_WARNING:
$_type = 'warning';
break;
case LOG_ERR:
$_type = 'error';
break;
case LOG_CRIT:
$_type = 'critical';
break;
case LOG_DEBUG:
$_type = 'debug';
break;
default:
$_type = 'unknown';
break;
}
return $_type;
}
private function getActionTypeDesc($action)
{
switch ($action) {
case FroxlorLogger::USR_ACTION:
$_action = 'user';
break;
case FroxlorLogger::ADM_ACTION:
$_action = 'admin';
break;
case FroxlorLogger::RES_ACTION:
$_action = 'reseller';
break;
case FroxlorLogger::CRON_ACTION:
$_action = 'cron';
break;
case FroxlorLogger::LOGIN_ACTION:
$_action = 'login';
break;
default:
$_action = 'unknown';
break;
}
return $_action;
}
/**
* Set whether to log cron-runs
*
* @param int $cronlog
*
* @return int
*/
public function setCronLog(int $cronlog = 0)
{
if ($cronlog < 0 || $cronlog > 2) {
$cronlog = 0;
}
Settings::Set('logger.log_cron', $cronlog);
return $cronlog;
}
/**
* setter for crondebug-flag
*
* @param bool $flag
*
* @return void
*/
public function setCronDebugFlag(bool $flag = false)
{
self::$crondebug_flag = $flag;
}
}