Skip to content

Commit d62112c

Browse files
author
Tim Helfensdörfer
committed
Added context encryption
1 parent 837089c commit d62112c

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ Add the following lines in your `config/logging.php`:
2121
'level' => env('LOG_LEVEL', 'debug'), // Optional
2222
'connection' => env('LOG_DATABASE', 'mysql'), // Optional
2323
'table' => env('LOG_TABLE', 'logs'), // Optional
24+
'encrypt' => env('LOG_ENCRYPT', false), // Optional, Encrypt the context part of the log message before inserting it into the database
2425
],
2526
```
2627

2728
## Change log
2829

30+
### 1.2.2
31+
32+
* FEATURE: Added context encryption
33+
2934
### 1.2.1
3035

3136
* FEATURE: Usage of custom database connections or tables

src/DatabaseHandler.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class DatabaseHandler extends AbstractProcessingHandler
2222
*/
2323
protected $table;
2424

25+
/**
26+
* Encrypt the context in database.
27+
*
28+
* @var boolean
29+
*/
30+
protected $encryption;
31+
2532
/**
2633
* Set the database connection name.
2734
*
@@ -42,6 +49,16 @@ public function setTable(string $table): void
4249
$this->table = $table;
4350
}
4451

52+
/**
53+
* If the context should be encrypted for the database.
54+
*
55+
* @param bool $encryption
56+
*/
57+
public function setEncryption(bool $encryption): void
58+
{
59+
$this->encryption = $encryption;
60+
}
61+
4562
/**
4663
* {@inheritdoc}
4764
*/
@@ -51,7 +68,11 @@ protected function write(array $record)
5168
$context = 'Closure';
5269
} else {
5370
try {
54-
$context = serialize($record['context']);
71+
if ($this->encryption) {
72+
$context = encrypt($record['context']);
73+
} else {
74+
$context = serialize($record['context']);
75+
}
5576
} catch (\Exception $e) {
5677
$context = null;
5778
}

src/DatabaseLogger.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __invoke(array $config)
1919
$handler = new DatabaseHandler($config['level'] ?? 'debug');
2020
$handler->setConnection($config['connection'] ?? 'mysql');
2121
$handler->setTable($config['table'] ?? 'logs');
22+
$handler->setEncryption($config['encrypt'] ?? false);
2223

2324
$logger->pushHandler($handler);
2425

0 commit comments

Comments
 (0)