6
6
* Class CLIBase
7
7
*
8
8
* All base functionality is implemented here.
9
- *
9
+ *
10
10
* Your commandline should not inherit from this class, but from one of the *CLI* classes
11
11
*
12
12
* @author Andreas Gohr <andi@splitbrain.org>
@@ -21,19 +21,65 @@ abstract class Base
21
21
/** @var Colors */
22
22
public $ colors ;
23
23
24
- /** @var array PSR-3 compatible loglevels and their prefix, color, output channel */
24
+ /** @var array PSR-3 compatible loglevels and their prefix, color, output channel, enabled status */
25
25
protected $ loglevel = array (
26
- 'debug ' => array ('' , Colors::C_RESET , STDOUT ),
27
- 'info ' => array ('i ' , Colors::C_CYAN , STDOUT ),
28
- 'notice ' => array ('☛ ' , Colors::C_CYAN , STDOUT ),
29
- 'success ' => array ('✓ ' , Colors::C_GREEN , STDOUT ),
30
- 'warning ' => array ('⚠ ' , Colors::C_BROWN , STDERR ),
31
- 'error ' => array ('✗ ' , Colors::C_RED , STDERR ),
32
- 'critical ' => array ('☠ ' , Colors::C_LIGHTRED , STDERR ),
33
- 'alert ' => array ('✖ ' , Colors::C_LIGHTRED , STDERR ),
34
- 'emergency ' => array ('✘ ' , Colors::C_LIGHTRED , STDERR ),
26
+ 'debug ' => array (
27
+ 'icon ' => '' ,
28
+ 'color ' => Colors::C_RESET ,
29
+ 'channel ' => STDOUT ,
30
+ 'enabled ' => true
31
+ ),
32
+ 'info ' => array (
33
+ 'icon ' => 'i ' ,
34
+ 'color ' => Colors::C_CYAN ,
35
+ 'channel ' => STDOUT ,
36
+ 'enabled ' => true
37
+ ),
38
+ 'notice ' => array (
39
+ 'icon ' => '☛ ' ,
40
+ 'color ' => Colors::C_CYAN ,
41
+ 'channel ' => STDOUT ,
42
+ 'enabled ' => true
43
+ ),
44
+ 'success ' => array (
45
+ 'icon ' => '✓ ' ,
46
+ 'color ' => Colors::C_GREEN ,
47
+ 'channel ' => STDOUT ,
48
+ 'enabled ' => true
49
+ ),
50
+ 'warning ' => array (
51
+ 'icon ' => '⚠ ' ,
52
+ 'color ' => Colors::C_BROWN ,
53
+ 'channel ' => STDERR ,
54
+ 'enabled ' => true
55
+ ),
56
+ 'error ' => array (
57
+ 'icon ' => '✗ ' ,
58
+ 'color ' => Colors::C_RED ,
59
+ 'channel ' => STDERR ,
60
+ 'enabled ' => true
61
+ ),
62
+ 'critical ' => array (
63
+ 'icon ' => '☠ ' ,
64
+ 'color ' => Colors::C_LIGHTRED ,
65
+ 'channel ' => STDERR ,
66
+ 'enabled ' => true
67
+ ),
68
+ 'alert ' => array (
69
+ 'icon ' => '✖ ' ,
70
+ 'color ' => Colors::C_LIGHTRED ,
71
+ 'channel ' => STDERR ,
72
+ 'enabled ' => true
73
+ ),
74
+ 'emergency ' => array (
75
+ 'icon ' => '✘ ' ,
76
+ 'color ' => Colors::C_LIGHTRED ,
77
+ 'channel ' => STDERR ,
78
+ 'enabled ' => true
79
+ ),
35
80
);
36
81
82
+ /** @var string default log level */
37
83
protected $ logdefault = 'info ' ;
38
84
39
85
/**
@@ -144,11 +190,7 @@ protected function handleDefaultOptions()
144
190
protected function setupLogging ()
145
191
{
146
192
$ level = $ this ->options ->getOpt ('loglevel ' , $ this ->logdefault );
147
- if (!isset ($ this ->loglevel [$ level ])) $ this ->fatal ('Unknown log level ' );
148
- foreach (array_keys ($ this ->loglevel ) as $ l ) {
149
- if ($ l == $ level ) break ;
150
- unset($ this ->loglevel [$ l ]);
151
- }
193
+ $ this ->setLogLevel ($ level );
152
194
}
153
195
154
196
/**
@@ -179,6 +221,21 @@ protected function execute()
179
221
180
222
// region logging
181
223
224
+ /**
225
+ * Set the current log level
226
+ *
227
+ * @param string $level
228
+ */
229
+ public function setLogLevel ($ level )
230
+ {
231
+ if (!isset ($ this ->loglevel [$ level ])) $ this ->fatal ('Unknown log level ' );
232
+ $ enable = true ;
233
+ foreach (array_keys ($ this ->loglevel ) as $ l ) {
234
+ $ this ->loglevel [$ l ]['enabled ' ] = $ enable ;
235
+ if ($ l == $ level ) $ enable = false ;
236
+ }
237
+ }
238
+
182
239
/**
183
240
* Exits the program on a fatal error
184
241
*
@@ -222,17 +279,20 @@ public function success($string, array $context = array())
222
279
*/
223
280
protected function logMessage ($ level , $ message , array $ context = array ())
224
281
{
225
- // is this log level wanted?
226
- if (!isset ($ this ->loglevel [$ level ])) return ;
282
+ // unknown level is always an error
283
+ if (!isset ($ this ->loglevel [$ level ])) $ level = ' error ' ;
227
284
228
- /** @var string $prefix */
229
- /** @var string $color */
230
- /** @var resource $channel */
231
- list ($ prefix , $ color , $ channel ) = $ this ->loglevel [$ level ];
232
- if (!$ this ->colors ->isEnabled ()) $ prefix = '' ;
285
+ $ info = $ this ->loglevel [$ level ];
286
+ if (!$ info ['enabled ' ]) return ; // no logging for this level
233
287
234
288
$ message = $ this ->interpolate ($ message , $ context );
235
- $ this ->colors ->ptln ($ prefix . $ message , $ color , $ channel );
289
+
290
+ // when colors are wanted, we also add the icon
291
+ if ($ this ->colors ->isEnabled ()) {
292
+ $ message = $ info ['icon ' ] . $ message ;
293
+ }
294
+
295
+ $ this ->colors ->ptln ($ message , $ info ['color ' ], $ info ['channel ' ]);
236
296
}
237
297
238
298
/**
0 commit comments