Skip to content

Commit 011481e

Browse files
authored
Merge pull request #1 from cherry-framework/develop
Develop
2 parents f85ded0 + c5100f7 commit 011481e

File tree

10 files changed

+251
-104
lines changed

10 files changed

+251
-104
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Cherry-Log-Viewer Changelog
2+
3+
## [v1.0.0](https://github.com/cherry-framework/logs-viewer/releases/tag/v1.0.0 "v1.0.0") (2019-03-31)
4+
#### The first stable version
5+
6+
- Package available on: `composer require cherry-project/logs-viewer`
7+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Temuri Takalandze
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# cherry-Log-Viewer
2+
The Cherry-project Logs Viewer
3+
4+
[![GitHub license](https://img.shields.io/github/license/cherry-framework/logs-viewer.svg)](https://github.com/cherry-framework/logs-viewer/blob/master/LICENSE)
5+
6+
[![GitHub release](https://img.shields.io/github/release/cherry-framework/logs-viewer.svg)](https://github.com/cherry-framework/logs-viewer/releases)
7+
8+
[![Packagist Version](https://img.shields.io/packagist/v/cherry-project/logs-viewer.svg "Packagist Version")](https://packagist.org/packages/cherry-project/logs-viewer "Packagist Version")
9+
10+
------------
11+
12+
## Including
13+
**Install from composer** `composer require cherry-project/logs-viewer`
14+
15+
**Include Autoloader in your main file** (Ex.: index.php)
16+
```php
17+
require_once __DIR__ . '/vendor/autoload.php';
18+
```
19+
20+
## Usage
21+
22+
**Logs Viewer** works only with [Cherry Logger](https://github.com/cherry-framework/logger) logs!
23+
24+
Import class
25+
26+
```php
27+
use Cherry\Log\LogViewer;
28+
```
29+
30+
Define path to your logs
31+
```php
32+
define('LOGS_PATH', __DIR__ . '/var/logs');
33+
```
34+
35+
**Note**: If you use [Cherry Core](https://github.com/cherry-framework/core)
36+
or [Cherry Framework](https://github.com/cherry-framework/framework), path must defined in config file:
37+
```json
38+
{
39+
"LOGS_PATH": "var/logs"
40+
}
41+
```
42+
43+
Crete class new object
44+
45+
```php
46+
$viewer = new LogViewer();
47+
```
48+
49+
Finally you need to render the Log Viewer view:
50+
```php
51+
$viewer->render();
52+
```
53+
54+
**2019 © Cherry-project**

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cherry-project/logs-viewer",
33
"description": "Cherry-project Logs Viewer.",
44
"type": "library",
5-
"keywords": ["cherry","logs","logs=viewer"],
5+
"keywords": ["cherry","logs","logs-viewer"],
66
"homepage": "https://github.com/",
77
"license": "MIT",
88
"authors": [

examples/index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
22

3+
use Cherry\Log\LogViewer;
4+
35
require_once __DIR__ . "/../vendor/autoload.php";
46

57
define('LOGS_PATH', __DIR__ . '/var/logs');
68

7-
$lv = new \Cherry\Log\LogViewer();
9+
$viewer = new LogViewer();
10+
11+
$viewer->render();

examples/var/logs/kernel.log

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,3 @@
22
[2019-03-30 19:36:47] WARNING: Warning Message In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 12.
33
[2019-03-30 19:36:47] ERROR: Error Message In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 13.
44
[2019-03-30 19:36:47] DEBUG: Debug Message In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 14.
5-
[2019-03-30 19:36:47] INFO: Info Message 2 In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 16.
6-
[2019-03-30 19:36:47] WARNING: Warning Message 2 In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 17.
7-
[2019-03-30 19:36:47] ERROR: Error Message 2 In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 18.
8-
[2019-03-30 19:36:47] DEBUG: Debug Message 2 In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 19.

examples/var/logs/kernel2.log

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
[2019-03-30 19:36:47] INFO: Info Message In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 11.
2-
[2019-03-30 19:36:47] WARNING: Warning Message In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 12.
3-
[2019-03-30 19:36:47] ERROR: Error Message In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 13.
4-
[2019-03-30 19:36:47] DEBUG: Debug Message In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 14.
52
[2019-03-30 19:36:47] INFO: Info Message 2 In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 16.
63
[2019-03-30 19:36:47] WARNING: Warning Message 2 In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 17.
74
[2019-03-30 19:36:47] ERROR: Error Message 2 In /home/abgeo/Programming/MyProjects/PHP/Projects/Cherry-project/Logger/examples/test.php On line 18.

src/Log/LogViewer.php

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,66 @@
11
<?php
2+
/**
3+
* The file contains LogViewer class
4+
*
5+
* PHP version 5
6+
*
7+
* @category Library
8+
* @package Cherry
9+
* @author Temuri Takalandze <[email protected]>
10+
* @license https://github.com/cherry-framework/logs-viewer/blob/master/LICENSE MIT
11+
* @link https://github.com/cherry-framework/logs-viewer
12+
*/
213

314
namespace Cherry\Log;
415

16+
/**
17+
* Cherry project Log Viewer class
18+
*
19+
* @category Library
20+
* @package Cherry
21+
* @author Temuri Takalandze <[email protected]>
22+
* @license https://github.com/cherry-framework/logs-viewer/blob/master/LICENSE MIT
23+
* @link https://github.com/cherry-framework/logs-viewer
24+
*/
525
class LogViewer
626
{
7-
private $logsPath;
8-
private $logsFiles;
27+
private $_logsPath;
928

29+
/**
30+
* LogViewer constructor.
31+
*/
1032
public function __construct()
1133
{
12-
$this->logsPath = LOGS_PATH;
34+
$this->_logsPath = LOGS_PATH;
35+
}
1336

37+
/**
38+
* Render Logs Viewer view.
39+
*
40+
* @return void
41+
*/
42+
public function render()
43+
{
1444
$logFiles = $this->_getLogFiles();
1545

16-
$this->logsFiles = $logFiles;
17-
18-
$logs = $this->_readLogs($this->logsPath . '/' . $logFiles[0]);
46+
$allLogs = [];
47+
foreach ($logFiles as $logsFile) {
48+
$allLogs[] = $this->_readLogs($this->_logsPath . '/' . $logsFile);
49+
}
1950

20-
require_once __DIR__ . '/../templates/logs.php';
51+
include_once __DIR__ . '/../view/logs.php';
2152
}
2253

54+
/**
55+
* Get log files from _logsPath.
56+
*
57+
* @return array Logs filenames
58+
*/
2359
private function _getLogFiles()
2460
{
2561
$logFiles = [];
2662

27-
if ($handle = opendir($this->logsPath)) {
63+
if ($handle = opendir($this->_logsPath)) {
2864
while (false !== ($entry = readdir($handle))) {
2965
if ($entry != "." && $entry != "..") {
3066
$logFiles[] = $entry;
@@ -34,32 +70,45 @@ private function _getLogFiles()
3470
closedir($handle);
3571
}
3672

73+
//Sort and reindex array
3774
natsort($logFiles);
75+
$logFiles = array_values($logFiles);
3876

3977
return $logFiles;
4078
}
4179

42-
private function _readLogs(string $logsFile)
80+
/**
81+
* Read logs from given file.
82+
*
83+
* @param string $logsFile Logs file
84+
*
85+
* @return array Logs from file
86+
*/
87+
private function _readLogs($logsFile)
4388
{
4489
$logs = [];
4590

46-
$handle = fopen($logsFile,"r");
91+
$handle = fopen($logsFile, "r");
4792

48-
while(!feof($handle)) {
93+
while (!feof($handle)) {
4994
$line = fgets($handle);
5095

5196
if ($line != '') {
52-
5397
$dateTimeStart = strpos($line, '[');
5498
$dateTimeEnd = strpos($line, ']', $dateTimeStart);
5599
$levelEnd = strpos($line, ':', $dateTimeEnd);
56100

57-
$log = [
58-
"dateTime" => substr($line, $dateTimeStart + 1, $dateTimeEnd - $dateTimeStart - 1),
59-
"level" => substr($line, $dateTimeEnd + 3, $levelEnd - $dateTimeEnd - 3),
101+
$logs[] = [
102+
"dateTime" => substr(
103+
$line, $dateTimeStart + 1,
104+
$dateTimeEnd - $dateTimeStart - 1
105+
),
106+
"level" => substr(
107+
$line, $dateTimeEnd + 3,
108+
$levelEnd - $dateTimeEnd - 3
109+
),
60110
"message" => substr($line, $levelEnd + 2)
61111
];
62-
$logs[] = $log;
63112
}
64113
}
65114

src/templates/logs.php

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)