alexlcdee / colorcli
Console Logger with color highlighted level tags
Requires
- php: >=5.4
- myclabs/php-enum: 1.5.1
- psr/log: ^1.0
This package is auto-updated.
Last update: 2024-10-23 18:32:55 UTC
README
PHP library for rendering colored CLI messages.
Usage
Installation
composer require alexlcdee/colorcli
Logger
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.
Usage
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:
ColorHelper
Provides static method to color input string
Usage
// 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:
BackgroundColors and ForegroundColors
These classes are simply MyCLabs\Enum\Enum classes and provides supported colors lists. Read more about MyCLabs\Enum
Credits
ColorHelper idea was taken from article PHP CLI Colors – PHP Class Command Line Colors (bash)