-
Notifications
You must be signed in to change notification settings - Fork 1
/
emLoggerTrait.php
115 lines (92 loc) · 3.49 KB
/
emLoggerTrait.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
<?php
namespace Stanford\TensorFlowJS;
/** @var TensorFlowJS $this */
/**
* This trait can be used with External Modules to enable the Stanford emLogger module
*
* Version 1.2
*
* To use this on your project:
* 1) simply add this trait to your EM project root
* 2) change the NAMESPACE of this file to match that of your EM
* 3) Before you declare your EM class, insert the command: include "emLoggerTrait.php";
* 4) Inside your class, before your first function, insert: use emLoggerTrait;
* 5) (optional) Modify your config.json to include these two optional debug settings
(OPTIONAL) INSERT THESE OPTIONS INTO THE CONFIG.JSON
"system-settings": [
{
"key": "enable-system-debug-logging",
"name": "<b>Enable Debug Logging (system-wide)</b><i>(optional)</i> Requires installation and configuration of emLogger",
"required": false,
"type": "checkbox"
},
],
"project-settings": [
{
"key": "enable-project-debug-logging",
"name": "<b>Enable Debug Logging</b><br><i>(optional)</i> Requires installation and configuration of emLogger",
"required": false,
"type": "checkbox"
},
],
*/
trait emLoggerTrait
{
private $emLoggerEnabled = null; // Cache logger enabled
private $emLoggerDebug = null; // Cache debug mode
/**
* Determine if we are in debug mode either on system or project level and cache
* @return bool
*/
function emLoggerDebugMode() {
// Set if debug mode once on the first log call
if (is_null($this->emLoggerDebug)) {
$systemDebug = $this->getSystemSetting('enable-system-debug-logging');
$projectDebug = !empty($_GET['pid']) && $this->getProjectSetting('enable-project-debug-logging');
$this->emLoggerDebug = $systemDebug || $projectDebug;
}
return $this->emLoggerDebug;
}
/**
* Obtain an instance of emLogger or false if not installed / active
* @return bool|mixed
*/
function emLoggerInstance() {
// This is the first time used, see if it is enabled on server
if (is_null($this->emLoggerEnabled)) {
$versions = \ExternalModules\ExternalModules::getEnabledModules();
$this->emLoggerEnabled = isset($versions['em_logger']);
}
// Return instance if enabled
if ($this->emLoggerEnabled) {
// Try to return the instance of emLogger (which is cached by the EM framework)
try {
return \ExternalModules\ExternalModules::getModuleInstance('em_logger');
} catch (\Exception $e) {
// Unable to initialize em_logger
error_log("Exception caught - unable to initialize emLogger in " . __NAMESPACE__ . "/" . __FILE__ . "!");
$this->emLoggerEnabled = false;
}
}
return false;
}
/**
* Do the logging
* The reason we broke it into three functions was to reduce complexity with backtrace and the calling function
*/
function emLog() {
if ($emLogger = $this->emLoggerInstance()) $emLogger->emLog($this->PREFIX, func_get_args(), "INFO");
}
/**
* Wrapper for logging an error
*/
function emError() {
if ($emLogger = $this->emLoggerInstance()) $emLogger->emLog($this->PREFIX, func_get_args(), "ERROR");
}
/**
* Wrapper for logging debug statements
*/
function emDebug() {
if ( $this->emLoggerDebugMode() && ($emLogger = $this->emLoggerInstance()) ) $emLogger->emLog($this->PREFIX, func_get_args(), "DEBUG");
}
}