Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7044374

Browse files
Allow dynamic log level setting
This was inspired by #32 but I am not sure this actually the way to go. Thinking a bit more about the intial post, an even better way would be to add config file reading to the Options class. Maybe from a .env file?
1 parent 93c29c1 commit 7044374

File tree

1 file changed

+84
-24
lines changed

1 file changed

+84
-24
lines changed

‎src/Base.php

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Class CLIBase
77
*
88
* All base functionality is implemented here.
9-
*
9+
*
1010
* Your commandline should not inherit from this class, but from one of the *CLI* classes
1111
*
1212
* @author Andreas Gohr <andi@splitbrain.org>
@@ -21,19 +21,65 @@ abstract class Base
2121
/** @var Colors */
2222
public $colors;
2323

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 */
2525
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+
),
3580
);
3681

82+
/** @var string default log level */
3783
protected $logdefault = 'info';
3884

3985
/**
@@ -144,11 +190,7 @@ protected function handleDefaultOptions()
144190
protected function setupLogging()
145191
{
146192
$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);
152194
}
153195

154196
/**
@@ -179,6 +221,21 @@ protected function execute()
179221

180222
// region logging
181223

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+
182239
/**
183240
* Exits the program on a fatal error
184241
*
@@ -222,17 +279,20 @@ public function success($string, array $context = array())
222279
*/
223280
protected function logMessage($level, $message, array $context = array())
224281
{
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';
227284

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
233287

234288
$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']);
236296
}
237297

238298
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /