diff --git a/config/hit-logger.php b/config/hit-logger.php index 6fb0d4a..d33bfed 100644 --- a/config/hit-logger.php +++ b/config/hit-logger.php @@ -1,4 +1,5 @@ \AlImranAhmed\HitLogger\DefaultLogWriter::class, /** - * Request fields that should never be logged + * Request fields or path(wildcard supported) that should never be logged */ 'except' => [ - 'password', - 'password_confirmation' - ] + 'fields' => [ + 'password', + 'password_confirmation', + ], + + 'paths' => [ + + ], + ], ]; diff --git a/src/DefaultLogSetting.php b/src/DefaultLogSetting.php index e5c198a..b8e6eb8 100644 --- a/src/DefaultLogSetting.php +++ b/src/DefaultLogSetting.php @@ -9,8 +9,37 @@ class DefaultLogSetting implements LogSetting { + protected $dontLogPaths; + + public function __construct() + { + $this->dontLogPaths = config('hit-logger.except.paths'); + } + public function shouldLog(Request $request): bool { + if (empty($this->dontLogPaths)) { + return true; + } + + foreach ($this->dontLogPaths as $dontLogPath) { + if ($this->isMatched($dontLogPath, $request->path())) { + return false; + } + } return true; } + + protected function isMatched(string $pattern, string $value): bool + { + if ($pattern == $value) { + return true; + } + + $pattern = preg_quote($pattern, '#'); + + $pattern = str_replace('\*', '.*', $pattern) . '\z'; + + return (bool)preg_match('#^' . $pattern . '#', $value); + } } diff --git a/src/DefaultLogWriter.php b/src/DefaultLogWriter.php index 54ebf09..ddc269b 100644 --- a/src/DefaultLogWriter.php +++ b/src/DefaultLogWriter.php @@ -20,7 +20,7 @@ public function logRequest(Request $request): void $headers = "- Header: ".json_encode($request->header()); - $body = "- Body: ".json_encode($request->except(config('hit-logger.except'))); + $body = "- Body: ".json_encode($request->except(config('hit-logger.except.fields'))); $files = array_map(function (UploadedFile $file) { return $file->getClientOriginalName();