Skip to content

Instantly share code, notes, and snippets.

@Ekstazi
Last active August 29, 2015 13:59
Show Gist options
  • Save Ekstazi/10743086 to your computer and use it in GitHub Desktop.
Save Ekstazi/10743086 to your computer and use it in GitHub Desktop.
yii console log route
<?
/**
* Class ConsoleLogRoute
* Console log route. Output log messages into console
* @package app\components\console
*/
class ConsoleLogRoute extends \CLogRoute {
const LEVEL_CONSOLE='console';
/**
* @var string
*/
public $charset;
/**
* @var string
*/
public $levels=self::LEVEL_CONSOLE;
/**
* @var string
*/
public $template="[{time}] [{category}] [{level}] {message}\n";
public function __construct()
{
if(!isset($this->charset))
$this->charset=\Yii::app()->charset;
}
/**
* Processes log messages and sends them to specific destination.
* Derived child classes must implement this method.
* @param array $logs list of messages. Each array element represents one message
* with the following structure:
* array(
* [0] => message (string)
* [1] => level (string)
* [2] => category (string)
* [3] => timestamp (float, obtained by microtime(true));
*/
protected function processLogs($logs)
{
foreach($logs as $msg)
echo $this->formatLogMessage($msg[0],$msg[1],$msg[2],$msg[3]);
}
/**
* @param string $message
* @param int $level
* @param string $category
* @param int $time
* @return string
*/
protected function formatLogMessage($message, $level, $category, $time)
{
return strtr($this->template,array(
'{time}'=>@date('d-m-Y H:i:s.',$time).sprintf('%06d',(int)(($time-(int)$time)*1000000)),
'{level}'=>$level,
'{category}'=>$category,
'{message}'=>mb_convert_encoding($message,$this->charset,\Yii::app()->charset),
));
return $time." [$category] [$level] ".mb_convert_encoding($message,$this->charset,\Yii::app()->charset)."\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment