Skip to content

Commit b889cdc

Browse files
elminsonrap2hpoutre
authored andcommitted
log location customisable #139 + fix #156
* Creating a array that content all the patterns used in the class, this allow if in the feature we want to create a new featured that include patterns we cant add this for a best readeability (some patterns get really complicated * Modifying all the sugestion from rap2hpoutre in the PR comments * Log Location customisable #139 * changing storage_path() to ->storage_path * Fixing error Cannot use object of type Rap2hpoutre\LaravelLogViewer\Pattern as array + changing array to not static + creating new function getPattern * fixing Codacy/PR Quality Review
1 parent b0a7055 commit b889cdc

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

src/Rap2hpoutre/LaravelLogViewer/LaravelLogViewer.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class LaravelLogViewer
1818
*/
1919
private $folder;
2020

21+
/**
22+
* @var string storage_path
23+
*/
24+
private $storage_path;
25+
2126
/**
2227
* Why? Uh... Sorry
2328
*/
@@ -40,14 +45,16 @@ public function __construct()
4045
{
4146
$this->level = new Level();
4247
$this->pattern = new Pattern();
48+
$this->storage_path = function_exists('config') ? config('logviewer.storage_path', storage_path('logs')) : storage_path('logs');
49+
4350
}
4451

4552
/**
4653
* @param string $folder
4754
*/
4855
public function setFolder($folder)
4956
{
50-
$logsPath = storage_path('logs') . '/' . $folder;
57+
$logsPath = $this->storage_path . '/' . $folder;
5158

5259
if (app('files')->exists($logsPath)) {
5360
$this->folder = $folder;
@@ -74,7 +81,7 @@ public function setFile($file)
7481
*/
7582
public function pathToLogFile($file)
7683
{
77-
$logsPath = storage_path('logs');
84+
$logsPath = $this->storage_path;
7885
$logsPath .= ($this->folder) ? '/' . $this->folder : '';
7986

8087
if (app('files')->exists($file)) { // try the absolute path
@@ -126,13 +133,13 @@ public function all()
126133

127134
$file = app('files')->get($this->file);
128135

129-
preg_match_all($this->pattern['logs'], $file, $headings);
136+
preg_match_all($this->pattern->getPattern('logs'), $file, $headings);
130137

131138
if (!is_array($headings)) {
132139
return $log;
133140
}
134141

135-
$log_data = preg_split($this->pattern['logs'], $file);
142+
$log_data = preg_split($this->pattern->getPattern('logs'), $file);
136143

137144
if ($log_data[0] < 1) {
138145
array_shift($log_data);
@@ -143,7 +150,7 @@ public function all()
143150
foreach ($this->level->all() as $level) {
144151
if (strpos(strtolower($h[$i]), '.' . $level) || strpos(strtolower($h[$i]), $level . ':')) {
145152

146-
preg_match($this->pattern['current_log'][0] . $level . $this->pattern['current_log'][1], $h[$i], $current);
153+
preg_match($this->pattern->getPattern('current_log',0) . $level . $this->pattern->getPattern('current_log',1), $h[$i], $current);
147154
if (!isset($current[4])) continue;
148155

149156
$log[] = array(
@@ -188,7 +195,7 @@ public function all()
188195
*/
189196
public function getFolders()
190197
{
191-
$folders = glob(storage_path() . '/logs/*', GLOB_ONLYDIR);
198+
$folders = glob(storage_path() . '/'.$this->storage_path.'/*', GLOB_ONLYDIR);
192199
if (is_array($folders)) {
193200
foreach ($folders as $k => $folder) {
194201
$folders[$k] = basename($folder);
@@ -214,7 +221,7 @@ public function getFolderFiles($basename = false)
214221
public function getFiles($basename = false, $folder = '')
215222
{
216223
$pattern = function_exists('config') ? config('logviewer.pattern', '*.log') : '*.log';
217-
$files = glob(storage_path() . '/logs/' . $folder . '/' . $pattern, preg_match($this->pattern['files'], $pattern) ? GLOB_BRACE : 0);
224+
$files = glob(storage_path() . '/'.$this->storage_path.'/' . $folder . '/' . $pattern, preg_match($this->pattern->getPattern('files'), $pattern) ? GLOB_BRACE : 0);
218225
$files = array_reverse($files);
219226
$files = array_filter($files, 'is_file');
220227
if ($basename && is_array($files)) {

src/Rap2hpoutre/LaravelLogViewer/Pattern.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Pattern
1414
/**
1515
* @var array
1616
*/
17-
private static $patterns = [
17+
private $patterns = [
1818
'logs' => '/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?\].*/',
1919
'current_log' => [
2020
'/^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}([\+-]\d{4})?)\](?:.*?(\w+)\.|.*?)',
@@ -31,4 +31,18 @@ public function all()
3131
return array_keys($this->patterns);
3232
}
3333

34+
/**
35+
* @param $pattern
36+
* @param null $position
37+
* @return string pattern
38+
*/
39+
public function getPattern($pattern, $position = null)
40+
{
41+
if ($position != null) {
42+
return $this->patterns[$pattern][$position];
43+
}
44+
return $this->patterns[$pattern];
45+
46+
}
47+
3448
}

src/config/logviewer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
return [
44
'pattern' => env('LOGVIEWER_PATTERN', '*.log'),
5+
'storage_path' => env('LOGVIEWER_STORAGE_PATH', 'logs'),
56
];

0 commit comments

Comments
 (0)