Skip to content

Commit

Permalink
Merge pull request #25 from t1sh0o/skip-secrets-logging
Browse files Browse the repository at this point in the history
Implement skipping secrets logging
  • Loading branch information
aungwinthant authored Mar 11, 2020
2 parents d7f14c7 + e3d6d15 commit 1b91ae4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ The config file is called *apilogs.php*. Currently supported drivers are *db* an

By default the logger will use *file* to log the data. But if you want to use Database for logging, migrate table by using

You can also configure which fields should not be logged like passwords, secrets, etc.

***You dont need to migrate if you are just using file driver***

```bash
Expand Down
5 changes: 4 additions & 1 deletion config/apilog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
return [
//currently supported drivers are 'db' and 'file'
"driver" => "file",
"filename" => "api-{Y-m-d}.log"
"filename" => "api-{Y-m-d}.log",
"dont_log" => [
'password', 'password_confirmation', 'new_password', 'old_password',
]
];
21 changes: 20 additions & 1 deletion src/AbstractLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function logData($request,$response){
$this->logs['created_at'] = Carbon::now();
$this->logs['method'] = $request->method();
$this->logs['url'] = $request->path();
$this->logs['payload'] = json_encode($request->all());
$this->logs['payload'] = $this->payload($request);
$this->logs['response'] = $response->status();
$this->logs['duration'] = number_format($endTime - LARAVEL_START, 3);
$this->logs['controller'] = $controller;
Expand Down Expand Up @@ -106,4 +106,23 @@ public function mapArrayToModel(array $data){
$model->ip = $data[9];
return $model;
}

/**
* Formats the request payload for logging
*
* @param $request
* @return string
*/
protected function payload($request)
{
$allFields = $request->all();

foreach (config('apilog.dont_log', []) as $key) {
if (array_key_exists($key, $allFields)) {
unset($allFields[$key]);
}
}

return json_encode($allFields);
}
}

0 comments on commit 1b91ae4

Please sign in to comment.