Skip to content

Commit

Permalink
Colorize, optional context message
Browse files Browse the repository at this point in the history
  • Loading branch information
pahanini committed Jan 26, 2015
1 parent bcf60fc commit 1bdf2c2
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions ConsoleTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,59 @@

namespace pahanini\log;

use Yii;
use yii\helpers\Console;
use yii\log\Logger;
use yii\log\Target;

/**
* ConsoleTarget writes log to console (useful for debugging console applications)
*
* @author pahanini <[email protected]>
*/
class ConsoleTarget extends \yii\log\Target
class ConsoleTarget extends Target
{
/**
* Maximum length of string
* @var int
*/
public $maxLength = 100;
/**
* @var bool If true context message will be added to the end of output
*/
public $enableContextMassage = false;

/**
* @inheritdoc
* @return string
*/
protected function getContextMessage()
{
return $this->enableContextMassage ? parent::getContextMessage() : '';
}

/**
* Sends log messages to console.
*/
public function export()
{
foreach ($this->messages as $message) {
echo $this->formatMessage($message) . "\n";
}
}
/**
* @inheritdoc
*/
public function export()
{
foreach ($this->messages as $message) {
echo $this->formatMessage($message) . PHP_EOL;
}
}

/**
* @inheritdoc
*/
public function formatMessage($message)
{
list($text, $level, $category, $timestamp) = $message;
$level = \yii\log\Logger::getLevelName($level);
if (!is_string($text)) {
$text = var_export($text, true);
}
if ($this->maxLength && ($length = strlen($text)) > $this->maxLength) {
$text = substr(str_replace("\n", " ", $text), 0, $this->maxLength);
}
return "[$level][$category] $text";
}
/**
* @inheritdoc
*/
public function formatMessage($message)
{
$text = $message[0];
$level = Logger::getLevelName($message[1]);
if (!is_string($text)) {
$text = '(not string)';
}
$level = "[$level]";
if (Console::streamSupportsAnsiColors(\STDOUT)) {
if ($level == '[error]') {
$level = Console::ansiFormat($level, [Console::BG_RED]);
}
$level = Console::ansiFormat($level, [Console::BOLD]);
}
return "$level\t$text";
}
}

0 comments on commit 1bdf2c2

Please sign in to comment.