Skip to content

Commit 837089c

Browse files
author
Tim Helfensdörfer
committed
Usage of custom database connections or tables
1 parent d8baef6 commit 837089c

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@ Add the following lines in your `config/logging.php`:
1818
'db' => [
1919
'driver' => 'custom',
2020
'via' => VisualAppeal\DatabaseLogger\DatabaseLogger::class,
21-
'level' => env('LOG_LEVEL', 'debug'),
21+
'level' => env('LOG_LEVEL', 'debug'), // Optional
22+
'connection' => env('LOG_DATABASE', 'mysql'), // Optional
23+
'table' => env('LOG_TABLE', 'logs'), // Optional
2224
],
2325
```
2426

2527
## Change log
2628

29+
### 1.2.1
30+
31+
* FEATURE: Usage of custom database connections or tables
32+
2733
### 1.2.0
2834

29-
* New logging class compatible to Laravel 5.6
35+
* FEATURE: New logging class compatible to Laravel 5.6
3036

3137
### 1.0.0
3238

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
],
2020
"require": {
21-
"php" : ">=5.6",
21+
"php" : ">=7.0",
2222
"illuminate/support": "5.6.*"
2323
},
2424
"autoload": {

src/DatabaseHandler.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,40 @@
88

99
class DatabaseHandler extends AbstractProcessingHandler
1010
{
11+
/**
12+
* Name of the database connection.
13+
*
14+
* @var string
15+
*/
16+
protected $connection;
17+
18+
/**
19+
* connection table name.
20+
*
21+
* @var string
22+
*/
23+
protected $table;
24+
25+
/**
26+
* Set the database connection name.
27+
*
28+
* @param string $connection
29+
*/
30+
public function setConnection(string $connection): void
31+
{
32+
$this->connection = $connection;
33+
}
34+
35+
/**
36+
* Set the database table name.
37+
*
38+
* @param string $table
39+
*/
40+
public function setTable(string $table): void
41+
{
42+
$this->table = $table;
43+
}
44+
1145
/**
1246
* {@inheritdoc}
1347
*/
@@ -24,7 +58,7 @@ protected function write(array $record)
2458
}
2559

2660
try {
27-
DB::table('logs')->insert([
61+
DB::connection($this->connection)->table($this->table)->insert([
2862
'message' => $record['message'],
2963
'context' => $context,
3064
'level' => $record['level'],

src/DatabaseLogger.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ class DatabaseLogger
1515
public function __invoke(array $config)
1616
{
1717
$logger = new Logger('laravel.database');
18-
$logger->pushHandler(new DatabaseHandler($config['level'] ?? 'debug'));
18+
19+
$handler = new DatabaseHandler($config['level'] ?? 'debug');
20+
$handler->setConnection($config['connection'] ?? 'mysql');
21+
$handler->setTable($config['table'] ?? 'logs');
22+
23+
$logger->pushHandler($handler);
24+
1925
return $logger;
2026
}
2127
}

0 commit comments

Comments
 (0)