Skip to content

Commit 0b41de1

Browse files
committed
feat: to 2.0
1 parent f45ce31 commit 0b41de1

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed

Log.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,7 @@ public static function extractFileName($alias, $stamp = null)
182182
$fileName = FileHelper::normalizePath(Yii::getAlias($alias, false));
183183
if ($stamp === null) return $fileName;
184184

185-
$info = pathinfo($fileName);
186-
return strtr('{dir}/{name}.{stamp}.{ext}', [
187-
'{dir}' => $info['dirname'],
188-
'{name}' => $info['filename'],
189-
'{ext}' => $info['extension'],
190-
'{stamp}' => $stamp,
191-
]);
185+
return $fileName . '.' . $stamp;
192186
}
193187

194188
/**
@@ -202,11 +196,10 @@ public static function extractFileStamp($alias, $fileName)
202196
$origInfo = pathinfo($originName);
203197
$fileInfo = pathinfo($fileName);
204198
if (
205-
$origInfo['dirname'] === $fileInfo['dirname'] &&
206-
$origInfo['extension'] === $fileInfo['extension'] &&
207-
strpos($fileInfo['filename'], $origInfo['filename']) === 0
199+
$origInfo['dirname'] === $fileInfo['dirname']
200+
&& strpos($fileInfo['filename'], $origInfo['filename']) === 0
208201
) {
209-
return substr($fileInfo['filename'], strlen($origInfo['filename']) + 1);
202+
return $fileInfo['extension'];
210203
} else {
211204
return null;
212205
}

Module.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class Module extends \yii\base\Module implements BootstrapInterface
1818
* @var array
1919
*/
2020
public $aliases = [];
21+
/**
22+
* @var array
23+
*/
24+
public $extraBehaviors = [];
2125
/**
2226
* @var array
2327
*/
@@ -48,6 +52,14 @@ public function bootstrap($app)
4852
}
4953
}
5054

55+
/**
56+
* @inheritdoc
57+
*/
58+
public function behaviors()
59+
{
60+
return array_merge(parent::behaviors(), $this->extraBehaviors);
61+
}
62+
5163
/**
5264
* @return Log[]
5365
*/

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ Yii2 Log Reader
22
===============
33
Yii2 log reader
44

5-
> this project is extend from [zhuravljov/yii2-logreader](https://github.com/zhuravljov/yii2-logreader), and Add more operation like delete and so on.
5+
> this project is extend from [zhuravljov/yii2-logreader](https://github.com/zhuravljov/yii2-logreader), and Add more operation like `delete` `download` and so on.
6+
7+
> from 2.0. `history` can load file that Yii2 FileTarget rotated.
68
79
Preview
810
------------
@@ -16,13 +18,13 @@ The preferred way to install this extension is through [composer](http://getcomp
1618
Either run
1719

1820
```
19-
php composer.phar require --prefer-dist kriss/yii2-log-reader "*"
21+
php composer.phar require --prefer-dist kriss/yii2-log-reader "^2.0"
2022
```
2123

2224
or add
2325

2426
```
25-
"kriss/yii2-log-reader": "*"
27+
"kriss/yii2-log-reader": "^2.0"
2628
```
2729

2830
to the require section of your `composer.json` file.
@@ -59,4 +61,28 @@ or if you have enabled pretty URLs, you may use the following URL:
5961

6062
```php
6163
http://localhost/path/to/log-reader
64+
```
65+
66+
Advanced Usage
67+
-----
68+
69+
you can config module params `extraBehaviors` to add behaviors, like user login filter:
70+
71+
```php
72+
return [
73+
'bootstrap' => ['log-reader'],
74+
'modules' => [
75+
'log-reader' => [
76+
'class' => 'kriss\logreader\Module',
77+
'extraBehaviors' => [
78+
'login-filter' => 'xxx/action/UserLoginFilter'
79+
],
80+
'aliases' => [
81+
'Frontend Errors' => '@frontend/runtime/logs/app.log',
82+
'Backend Errors' => '@backend/runtime/logs/app.log',
83+
'Console Errors' => '@console/runtime/logs/app.log',
84+
],
85+
],
86+
],
87+
];
6288
```

controllers/DefaultController.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,27 @@ public function actionHistory($slug)
7676
]);
7777
}
7878

79-
public function actionDelete($slug, $stamp = null){
79+
public function actionDelete($slug, $stamp = null)
80+
{
8081
$log = $this->find($slug, $stamp);
81-
if(unlink($log->fileName)){
82+
if (unlink($log->fileName)) {
8283
Yii::$app->session->setFlash('success', 'delete success');
83-
}else{
84+
} else {
8485
Yii::$app->session->setFlash('error', 'delete error');
8586
}
8687
return $this->redirect(Url::previous());
8788
}
8889

90+
public function actionDownload($slug, $stamp = null)
91+
{
92+
$log = $this->find($slug, $stamp);
93+
if ($log->isExist) {
94+
Yii::$app->response->sendFile($log->fileName)->send();
95+
} else {
96+
throw new NotFoundHttpException('Log not found.');
97+
}
98+
}
99+
89100
/**
90101
* @param string $slug
91102
* @param null|string $stamp

views/default/history.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
'headerOptions' => ['class' => 'sort-numerical'],
4141
], [
4242
'class' => '\yii\grid\ActionColumn',
43-
'template' => '{view} {delete}',
43+
'template' => '{view} {delete} {download}',
4444
'urlCreator' => function ($action, Log $log) {
4545
return [$action, 'slug' => $log->slug, 'stamp' => $log->stamp];
4646
},
@@ -57,6 +57,11 @@
5757
'data' => ['method' => 'post', 'confirm' => 'Are you sure?'],
5858
]);
5959
},
60+
'download' => function ($url, Log $log) {
61+
return !$log->isExist ? '' : Html::a('Download', $url, [
62+
'class' => 'btn btn-xs btn-default',
63+
]);
64+
},
6065
],
6166
],
6267
],

views/default/index.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
'headerOptions' => ['class' => 'sort-numerical'],
4545
], [
4646
'class' => '\yii\grid\ActionColumn',
47-
'template' => '{history} {view} {archive} {delete}',
47+
'template' => '{history} {view} {archive} {delete} {download}',
4848
'urlCreator' => function ($action, Log $log) {
4949
return [$action, 'slug' => $log->slug];
5050
},
@@ -72,6 +72,11 @@
7272
'data' => ['method' => 'post', 'confirm' => 'Are you sure?'],
7373
]);
7474
},
75+
'download' => function ($url, Log $log) {
76+
return !$log->isExist ? '' : Html::a('Download', $url, [
77+
'class' => 'btn btn-xs btn-default',
78+
]);
79+
},
7580
],
7681
],
7782
],

0 commit comments

Comments
 (0)