Skip to content

added support for laravel.log general file #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/laravel-log-reader.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

return [
'api_route_path' => 'admin/api/log-reader',
'api_route_path' => 'log/json',
'view_route_path' => 'admin/log-reader',
'admin_panel_path' => 'admin',
'middleware' => ['web', 'auth']
];
];
26 changes: 14 additions & 12 deletions src/LaravelLogReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

class LaravelLogReader
{
protected $final = [];
protected $config = [];


public function __construct($config = [])
{
if (array_key_exists('date', $config)) {
Expand All @@ -26,12 +24,12 @@ public function __construct($config = [])
public function getLogFileDates()
{
$dates = [];
$files = glob(storage_path('logs/laravel-*.log'));
$files = glob(storage_path('logs/laravel*.log'));
$files = array_reverse($files);
foreach ($files as $path) {
$fileName = basename($path);
preg_match('/(?<=laravel-)(.*)(?=.log)/', $fileName, $dtMatch);
$date = $dtMatch[0];
$date = $dtMatch[0] ?? null;
array_push($dates, $date);
}

Expand Down Expand Up @@ -65,22 +63,26 @@ public function get()

$pattern = "/^\[(?<date>.*)\]\s(?<env>\w+)\.(?<type>\w+):(?<message>.*)/m";

$fileName = 'laravel-' . $configDate . '.log';
$fileName = 'laravel' . $configDate . '.log';
$content = file_get_contents(storage_path('logs/' . $fileName));
preg_match_all($pattern, $content, $matches, PREG_SET_ORDER, 0);
// splitting by regexp in order to get the whole message between 2 log entries
$chars = preg_split('/\[(?<date>.*)\]\s(?<env>\w+)\.(?<type>\w+):/i', $content, -1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
// chunking - every chung will contain all needed data
$matches = array_chunk($chars, 4, false);

$logs = [];
foreach ($matches as $match) {
foreach ($matches as [$date, $env, $type, $message]) {
$logs[] = [
'timestamp' => $match['date'],
'env' => $match['env'],
'type' => $match['type'],
'message' => trim($match['message'])
'timestamp' => $date,
'env' => $env,
'type' => $type,
'message' => trim($message),
];
}

preg_match('/(?<=laravel-)(.*)(?=.log)/', $fileName, $dtMatch);
$date = $dtMatch[0];
$date = $dtMatch[0] ?? null;

$data = [
'available_log_dates' => $availableDates,
Expand Down
30 changes: 27 additions & 3 deletions views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
text-transform: uppercase;
}

.angular-with-newlines {
white-space: pre-wrap;
}

@media screen and (max-width: 700px) {
.top_content {
flex-direction: column;
Expand Down Expand Up @@ -311,11 +315,19 @@
</tr>
</thead>

<tr ng-repeat="log in data.logs |filter: selectedType track by $index">
<tr ng-repeat="log in data.logs | filter: {type:selectedType} track by $index">
<td>@{{ log.timestamp }}</td>
<td>@{{log.env}}</td>
<td>@{{ log.env }}</td>
<td><span class="badge @{{ log.type.toLowerCase() }}">@{{ log.type }}</span></td>
<td>@{{ log.message }}</td>
<td>@{{ log.first_line }}
<input type="button" value="Show Full Stack" ng-hide="!log.message || log.showStackTrace"
ng-click="log.showStackTrace = true"/>
<input type="button" value="Hide Full Stack" ng-show="log.message && log.showStackTrace"
ng-click="log.showStackTrace = false"/>
<div class="angular-with-newlines" ng-show="log.message && log.showStackTrace">@{{ log.message
}}
</div>
</td>
</tr>
</table>
</div>
Expand All @@ -340,6 +352,18 @@

$http.get(url)
.success(function (data) {
data.data.logs.forEach(function (el) {
el.showStackTrace = false;
var firstBreakIndex = el.message.indexOf('\n');
if (firstBreakIndex === -1) {
el.first_line = el.message;
el.message = undefined;
} else {
el.first_line = el.message.substr(0, firstBreakIndex + 1);
el.message = el.message.substr(firstBreakIndex + 1);
}
});

$scope.response = data;
$scope.data = data.data;
originalData = data.data;
Expand Down