1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Psr\Log\LogLevel;
13: use Xoops\Core\PreloadItem;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class MonologPreload extends PreloadItem
28: {
29: private static $registry = array();
30:
31: private static $configs = null;
32:
33: 34: 35: 36: 37:
38: private static function getConfigs()
39: {
40: $xoops = \Xoops::getInstance();
41: if (array_key_exists('monolog_default_configs', self::$configs)
42: && self::$configs['monolog_default_configs'] == true) {
43: self::$configs = $xoops->getModuleConfigs('monolog');
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: return self::$configs;
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public static function eventCoreException($e)
82: {
83: MonologLogger::getInstance()->addException($e);
84: }
85:
86: 87: 88: 89: 90: 91: 92:
93: public static function eventCoreIncludeCommonClassmaps($args)
94: {
95: $path = dirname(__DIR__);
96: XoopsLoad::addMap(array(
97: 'monologlogger' => $path . '/class/monologlogger.php',
98: ));
99: }
100:
101: 102: 103: 104: 105: 106: 107:
108: public static function eventCoreIncludeCommonSecurity($args)
109: {
110: $cache = \Xoops::getInstance()->cache();
111: $key = 'system/module/configs/monolog';
112:
113: self::$configs=array();
114: self::$configs['monolog_enable'] = false;
115: self::$configs['monolog_default_configs']=true;
116:
117:
118: $monolog_configs = $cache->read($key);
119: if ($monolog_configs!==false) {
120: self::$configs = $monolog_configs;
121: }
122: $logger = MonologLogger::getInstance();
123:
124: if (self::$configs['monolog_enable']) {
125: $logger->setConfigs(self::$configs);
126: $logger->enable();
127: } else {
128:
129: self::$configs['include_blocks'] = false;
130: self::$configs['include_deprecated'] = false;
131: self::$configs['include_extra'] = false;
132: self::$configs['include_queries'] = false;
133: self::$configs['include_timers'] = false;
134: $logger->setConfigs(self::$configs);
135: }
136: $logger->startTime();
137: $logger->startTime('XOOPS Boot');
138: }
139:
140: 141: 142: 143: 144: 145: 146:
147: public static function eventCoreDatabaseNoconn($args)
148: {
149: if (class_exists('MonologLogger')) {
150:
151: $db = $args[0];
152: MonologLogger::getInstance()->log(LogLevel::ALERT, $db->error(), array('errno' => $db->errno()));
153: }
154: }
155:
156: 157: 158: 159: 160: 161: 162:
163: public static function eventCoreDatabaseNodb($args)
164: {
165: if (class_exists('MonologLogger')) {
166:
167: $db = $args[0];
168: MonologLogger::getInstance()->log(LogLevel::ALERT, $db->error(), array('errno' => $db->errno()));
169: }
170: }
171:
172: 173: 174: 175: 176: 177: 178:
179: public static function eventCoreDatabaseQueryComplete($args)
180: {
181: $sql = $args['sql'];
182: $context = array(
183: 'channel'=>'Queries',
184: 'query_time'=>$args['executionMS'],
185: 'params'=>$args['params'],
186: 'types'=>$args['types'],
187: );
188:
189: MonologLogger::getInstance()->log(LogLevel::INFO, $sql, $context);
190: }
191:
192: 193: 194: 195: 196: 197: 198:
199: public static function eventCoreIncludeCommonConfigsSuccess($args)
200: {
201: $xoops = Xoops::getInstance();
202: $xoops->loadLocale();
203: $xoops->loadLanguage('main', 'monolog');
204: }
205:
206: 207: 208: 209: 210:
211: public static function eventCoreIncludeCommonAuthSuccess()
212: {
213: $logger = MonologLogger::getInstance();
214: $configs = self::getConfigs();
215: $logger->setConfigs($configs);
216: if ($configs['monolog_enable']) {
217: $logger->enable();
218: } else {
219: $logger->disable();
220: }
221: }
222:
223: 224: 225: 226: 227: 228: 229:
230: public static function eventCoreIncludeCommonEnd($args)
231: {
232: $logger = MonologLogger::getInstance();
233: $logger->stopTime('XOOPS Boot');
234: $logger->startTime('Module init');
235: }
236:
237: 238: 239: 240: 241: 242: 243:
244: public static function eventCoreTemplateConstructStart($args)
245: {
246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256:
257: }
258:
259: 260: 261: 262: 263: 264: 265:
266: public static function eventCoreThemeRenderStart($args)
267: {
268: MonologLogger::getInstance()->startTime('Page rendering');
269: }
270:
271: 272: 273: 274: 275: 276: 277:
278: public static function eventCoreThemeRenderEnd($args)
279: {
280: MonologLogger::getInstance()->stopTime('Page rendering');
281: }
282:
283: 284: 285: 286: 287: 288: 289:
290: public static function eventCoreThemeCheckcacheSuccess($args)
291: {
292: $template = $args[0];
293: $theme = $args[1];
294: MonologLogger::getInstance()->addExtra(
295: $template,
296: sprintf('Cached (regenerates every %d seconds)', $theme->contentCacheLifetime)
297: );
298: }
299:
300: 301: 302: 303: 304: 305: 306:
307: public static function eventCoreThemeblocksBuildblockStart($args)
308: {
309:
310: $block = $args[0];
311: $isCached= $args[1];
312: $context = array('channel'=>'Blocks', 'cached'=>$isCached, 'cachetime'=>$block->getVar('bcachetime'));
313: MonologLogger::getInstance()->log(LogLevel::INFO, $block->getVar('name'), $context);
314:
315: }
316:
317: 318: 319: 320: 321: 322: 323:
324: public static function eventCoreDeprecated($args)
325: {
326: $message = $args[0];
327: MonologLogger::getInstance()->log(LogLevel::WARNING, $message, array('channel'=>'Deprecated'));
328: }
329:
330: 331: 332: 333: 334: 335: 336:
337: public static function eventCoreDisableerrorreporting($args)
338: {
339: MonologLogger::getInstance()->disable();
340: }
341:
342: 343: 344: 345: 346: 347: 348:
349: public static function eventCoreHeaderStart($args)
350: {
351: $logger = MonologLogger::getInstance();
352: $logger->stopTime('Module init');
353: $logger->startTime('XOOPS output init');
354: }
355:
356: 357: 358: 359: 360: 361: 362:
363: public static function eventCoreHeaderEnd($args)
364: {
365: $logger = MonologLogger::getInstance();
366: $logger->stopTime('XOOPS output init');
367: $logger->startTime('Module display');
368: }
369:
370: 371: 372: 373: 374: 375: 376:
377: public static function eventCoreFooterStart($args)
378: {
379: $logger = MonologLogger::getInstance();
380: $logger->stopTime('Module display');
381: $logger->addExtra(
382: _MD_MONOLOG_MEMORY,
383: sprintf(
384: _MD_MONOLOG_MEM_USAGE,
385: (float) memory_get_usage(true)/1000000,
386: (float) memory_get_peak_usage(true)/1000000
387: )
388: );
389: $logger->addExtra(
390: _MD_MONOLOG_INCLUDED_FILES,
391: sprintf(_MD_MONOLOG_FILES, count(get_included_files()))
392: );
393: $logger->stopTime();
394: }
395:
396: 397: 398: 399: 400: 401: 402:
403: public static function eventCoreFooterEnd($args)
404: {
405:
406:
407:
408: }
409:
410: 411: 412: 413: 414: 415: 416:
417: public static function eventCoreSecurityValidatetokenEnd($args)
418: {
419: $logger = MonologLogger::getInstance();
420: $logs = $args[0];
421: foreach ($logs as $log) {
422: $context = array('channel'=>'Extra', 'name'=>$log[0]);
423: $logger->log(LogLevel::INFO, $log[1], $context);
424: }
425: }
426:
427: 428: 429: 430: 431: 432: 433:
434: public static function eventCoreModuleAddlog($args)
435: {
436: $context = array('channel'=>'Extra', 'name'=>$args[0]);
437: MonologLogger::getInstance()->log(LogLevel::DEBUG, $args[1], $context);
438:
439: }
440:
441: 442: 443: 444: 445: 446: 447:
448: public static function eventSystemPreferencesSave($args)
449: {
450: $configs = array();
451: $cache = \Xoops::getInstance()->cache();
452: $key = 'system/module/configs/monolog';
453:
454: if (isset($_REQUEST['monolog_enable'])) {
455: $cache->delete($key);
456:
457: $helper = \Xoops::getInstance()->getModuleHelper('monolog');
458: $configs['monolog_enable'] = (bool) $helper->getConfig('monolog_enable');
459: $configs['include_blocks'] = (bool) $helper->getConfig('include_blocks');
460: $configs['include_deprecated'] = (bool) $helper->getConfig('include_deprecated');
461: $configs['include_extra'] = (bool) $helper->getConfig('include_extra');
462: $configs['include_queries'] = (bool) $helper->getConfig('include_queries');
463: $configs['include_timers'] = (bool) $helper->getConfig('include_timers');
464: $configs['logging_threshold'] = $helper->getConfig('logging_threshold');
465: $configs['log_file_path'] = $helper->getConfig('log_file_path');
466: $configs['max_versions'] = (int) $helper->getConfig('max_versions');
467:
468: $cache->write($key, $configs);
469: }
470:
471: }
472: }
473: