Skip to content

Commit 0b8f44b

Browse files
authored
Merge pull request #26 from azorpax/add-logger-exception-by-uri-pattern
Allow to exclude some uri patterns
2 parents 4a21ec3 + 842cd75 commit 0b8f44b

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ LARAVEL_LOGGER_ROLES_ENABLED=true
120120
LARAVEL_LOGGER_ROLES_MIDDLWARE=role:admin
121121
LARAVEL_LOGGER_ROLE_MODEL=jeremykenedy\LaravelRoles\Models\Role
122122
LARAVEL_LOGGER_MIDDLEWARE_ENABLED=true
123-
LARAVEL_LOGGER_USER_MODEL=App\Models\User
123+
LARAVEL_LOGGER_MIDDLEWARE_EXCEPT=
124+
LARAVEL_LOGGER_USER_MODEL=App\User
124125
LARAVEL_LOGGER_PAGINATION_ENABLED=true
125126
LARAVEL_LOGGER_PAGINATION_PER_PAGE=25
126127
LARAVEL_LOGGER_DATATABLES_ENABLED=true

src/app/Http/Middleware/LogActivity.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,31 @@ class LogActivity
2020
*/
2121
public function handle($request, Closure $next, $description = null)
2222
{
23-
if (config('LaravelLogger.loggerMiddlewareEnabled')) {
23+
if (config('LaravelLogger.loggerMiddlewareEnabled') && $this->shouldLog($request)) {
2424
ActivityLogger::activity($description);
2525
}
2626

2727
return $next($request);
2828
}
29+
30+
/**
31+
* Determine if the request has a URI that should log.
32+
*
33+
* @param \Illuminate\Http\Request $request
34+
* @return bool
35+
*/
36+
protected function shouldLog($request)
37+
{
38+
foreach (config('LaravelLogger.loggerMiddlewareExcept') as $except) {
39+
if ($except !== '/') {
40+
$except = trim($except, '/');
41+
}
42+
43+
if ($request->is($except)) {
44+
return false;
45+
}
46+
}
47+
48+
return true;
49+
}
2950
}

src/config/laravel-logger.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828

2929
'loggerMiddlewareEnabled' => env('LARAVEL_LOGGER_MIDDLEWARE_ENABLED', true),
30+
'loggerMiddlewareExcept' => array_filter(explode(",", trim(env('LARAVEL_LOGGER_MIDDLEWARE_EXCEPT')))),
3031

3132
/*
3233
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)