Skip to content

Commit

Permalink
Added context encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
thelfensdrfer committed Mar 27, 2018
1 parent 837089c commit d62112c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ Add the following lines in your `config/logging.php`:
'level' => env('LOG_LEVEL', 'debug'), // Optional
'connection' => env('LOG_DATABASE', 'mysql'), // Optional
'table' => env('LOG_TABLE', 'logs'), // Optional
'encrypt' => env('LOG_ENCRYPT', false), // Optional, Encrypt the context part of the log message before inserting it into the database
],
```

## Change log

### 1.2.2

* FEATURE: Added context encryption

### 1.2.1

* FEATURE: Usage of custom database connections or tables
Expand Down
23 changes: 22 additions & 1 deletion src/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class DatabaseHandler extends AbstractProcessingHandler
*/
protected $table;

/**
* Encrypt the context in database.
*
* @var boolean
*/
protected $encryption;

/**
* Set the database connection name.
*
Expand All @@ -42,6 +49,16 @@ public function setTable(string $table): void
$this->table = $table;
}

/**
* If the context should be encrypted for the database.
*
* @param bool $encryption
*/
public function setEncryption(bool $encryption): void
{
$this->encryption = $encryption;
}

/**
* {@inheritdoc}
*/
Expand All @@ -51,7 +68,11 @@ protected function write(array $record)
$context = 'Closure';
} else {
try {
$context = serialize($record['context']);
if ($this->encryption) {
$context = encrypt($record['context']);
} else {
$context = serialize($record['context']);
}
} catch (\Exception $e) {
$context = null;
}
Expand Down
1 change: 1 addition & 0 deletions src/DatabaseLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __invoke(array $config)
$handler = new DatabaseHandler($config['level'] ?? 'debug');
$handler->setConnection($config['connection'] ?? 'mysql');
$handler->setTable($config['table'] ?? 'logs');
$handler->setEncryption($config['encrypt'] ?? false);

$logger->pushHandler($handler);

Expand Down

0 comments on commit d62112c

Please sign in to comment.