forked from krissss/yii2-log-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
108 lines (96 loc) · 2.38 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace kriss\logReader;
use yii\base\BootstrapInterface;
use yii\base\InvalidConfigException;
use yii\web\Application;
/**
* LogReader module definition class
*
* @property Log[] $logs
* @property integer $totalCount
*/
class Module extends \yii\base\Module implements BootstrapInterface
{
/**
* @var array
*/
public $aliases = [];
/**
* @var array
*/
public $levelClasses = [
'trace' => 'label-default',
'info' => 'label-info',
'warning' => 'label-warning',
'error' => 'label-danger',
];
/**
* @var string
*/
public $defaultLevelClass = 'label-default';
/**
* @inheritdoc
*/
public function bootstrap($app)
{
if ($app instanceof Application) {
$app->getUrlManager()->addRules([
$this->id => $this->id . '/default/index',
$this->id . '/<action:\w+>/<slug:[\w-]+>' => $this->id . '/default/<action>',
$this->id . '/<action:\w+>' => $this->id . '/default/<action>',
], false);
} else {
throw new InvalidConfigException('Can use for web application only.');
}
}
/**
* @return Log[]
*/
public function getLogs()
{
$logs = [];
foreach ($this->aliases as $name => $alias) {
$logs[] = new Log($name, $alias);
}
return $logs;
}
/**
* @param string $slug
* @param null|string $stamp
* @return null|Log
*/
public function findLog($slug, $stamp)
{
foreach ($this->aliases as $name => $alias) {
if ($slug === Log::extractSlug($name)) {
return new Log($name, $alias, $stamp);
}
}
return null;
}
/**
* @param Log $log
* @return Log[]
*/
public function getHistory(Log $log)
{
$logs = [];
foreach (glob(Log::extractFileName($log->alias, '*')) as $fileName) {
$logs[] = new Log($log->name, $log->alias, Log::extractFileStamp($log->alias, $fileName));
}
return $logs;
}
/**
* @return integer
*/
public function getTotalCount()
{
$total = 0;
foreach ($this->getLogs() as $log) {
foreach ($log->getCounts() as $count) {
$total += $count;
}
}
return $total;
}
}