Newer
Older
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Logs Controller
* @author dev@maarch.org
* @ingroup core
*/
use SrcCore\models\ValidatorModel;
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
if (!defined('_LOG4PHP'))
define(
'_LOG4PHP',
'log4php'
);
if (!defined('_LOGGER_NAME_TECH_DEFAULT'))
define(
'_LOGGER_NAME_TECH_DEFAULT',
'loggerTechnique'
);
if (!defined('_LOGGER_NAME_FUNC_DEFAULT'))
define(
'_LOGGER_NAME_FUNC_DEFAULT',
'loggerFonctionnel'
);
require_once 'apps/maarch_entreprise/tools/log4php/Logger.php';
class LogsController
{
protected static function getLoggingMethodConfFile()
{
$xmlFileName = 'logging_method.xml';
if (file_exists($sLoggingMethodConfFile = 'custom'
.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id']
.DIRECTORY_SEPARATOR . 'apps'
.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id']
.DIRECTORY_SEPARATOR.'xml'
.DIRECTORY_SEPARATOR.$xmlFileName
)) {
return $sLoggingMethodConfFile;
}
if (file_exists($sLoggingMethodConfFile =
'apps'
.DIRECTORY_SEPARATOR.$_SESSION['config']['app_id']
.DIRECTORY_SEPARATOR.'xml'
.DIRECTORY_SEPARATOR.$xmlFileName
)) {
return $sLoggingMethodConfFile;
}
return null;
}
public static function getLoggingFormat(array $aArgs = [])
{
ValidatorModel::notEmpty($aArgs,['key']);
ValidatorModel::intVal($aArgs,['key']);
$logging_methods = self::getLoggingMethod($aArgs);
return empty($logging_methods[$aArgs['key']]['LOG_FORMAT']) ? '[%RESULT%]'
. '[%CODE_METIER%][%WHERE%][%ID%][%HOW%][%USER%]'
. '[%WHAT%][%ID_MODULE%][%REMOTE_IP%]' : $logging_methods[$aArgs['key']]['LOG_FORMAT'];
}
public static function getLoggingCodeMetier(array $aArgs = [])
{
ValidatorModel::notEmpty($aArgs,['key']);
ValidatorModel::intVal($aArgs,['key']);
$logging_methods = self::getLoggingFormat($aArgs);
return empty($logging_methods[$aArgs['key']]['CODE_METIER']) ? '[%RESULT%]'
. '[%CODE_METIER%][%WHERE%][%ID%][%HOW%][%USER%]'
. '[%WHAT%][%ID_MODULE%][%REMOTE_IP%]' : $logging_methods[$aArgs['key']]['CODE_METIER'];
}
/**
* Get the logging method in the configuration file
*/
protected static function getLoggingMethod(array $aArgs = [])
{
$sLoggingMethodConfFile = self::getLoggingMethodConfFile();
$logging_methods = [];
if ( ! $sLoggingMethodConfFile = self::getLoggingMethodConfFile() ) {
$logging_methods[0]['ID'] = 'database';
$logging_methods[0]['ACTIVATED'] = true;
$logging_methods[1]['ID'] = 'log4php';
$logging_methods[1]['ACTIVATED'] = true;
$logging_methods[1]['LOGGER_NAME_TECH'] = 'loggerTechnique';
$logging_methods[1]['LOGGER_NAME_FUNC'] = 'loggerFonctionnel';
$logging_methods[1]['LOG_FORMAT'] = '[%RESULT%][%CODE_METIER%][%WHERE%][%ID%][%HOW%][%USER%][%WHAT%][%ID_MODULE%][%REMOTE_IP%]';
$logging_methods[1]['CODE_METIER'] = 'MAARCH';
return $logging_methods;
}
if (! file_exists($sLoggingMethodConfFile) ) {
throw new \Exception('not file_exists : '.$sLoggingMethodConfFile);
}
$xmlConfig = @simplexml_load_file($sLoggingMethodConfFile);
if (! $xmlConfig) {
throw new \Exception('simplexml_load_file failed : '.$sLoggingMethodConfFile);
}
if (! $xmlConfig->METHOD) {
throw new \Exception('no data METHOD found : '.$sLoggingMethodConfFile);
}
foreach ($xmlConfig->METHOD as $METHOD) {
$id = ((string)$METHOD->ID);
$activated = ((boolean)$METHOD->ENABLED);
$loggerNameTech = ((string)$METHOD->LOGGER_NAME_TECH);
$loggerNameFunc = ((string)$METHOD->LOGGER_NAME_FUNC);
$logFormat = ((string)$METHOD->APPLI_LOG_FORMAT);
$codeMetier = ((string)$METHOD->CODE_METIER);
array_push(
$logging_methods,
array(
'ID' => $id,
'ACTIVATED' => $activated,
'LOGGER_NAME_TECH' => $loggerNameTech,
'LOGGER_NAME_FUNC' => $loggerNameFunc,
'LOG_FORMAT' => $logFormat,
'CODE_METIER' => $codeMetier
)
);
}
$oCache->set($sCache, $logging_methods);
return $logging_methods;
}
protected static function getConfFile(array $aArgs = [])
{
if ( empty($_SESSION['config']['app_id']) ) {
$_SESSION['config']['app_id'] = 'maarch_entreprise';
}
if (
!empty($_SESSION['config']['corepath']) && !empty($_SESSION['custom_override_id'])
&& file_exists($configFileLog4PHP =
$_SESSION['config']['corepath']. DIRECTORY_SEPARATOR . 'custom'
. DIRECTORY_SEPARATOR . $_SESSION['custom_override_id']
. DIRECTORY_SEPARATOR . 'apps'
. DIRECTORY_SEPARATOR . $_SESSION['config']['app_id']
. DIRECTORY_SEPARATOR . 'xml'
. DIRECTORY_SEPARATOR . 'log4php.xml'
)) {
return $configFileLog4PHP;
}
if (file_exists($configFileLog4PHP =
'apps'
. DIRECTORY_SEPARATOR . $_SESSION['config']['app_id']
. DIRECTORY_SEPARATOR . 'xml'
. DIRECTORY_SEPARATOR . 'log4php.xml'
)) {
return $configFileLog4PHP;
}
return 'apps'
. DIRECTORY_SEPARATOR . $_SESSION['config']['app_id']
. DIRECTORY_SEPARATOR . 'xml'
. DIRECTORY_SEPARATOR . 'log4php.default.xml';
}
protected static function format_message(array $aArgs)
{
$aArgs['message'] = @$aArgs['message'];
switch (true) {
case is_object($aArgs['message']):
if ( $aArgs['message'] instanceof \Exception ) {
$e = $aArgs['message'];
$aArgs['code'] = $e->getCode();
$aArgs['message'] = 'Exception: '.$e->getMessage();
$aArgs['debug'] = $e->getTraceAsString();
break;
}
$aArgs['message'] = '--object--';
break;
case is_array($aArgs['message']):
$aArgs['message'] = '--array--';
break;
default:
$aArgs['message'] = (string) $aArgs['message'];
break;
}
$aArgs['message'] = str_replace("\n", '\n', $aArgs['message']);
// Old method :
$aArgs['message'] = TextFormatModel::htmlWasher($aArgs['message'], '');
$aArgs['message'] = TextFormatModel::removeAccent(['string' => $aArgs['message']]);
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
if(!empty($_SESSION['user']['UserId'])){
$aArgs['message'] = '[' . $_SESSION['user']['UserId'] . '] ' . $aArgs['message'];
}
return $aArgs;
}
protected static function logs(array $aArgs = [])
{
// Initialisation du Logger :
\Logger::configure(
self::getConfFile()
);
if ( @$aArgs['class'] ) {
\Logger::getLogger($aArgs['class']);
}
$aLoggingMethods = self::getLoggingMethod();
$aArgs = self::format_message($aArgs);
if ( @$aArgs['class'] ) {
$sLog .= "[class:{$aArgs['class']}]";
\Logger::getLogger($aArgs['class']);
}
foreach ($aLoggingMethods as $logging_method) {
if ( ! $logging_method['ACTIVATED'] ) {
continue;
}
if (isset($aArgs['isTech']) && $aArgs['isTech']) {
$logger = \Logger::getLogger(
$logging_method['LOGGER_NAME_TECH']
);
} else {
if(!isset($logging_method['LOGGER_NAME_FUNC'])){
$logging_method['LOGGER_NAME_FUNC'] = 'loggerFonctionnel';
}
$logger = \Logger::getLogger(
$logging_method['LOGGER_NAME_FUNC']
);
}
if ( empty($logger) ) {
throw new \Exception('logger not-loading', 1);
}
// Format :
$sLog = '';
if ( @$aArgs['file'] ) {
$sLog .= "[file:{$aArgs['file']}]";
}
if ( @$aArgs['class'] ) {
$sLog .= "[class:{$aArgs['class']}]";
}
if ( @$aArgs['function'] ) {
$sLog .= "[function:{$aArgs['function']}]";
}
if ( @$aArgs['code'] ) {
$aArgs['code'] = (int)$aArgs['code'];
$sLog .= "[code:{$aArgs['code']}]";
}
if(!isset($logging_method['CODE_METIER'])){
$logging_method['CODE_METIER'] = 'SIPol';
}
$sLog = str_replace(
'%CODE_METIER%',
$logging_method['CODE_METIER'],
"{$sLog}{$aArgs['message']}"
);
// Log :
switch ($aArgs['type']) {
case 'debug':
case _LEVEL_DEBUG:
$logger->debug($sLog);
break;
case 'info':
case _LEVEL_INFO:
$logger->info($sLog);
break;
case 'warning':
case _LEVEL_WARN:
$logger->warn($sLog);
break;
case 'error':
case _LEVEL_ERROR:
$logger->error($sLog);
break;
case _LEVEL_FATAL:
$logger->fatal($sLog);
break;
default:
$logger->error($sLog);
}
}
return true;
}
public static function debug(array $aArgs = [])
{
$aArgs['type'] = 'debug';
return self::logs($aArgs);
}
public static function info(array $aArgs = [])
{
$aArgs['type'] = 'info';
return self::logs($aArgs);
}
public static function warning(array $aArgs = [])
{
$aArgs['type'] = 'warning';
return self::logs($aArgs);
}
public static function error(array $aArgs = [])
{
$aArgs['type'] = 'error';
return self::logs($aArgs);
}
public static function fatal(array $aArgs = [])
{
$aArgs['type'] = _LEVEL_FATAL;
return self::logs($aArgs);
}
/*
timestart : timestamp Debut
timeend : timestamp Fin
level : level log4php
message : message dans les logs
*/
public function executionTimeLog($timestart, $timeend, $level, $message)
{
if (empty($timeend)){
$timeend = microtime(true);
}
$time = $timeend - $timestart;
self::$level(
['message' => $message.'. Done in ' . number_format($time, 3) . ' secondes.']
);
}
}