PHP library for rendering colored CLI messages.
composer require alexlcdee/colorcli
Logger class is a PSR-3 compatible logger. Provides colored log level tags.
Colors can be customised with
setBGColor($level, BackgroundColors $bgcolor = null)
and
setFGColor($level, ForegroundColors $fgcolor = null)
By default, Logger passes error-like messages to STDERR and info-like messages to STDOUT. This behavior can be also customised with
setOutputStream($level, resource $stream)
Logger has support of context param and placeholders in message.
$logger->debug('Debug output from {fileName}:{line}', [
'fileName' => __FILE__,
'line' => __LINE__
]);
Context key 'exception' accepts instance of Exception and renders exception name with message, filename, line and stacktrace right after logged message.
// catch an exception and pass it to log as context param
try {
// your code here
} catch (Exception $e) {
$logger->alert("I've caught an exception", ['exception' => $e]);
}
Messages with newlines will be padded with empty string with length of level tag.
require_once dirname(__DIR__) . '/vendor/autoload.php';
$logger = new \ColorCLI\Logger();
$logger->emergency('System is unusable.');
$logger->alert('Action must be taken immediately.');
$logger->critical('Critical conditions.');
$logger->error('Runtime errors that do not require immediate action but should typically');
$logger->warning('Exceptional occurrences that are not errors.');
$logger->notice('Normal but significant events.');
$logger->info('Interesting events.');
$logger->debug('Detailed debug information.');
$logger->debug('Contextual debug output from {fileName}:{line}', [
'fileName' => __FILE__,
'line' => __LINE__
]);
$logger->info("Processing
multiline message");
try {
(new Throwing())->throwException();
} catch (Exception $e) {
$logger->alert('Caught an exception!', ['exception' => $e]);
}
The example above provides the following output:
Provides static method to color input string
// Print string with red font on yellow background
echo ColorHelper::colorString('I am a RED string on YELLOW background', ForegroundColors::RED(), BackgroundColors::YELLOW());
echo PHP_EOL;
The example above provides the following output:
These classes are simply MyCLabs\Enum\Enum classes and provides supported colors lists. Read more about MyCLabs\Enum
ColorHelper idea was taken from article PHP CLI Colors – PHP Class Command Line Colors (bash)