25
25
26
26
namespace doganoo \PHPUtil \Log ;
27
27
28
- use doganoo \PHPUtil \FileSystem \FileHandler ;
29
-
30
28
/**
31
29
* Class FileLogger
32
30
*
33
31
* TODO add other log levels
34
32
*
35
33
* @package doganoo\PHPUtil\Log
36
34
*/
37
- class FileLogger {
38
- /** @var int DEBUG log level 0 */
39
- public const DEBUG = 0 ;
40
- /** @var int INFO log level 1 */
41
- public const INFO = 1 ;
42
- /** @var int WARN log level 2 */
43
- public const WARN = 2 ;
44
- /** @var int ERROR log level 3 */
45
- public const ERROR = 3 ;
46
- /** @var int FATAL log level 4 */
47
- public const FATAL = 4 ;
48
- /** @var int SIMPLE */
49
- public const SIMPLE = 0 ;
50
- /** @var int NORMAL */
51
- public const NORMAL = 1 ;
52
- /** @var int DESCRIPTIVE */
53
- public const DESCRIPTIVE = 2 ;
54
- /** @var int $level */
55
- private static $ level = 0 ;
56
- /** @var string $path */
57
- private static $ path = __DIR__ . "/logfile.log " ;
58
- /** @var string $EOL */
59
- private static $ EOL = "\n" ;
60
- /** @var bool $logEnabled */
61
- private static $ logEnabled = true ;
62
- /** @var int $mode */
63
- private static $ mode = FileLogger::SIMPLE ;
35
+ class FileLogger extends Logger {
36
+ private static $ path = null ;
64
37
65
38
/**
66
39
* Logger constructor prevents class instantiation
@@ -69,122 +42,36 @@ private function __construct() {
69
42
}
70
43
71
44
/**
72
- * sets the end of line after a message is logged
73
- *
74
- * @param string $eol
75
- */
76
- public static function setEOL (string $ eol ) {
77
- self ::$ EOL = $ eol ;
78
- }
79
-
80
- /**
81
- * defines whether logging is enabled or not.
82
- *
83
- * @param bool $logEnabled
84
- */
85
- public static function setLogEnabled (bool $ logEnabled ): void {
86
- self ::$ logEnabled = $ logEnabled ;
87
- }
88
-
89
- /**
90
- * @param int $mode
91
- * @return bool
92
- */
93
- public static function setMode (int $ mode ): bool {
94
- if ($ mode === FileLogger::SIMPLE
95
- || $ mode === FileLogger::NORMAL
96
- || $ mode === FileLogger::DESCRIPTIVE
97
- ) {
98
- FileLogger::$ mode = $ mode ;
99
- return true ;
100
- }
101
- return false ;
102
- }
103
-
104
- /**
105
- * setting the log level. The level can be one of:
106
- *
107
- * <ul>0 = DEBUG</ul>
108
- * <ul>1 = INFO</ul>
109
- * <ul>2 = WARN</ul>
110
- * <ul>3 = ERROR</ul>
111
- * <ul>4 = FATAL</ul>
112
- *
113
- * if $level does not match to any of these levels, the
114
- * log level will be initialized to ERROR (3).
115
- *
116
- * @param int $level
117
- */
118
- public static function setLogLevel (int $ level ): void {
119
- if ($ level >= FileLogger::DEBUG && $ level <= FileLogger::FATAL ) {
120
- self ::$ level = $ level ;
121
- } else {
122
- self ::$ level = FileLogger::ERROR ;
123
- }
124
-
125
- }
126
-
127
- /**
128
- * logs a message with log level DEBUG
45
+ * logs a message with log level INFO
129
46
*
130
47
* @param $message
131
48
*/
132
- public static function debug ($ message ) {
133
- self ::log ($ message , FileLogger::DEBUG );
134
- }
135
-
136
- /**
137
- * logs a message to the console
138
- *
139
- * @param string $message
140
- * @param int $level
141
- */
142
- private static function log (string $ message , int $ level ) {
143
- $ description = "" ;
144
- $ backTrace = \debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS );
145
- if (FileLogger::$ mode === FileLogger::NORMAL ) $ description = $ backTrace [0 ]["file " ] . " : " ;
146
- if (FileLogger::$ mode === FileLogger::DESCRIPTIVE ) $ description = \json_encode ($ backTrace ) . " : " ;;
147
-
148
- $ logLevelDescription = FileLogger::getLogLevelDescription ($ level );
149
- $ fileHandler = new FileHandler (FileLogger::getPath ());
150
- $ fileHandler ->forceCreate ();
151
- $ output = "" ;
152
- if ($ level >= self ::$ level
153
- && self ::$ logEnabled
154
- && $ fileHandler ->isFile ()) {
155
- $ output .= (new \DateTime ())->format ("Y-m-d H:i:s " );
156
- $ output .= " : " ;
157
- $ output .= $ logLevelDescription ;
158
- $ output .= " : " ;
159
- $ output .= $ description ;
160
- $ output .= $ message ;
161
- $ output .= self ::$ EOL ;
162
- \file_put_contents (self ::$ path , $ output , \FILE_APPEND );
163
- }
49
+ public static function info ($ message ) {
50
+ parent ::setConfig (FileLogger::getConfiguration ());
51
+ parent ::info ($ message );
164
52
}
165
53
166
- /**
167
- * returns a string that describes the current log level
168
- *
169
- * @param int $level
170
- * @return string
171
- */
172
- private static function getLogLevelDescription (int $ level ): string {
173
- if ($ level === Logger::DEBUG ) {
174
- return "debug " ;
175
- }
176
- if ($ level === Logger::INFO ) {
177
- return "info " ;
178
- }
179
- if ($ level === Logger::WARN ) {
180
- return "warn " ;
181
- }
182
- if ($ level === Logger::ERROR ) {
183
- return "error " ;
184
- }
185
- if ($ level === Logger::FATAL ) {
186
- return "fatal " ;
187
- }
54
+ private static function getConfiguration (): array {
55
+ return array (
56
+ 'appenders ' => array (
57
+ 'default ' => array (
58
+ 'class ' => 'LoggerAppenderFile ' ,
59
+ 'layout ' => array (
60
+ 'class ' => 'LoggerLayoutPattern ' ,
61
+ 'params ' => array (
62
+ 'conversionPattern ' => '%date{Y-m-d H:i:s} %logger %-5level %msg%n '
63
+ )
64
+ ),
65
+ 'params ' => array (
66
+ 'file ' => FileLogger::getPath (),
67
+ 'append ' => true ,
68
+ ),
69
+ ),
70
+ ),
71
+ 'rootLogger ' => array (
72
+ 'appenders ' => array ('default ' ),
73
+ ),
74
+ );
188
75
}
189
76
190
77
/**
@@ -205,22 +92,14 @@ public static function setPath(string $path) {
205
92
self ::$ path = $ path ;
206
93
}
207
94
208
- /**
209
- * logs a message with log level INFO
210
- *
211
- * @param $message
212
- */
213
- public static function info ($ message ) {
214
- self ::log ($ message , FileLogger::INFO );
215
- }
216
-
217
95
/**
218
96
* logs a message with log level WARN
219
97
*
220
98
* @param $message
221
99
*/
222
100
public static function warn ($ message ) {
223
- self ::log ($ message , FileLogger::WARN );
101
+ parent ::setConfig (FileLogger::getConfiguration ());
102
+ parent ::warn ($ message );
224
103
}
225
104
226
105
/**
@@ -229,7 +108,8 @@ public static function warn($message) {
229
108
* @param $message
230
109
*/
231
110
public static function error ($ message ) {
232
- self ::log ($ message , FileLogger::ERROR );
111
+ parent ::setConfig (FileLogger::getConfiguration ());
112
+ parent ::error ($ message );
233
113
}
234
114
235
115
/**
@@ -238,7 +118,29 @@ public static function error($message) {
238
118
* @param $message
239
119
*/
240
120
public static function fatal ($ message ) {
241
- self ::log ($ message , FileLogger::FATAL );
121
+ parent ::setConfig (FileLogger::getConfiguration ());
122
+ parent ::fatal ($ message );
242
123
}
243
124
125
+ /**
126
+ * logs a message with log level TRACE
127
+ *
128
+ * @param $message
129
+ */
130
+ public static function trace ($ message ) {
131
+ parent ::setConfig (FileLogger::getConfiguration ());
132
+ parent ::trace ($ message );
133
+ }
134
+
135
+ /**
136
+ * logs a message with log level DEBUG
137
+ *
138
+ * @param $message
139
+ */
140
+ public static function debug ($ message ) {
141
+ parent ::setConfig (FileLogger::getConfiguration ());
142
+ parent ::debug ($ message );
143
+ }
144
+
145
+
244
146
}
0 commit comments