Skip to content

Commit

Permalink
Add ability to define fields to censor (#9)
Browse files Browse the repository at this point in the history
* Add ability to censor specific fields
* Mention censored_fields config option
  • Loading branch information
MaxGiting authored and Cherry-Pie committed May 30, 2018
1 parent 9431fe7 commit 0a33e94
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function report(Exception $e)

Change config ```yaro.log-envelope.php``` for your needs. You can choose to log your errors to your database or send them to your email/telegram/slack. Emails are preferable, cuz they contains more debug information, such as traceback.

There is a ```censored_fields``` option which will change any fields value to `*****` if it is named in this array. For example by default it will change values for fields called `password` to `*****`.

Also there is ```force_config``` option, where you can define which configs to override for LogEnvelope execution. E.g., if you using some smtp for mail sending and queue it, you can change configs to send LogEnvelope emails immediately and not via smtp:
```
'force_config' => [
Expand Down
7 changes: 7 additions & 0 deletions config/log-envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@
'Symfony\Component\Process\Exception\ProcessTimedOutException',
],

/*
* List of fields to censor
*/
'censored_fields' => [
'password',
],

];
24 changes: 20 additions & 4 deletions src/LogEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

class LogEnvelope
{

private $config = [];
private $cachedConfig = [];

public function __construct()
{
$this->config['censored_fields'] = config('yaro.log-envelope.censored_fields', ['password']);
$this->config['except'] = config('yaro.log-envelope.except', []);
$this->config['count'] = config('yaro.log-envelope.lines_count', 6);
$this->config['drivers'] = config('yaro.log-envelope.drivers', []);
Expand All @@ -25,7 +25,7 @@ public function __construct()
public function send($exception)
{
$this->onBefore();

try {
$data = $this->getExceptionData($exception);

Expand Down Expand Up @@ -94,8 +94,12 @@ private function getExceptionData($exception)
'HEADERS' => Request::header(),
);

// Remove empty, false and null values
$data['storage'] = array_filter($data['storage']);

// Censor sensitive field values
array_walk_recursive($data['storage'], 'self::censorSensitiveFields');

$count = $this->config['count'];

$data['exegutor'] = [];
Expand All @@ -122,6 +126,20 @@ private function getExceptionData($exception)
return $data;
} // end getExceptionData

/**
* Set the value of specified fields to *****
*
* @param string $value
* @param string $key
* @return void
*/
public function censorSensitiveFields(&$value, $key)
{
if (in_array($key, $this->config['censored_fields'], true)) {
$value = '*****';
}
}

/**
* @param SplFileObject $file
*/
Expand All @@ -144,6 +162,4 @@ private function getLineInfo($file, $line, $i)
]
];
} // end getLineInfo

}

0 comments on commit 0a33e94

Please sign in to comment.