Skip to content

Commit

Permalink
Except path added
Browse files Browse the repository at this point in the history
  • Loading branch information
alimranahmed committed Jun 10, 2020
1 parent 9cc5551 commit 99bc549
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
15 changes: 11 additions & 4 deletions config/hit-logger.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return [
/**
* Should implement the interface `\AlImranAhmed\HitLogger\LogSetting`
Expand All @@ -11,10 +12,16 @@
'log_writer' => \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' => [

],
],
];
29 changes: 29 additions & 0 deletions src/DefaultLogSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/DefaultLogWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 99bc549

Please sign in to comment.