Created
June 29, 2012 19:32
-
-
Save cebe/3020140 to your computer and use it in GitHub Desktop.
Yii Logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // This is http://pear.php.net/package/Console_Color | |
| Yii::import('application.vendors.Console_Color.Console_Color'); | |
| /** | |
| * Logroute that simply echos what has been logged | |
| * | |
| * This files is available online at https://gist.github.com/3020140 | |
| * | |
| * you can use it on console to have logs displayed while running a command | |
| * Need force logger to dump messages when one comes in: | |
| * Yii::getLogger()->autoFlush = 1; | |
| * Yii::getLogger()->autoDump = true; | |
| * | |
| * @author Carsten Brandt <[email protected]> | |
| */ | |
| class EchoLogRoute extends CLogRoute | |
| { | |
| /** | |
| * @var bool whether to strip the stack trace from every message | |
| */ | |
| public $stripStacktrace = true; | |
| /** | |
| * @var string format of the printed message line. | |
| * The following tags will be replaced: | |
| * - {timestamp} | |
| * - {level} | |
| * - {category} | |
| * - {message} | |
| */ | |
| public $messageFormat = "{timestamp} [{level}] [{category}] {message}"; | |
| /** | |
| * @var bool whether to color output with Console_Color | |
| */ | |
| public $colored = true; | |
| /** | |
| * @var array array defining colorcode of Console_Color for each log level | |
| */ | |
| public $levelColors = array( | |
| 'error' => '%R', | |
| 'warning' => '%Y', | |
| 'info' => '%W', | |
| 'trace' => '%c', | |
| 'profile' => '%g', | |
| ); | |
| public function init() | |
| { | |
| // prepare logger to dump logs every time one comes in | |
| Yii::getLogger()->autoFlush = 1; | |
| Yii::getLogger()->autoDump = true; | |
| } | |
| /** | |
| * 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 $log) { | |
| list($message, $stacktrace) = $this->splitMessage($log[0]); | |
| $message = $this->formatLogMessage($message, $log[1], $log[2], $log[3]); | |
| echo $this->stripStacktrace ? $message : $message.$stacktrace; | |
| echo "\n"; | |
| } | |
| } | |
| /** | |
| * Formats a log message given different fields. | |
| * @param string $message message content | |
| * @param integer $level message level | |
| * @param string $category message category | |
| * @param integer $time timestamp | |
| * @return string formatted message | |
| */ | |
| protected function formatLogMessage($message,$level,$category,$time) | |
| { | |
| $message = strtr($this->messageFormat, array( | |
| '{timestamp}' => @date('Y/m/d H:i:s',$time), | |
| '{level}' => $level, | |
| '{category}' => $category, | |
| '{message}' => $message, | |
| )); | |
| if ($this->colored && isset($this->levelColors[$level])) { | |
| $c = $this->levelColors[$level]; | |
| $message = Console_Color::convert($c . $message . ((ucfirst($c[1])==$c[1]) ? '%N' : '%n')); | |
| } | |
| return $message; | |
| } | |
| /** | |
| * split a message into msg and stacktrace | |
| */ | |
| protected function splitMessage($message) | |
| { | |
| $msg = trim(preg_replace('/^in .* \([0-9]+\)$/m', '', $message)); | |
| $stacktrace = substr($message, strlen($msg)); | |
| return array($msg, $stacktrace); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Will filter log messages by pattern | |
| * | |
| * This files is available online at https://gist.github.com/3020140 | |
| * | |
| * @author Carsten Brandt <[email protected]> | |
| */ | |
| class ExpressionLogFilter extends CComponent implements ILogFilter | |
| { | |
| /** | |
| * @var string keep messages that match pattern | |
| * This will be passed to preg_match(). | |
| */ | |
| public $pattern; | |
| /** | |
| * if true, will keep messages that do not match | |
| */ | |
| public $inverted=false; | |
| /** | |
| * Filters the given log messages. | |
| * @param array $logs the log messages | |
| * @throws CException if pattern is not set | |
| */ | |
| public function filter(&$logs) | |
| { | |
| if ($this->pattern === null) { | |
| throw new CException('pattern property of ExpressionLogFilter is not set!'); | |
| } | |
| foreach($logs as $k => $message) { | |
| list($msg, $level, $category, $time) = $message; | |
| if (preg_match($this->pattern, $msg)) | |
| { | |
| if ($this->inverted) | |
| unset($logs[$k]); | |
| } | |
| else | |
| { | |
| if (!$this->inverted) | |
| unset($logs[$k]); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment