Permalink
Browse files

Merge pull request #1 from digibart/digibart

* remember the selected log level
* move files to root, just like the official KO modules
* and a few bug fixes
Thanks a lot to @digibart and @jacksongomesbr
  • Loading branch information...
2 parents 09846dd + 4ac056a commit 35f9d7b3c37c209311c08abf7ffdf74191b7e529 @ajaxray committed Jan 22, 2012
View
@@ -1,6 +1,10 @@
# Kohana 3.x Log Viewer module
## A Kohana module for exploring log files
+### Disclaimer
+
+This module forks original one from https://github.com/ajaxray adding suport for Kohana's 3.2 log file format, which includes stack trace
+
### Installation:
1. Download this module and add the **logviewer** folder it to your `MODPATH`
@@ -29,3 +33,4 @@ It's completely self explanatory. Here are some points for quick refs -
- If you've a suggestion or found a bug, please let me know at - anisniit(at)gmail.com
- BE CAREFUL ABOUT USING ON PRODUCTION!
+
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -16,7 +16,7 @@ class Controller_Logs extends Controller {
function before()
{
$this->layout = new View('logs/layout');
- $this->_logDir = APPPATH . 'logs';
+ $this->_logDir = Kohana::$config->load('logviewer.log_path');
$today = getdate();
$this->_year = $this->request->param('year', $today['year'] );
@@ -28,6 +28,10 @@ function before()
public function action_index()
{
//echo "$this->_year/$this->_month/$this->_day/$this->_level";
+
+ if (!$this->request->query('mode'))
+ $this->request->redirect($this->request->uri().'?mode=raw');
+
if($this->_getMonths()){
$this->_setLayoutVars();
$this->layout->set('content', $this->_getLogReport($this->_level));
@@ -77,7 +81,7 @@ private function _getMonths()
$yearMonths = array_slice($yearMonths, 2);
array_walk($yearMonths, function(&$m, $k, $y)
{
- $m = $y . DIRECTORY_SEPARATOR . $m;
+ $m = (substr($m,0,1) != ".") ? $y . DIRECTORY_SEPARATOR . $m : '';
}, $year);
$months = array_merge($months, $yearMonths);
@@ -89,7 +93,7 @@ private function _getMonths()
private function _getDays()
{
$days = @scandir($this->_logDir . "/{$this->_year}/{$this->_month}");
- if(empty($days)) return false;
+ if(empty($days)) return array();
return array_slice($days, 2); // remove . and ..
}
@@ -103,7 +107,7 @@ private function _getLogReport($level = null)
if($level) {
foreach($logsEntries as $k => $entry){
- if($entry['level'] != $level) unset($logsEntries[$k]);
+ if(Arr::get($entry, 'level') != $level) unset($logsEntries[$k]);
}
}
@@ -36,22 +36,44 @@ public function getLogsEntries($level = null){
protected function _createLogEntries()
{
$pattern = "/(.*) --- ([A-Z]*): ([^:]*):? ([^~]*)~? (.*)/";
-
+ $last_log = null;
+ $message = '';
+ $start_trace = false;
+ $i = 0;
foreach($this->_rawContent as $logRaw) {
- preg_match($pattern, $logRaw, $matches);
+ $logRaw = trim($logRaw);
+ if ($logRaw != '--' && $logRaw[0] != '#' && stripos($logRaw, 'STRACE') === FALSE) {
+ preg_match($pattern, $logRaw, $matches);
- $log = array();
- $log['raw'] = $logRaw;
- if($matches) {
- $log['time'] = strtotime($matches[1]);
- $log['level'] = $matches[2]; // Notice, Error etc.
- $log['style'] = $this->_getStyle($matches[2]); // CSS class for styling
- $log['type'] = $matches[3]; // Exception name
- $log['message'] = $matches[4];
- $log['file'] = $matches[5];
- }
+ $log = array();
+ $log['raw'] = $logRaw;
+ if($matches) {
+ $log['time'] = strtotime($matches[1]);
+ $log['level'] = $matches[2]; // Notice, Error etc.
+ $log['style'] = $this->_getStyle($matches[2]); // CSS class for styling
+ $log['type'] = $matches[3]; // Exception name
+ $log['message'] = $matches[4];
+ $log['file'] = $matches[5];
+ }
- $this->_logEntries[] = $log;
+ $this->_logEntries[] = $log;
+ $last_log = $i;
+ $i++;
+ }
+
+ if (stripos($logRaw, 'STRACE') !== FALSE) {
+ $message = Arr::get($this->_logEntries[$last_log], 'message');
+ $this->_logEntries[$last_log]['message'] = $message . '<br/><br/><p>Stack Trace:</p><ol style="font-family:consolas;font-size:8pt">';
+ }
+
+ if ($logRaw[0] == '#') {
+ $logRaw = preg_replace('/#\d /', '', $logRaw);
+ $this->_logEntries[$last_log]['message'] .= '<li>'.$logRaw . '</li>';
+ }
+
+ if (preg_match('/\{main\}/', $logRaw)) {
+ $this->_logEntries[$last_log]['message'] .= '</ol>';
+ }
}
}
@@ -77,7 +99,4 @@ private function _getStyle($level)
}
}
-}
-
-
-
+}
@@ -0,0 +1,5 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+return array(
+ 'log_path' => APPPATH . 'logs',
+);
File renamed without changes.
@@ -1,9 +0,0 @@
-<ul class="pills">
- <?php foreach($days as $day): ?>
- <li class="<?php if($active_report == $day) echo "active" ?>">
- <a href="<?php echo URL::site("logs/$active_month/" . substr($day, 0, 2)) ?>"><?php echo $day ?></a>
- </li>
- <?php endforeach;?>
-</ul>
-
-
@@ -0,0 +1,10 @@
+<?php
+ $mode = isset($_GET['mode']) ? $_GET['mode'] : 'raw';
+?>
+<ul class="pills">
+ <?php foreach($days as $day): ?>
+ <li class="<?php if($active_report == $day) echo "active" ?>">
+ <?php echo HTML::anchor("logs/$active_month/" . substr($day, 0, 2) . "/$log_level?mode=$mode", $day); ?>
+ </li>
+ <?php endforeach;?>
+</ul>
File renamed without changes.
@@ -1,7 +1,10 @@
+<?php
+ $mode = isset($_GET['mode']) ? $_GET['mode'] : 'raw';
+?>
<ul class="pills">
<?php foreach($months as $month): ?>
<li class="<?php if($active_month == $month) echo "active" ?>">
- <a href="<?php echo URL::site("logs/$month") ?>"><?php echo $month ?></a>
+ <?php echo HTML::anchor("logs/$month/01/$log_level?mode=$mode", $month); ?></a>
</li>
<?php endforeach;?>
</ul>
@@ -1,12 +1,16 @@
- <h2><?php echo $header ?></h2>
+<?php
+ $mode = isset($_GET['mode']) ? $_GET['mode'] : 'raw';
+?>
+
+<h2><?php echo $header ?></h2>
<p>&nbsp;</p>
<div class="row filter-form">
<form name="mapping-filter" action="" class="pull-right">
Level
- <select class="input-small" name="levels" onchange="location='<?php echo URL::site("logs/$active_month/$active_day")?>/' + options[selectedIndex].value">
+ <select class="input-small" name="levels" onchange="location='<?php echo URL::site("logs/$active_month/$active_day")?>/' + options[selectedIndex].value + '/?mode=<?php echo $mode ?>'">
<option value="">--All--</option>
<?php
foreach (Model_Logreport::$levels as $level):
@@ -16,7 +20,7 @@
?>
</select>&nbsp;
- <?php if(isset($_GET['mode']) and $_GET['mode'] == 'raw'): ?>
+ <?php if($mode == 'raw'): ?>
<a class="btn success" href="?mode=formatted">formatted mode</a>
<?php else: ?>
<a class="btn info" href="?mode=raw">raw mode</a>
@@ -25,7 +29,7 @@
</form>
</div>
<table class="zebra-striped" width="100%">
- <?php if(isset($_GET['mode']) and $_GET['mode'] != 'raw'): ?>
+ <?php if($mode != 'raw'): ?>
<thead>
<tr>
<th width="5%">Level</th>
@@ -39,21 +43,21 @@
<tbody>
<?php foreach ($logs as $log):?>
<tr>
- <?php if(isset($_GET['mode']) and $_GET['mode'] != 'raw'): ?>
+ <?php if($mode != 'raw'): ?>
<td rowspan="2">
- <span class="label <?php echo $log['style'] ?>"> <?php echo $log['level'] ?> </span>
+ <span class="label <?php echo Arr::get($log,'style') ?>"> <?php echo Arr::get($log,'level') ?> </span>
</td>
- <td><?php echo date('H:i:s', $log['time']) ?></td>
- <td><?php echo $log['type'] ?></td>
- <td><?php echo $log['file'] ?></td>
+ <td><?php echo date('H:i:s', Arr::get($log,'time')) ?></td>
+ <td><?php echo Arr::get($log,'type') ?></td>
+ <td><?php echo Arr::get($log,'file') ?></td>
</tr>
- <tr><td colspan="4"><b>Message: </b><?php echo $log['message'] ?></td></tr>
+ <tr><td colspan="4"><b>Message: </b><?php echo Arr::get($log,'message') ?></td></tr>
<?php else: // Raw mode ?>
<tr>
<td>
- <span class="label <?php echo $log['style'] ?>"> <?php echo $log['level'] ?></span> &nbsp;
- <?php echo $log['raw'] ?>
+ <span class="label <?php echo Arr::get($log,'style') ?>"> <?php echo Arr::get($log,'level') ?></span> &nbsp;
+ <?php echo Arr::get($log,'raw') ?>
</td>
</tr>
<?php endif; ?>

0 comments on commit 35f9d7b

Please sign in to comment.