Skip to content

Commit 14bb8fc

Browse files
authored
Merge pull request #7 from danielme85/dev
Merge dev
2 parents 447ea85 + f67c438 commit 14bb8fc

File tree

5 files changed

+63
-58
lines changed

5 files changed

+63
-58
lines changed

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ You will need to add an array under 'channels' for Log-to-DB here like so:
2525
```php
2626
'channels' => [
2727
'stack' => [
28+
'name' => 'Log Stack',
2829
'driver' => 'stack',
2930
'channels' => ['database', 'mongodb'],
3031
],
@@ -47,7 +48,7 @@ You will need to add an array under 'channels' for Log-to-DB here like so:
4748
* driver = Required to trigger the log driver.
4849
* via = The Log handler class.
4950
* level = The minimum error level to trigger this Log Channel.
50-
* name = The channel name that will be stored with the Log event.
51+
* name = The channel name that will be stored with the Log event. Please note that if you use the stack driver the name value in the stack array is used.
5152
* connection = The DB connection from config/database.php to use (default: 'default').
5253
* collection = The DB table or collection name. (Default: log).
5354
* detailed = Store detailed log on Exceptions like stack-trace (default: true).

src/LogToDB.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ function __construct(string $channelConnection = null,
8585
if (isset($config['detailed'])) {
8686
$this->detailed = $config['detailed'];
8787
}
88-
if (isset($config['max_rows'])) {
89-
$this->maxRows = (int)$config['max_rows'];
90-
}
9188
if (isset($config['queue_db_saves'])) {
9289
$this->saveWithQueue = $config['queue_db_saves'];
9390
}
@@ -217,6 +214,11 @@ public function newFromMonolog(array $record)
217214
{
218215
if (!empty($this->connection)) {
219216
if ($this->saveWithQueue) {
217+
if (isset($record['context']['exception']) and !empty($record['context']['exception'])) {
218+
if (strpos(get_class($record['context']['exception']), "Exception") !== false) {
219+
dispatch_now(new SaveNewLogEvent($this, $record));
220+
}
221+
}
220222
if (empty($this->saveWithQueueName) and empty($this->saveWithQueueConnection)) {
221223
dispatch(new SaveNewLogEvent($this, $record));
222224
} else if (!empty($this->saveWithQueueName) and !empty($this->saveWithQueueConnection)) {
@@ -230,7 +232,7 @@ public function newFromMonolog(array $record)
230232
dispatch(new SaveNewLogEvent($this, $record))
231233
->onQueue($this->saveWithQueueName);
232234
}
233-
} else {
235+
} else {
234236
$log = CreateLogFromRecord::generate(
235237
$this->connection,
236238
$this->collection,

src/LogToDbCustomLoggingHandler.php

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,54 @@ class LogToDbCustomLoggingHandler extends AbstractProcessingHandler
1515
private $connection;
1616
private $collection;
1717
private $detailed;
18-
private $maxRows;
1918
private $saveWithQueue;
2019
private $saveWithQueueName;
2120
private $saveWithQueueConnection;
2221

2322
/**
2423
* LogToDbHandler constructor.
2524
*
26-
* @param string $connection The DB connection.
27-
* @param string $collection The Collection/Table name for DB.
28-
* @param bool $detailed Detailed error logging.
29-
* @param int $maxRows The Maximum number of rows/objects before auto purge.
30-
* @param bool $enableQueue disable|enable enqueuing log db update
31-
* @param string $queueName optional name of queue
32-
* @param string $queueConnection optional name of queue connection
33-
* @param int $level The minimum logging level at which this handler will be triggered
25+
* @param array $config Logging configuration from logging.php
26+
* @param array $processors collection of log processors
3427
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
3528
*/
36-
function __construct($connection,
37-
$collection,
38-
$detailed,
39-
$enableQueue,
40-
$queueName,
41-
$queueConnection,
42-
$level = Logger::DEBUG, bool $bubble = true)
29+
function __construct(array $config,
30+
array $processors,
31+
bool $bubble = true)
4332
{
33+
//Set default level debug
34+
$level = 'debug';
35+
4436
//Log default config if present
45-
$config = config('logtodb');
37+
$defaultConfig = config('logtodb');
38+
39+
if (!empty($defaultConfig)) {
40+
if (isset($defaultConfig['connection'])) {
41+
$this->connection = $defaultConfig['connection'];
42+
}
43+
if (isset($defaultConfig['collection'])) {
44+
$this->collection = $defaultConfig['collection'];
45+
}
46+
if (isset($defaultConfig['detailed'])) {
47+
$this->detailed = $defaultConfig['detailed'];
48+
}
49+
if (isset($defaultConfig['queue_db_saves'])) {
50+
$this->saveWithQueue = $defaultConfig['queue_db_saves'];
51+
}
52+
if (isset($defaultConfig['queue_db_name'])) {
53+
$this->saveWithQueueName = $defaultConfig['queue_db_name'];
54+
}
55+
if (isset($defaultConfig['queue_db_connection'])) {
56+
$this->saveWithQueueConnection = $defaultConfig['queue_db_connection'];
57+
}
58+
}
4659

60+
61+
//Override default config with array in logging.php
4762
if (!empty($config)) {
63+
if (isset($config['level'])) {
64+
$level = $config['level'];
65+
}
4866
if (isset($config['connection'])) {
4967
$this->connection = $config['connection'];
5068
}
@@ -54,35 +72,24 @@ function __construct($connection,
5472
if (isset($config['detailed'])) {
5573
$this->detailed = $config['detailed'];
5674
}
57-
if (isset($config['queue_db_saves'])) {
58-
$this->saveWithQueue = $config['queue_db_saves'];
75+
if (isset($config['queue'])) {
76+
$this->saveWithQueue = $config['queue'];
5977
}
60-
if (isset($config['queue_db_name'])) {
61-
$this->saveWithQueueName = $config['queue_db_name'];
78+
if (isset($config['queue_name'])) {
79+
$this->saveWithQueueName = $config['queue_name'];
6280
}
63-
if (isset($config['queue_db_connection'])) {
64-
$this->saveWithQueueConnection = $config['queue_db_connection'];
81+
if (isset($config['queue_connection'])) {
82+
$this->saveWithQueueConnection = $config['queue_connection'];
6583
}
6684
}
6785

68-
//override with config array in logging.php
69-
if (!empty($connection)) {
70-
$this->connection = $connection;
71-
}
72-
if (!empty($collection)) {
73-
$this->collection = $collection;
74-
}
75-
if (!empty($detailed)) {
76-
$this->detailed = $detailed;
77-
}
78-
if (!empty($collection)) {
79-
$this->saveWithQueue = $enableQueue;
80-
}
81-
if (!empty($enableQueue)) {
82-
$this->saveWithQueueName = $queueName;
83-
}
84-
if (!empty($queueConnection)) {
85-
$this->saveWithQueueConnection = $queueConnection;
86+
87+
//Set the processors
88+
if (!empty($processors))
89+
{
90+
foreach ($processors as $processor) {
91+
$this->pushProcessor($processor);
92+
}
8693
}
8794

8895
parent::__construct($level, $bubble);

src/LogToDbHandler.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,15 @@ class LogToDbHandler
2020
*/
2121
public function __invoke(array $config)
2222
{
23+
$processors = [
24+
new IntrospectionProcessor()
25+
];
2326
return new Logger($config['name'] ?? 'LogToDB',
2427
[
25-
new LogToDbCustomLoggingHandler(
26-
$config['connection'] ?? null,
27-
$config['collection'] ?? null,
28-
$config['detailed'] ?? null,
29-
$config['queue'] ?? null,
30-
$config['queue_name'] ?? null,
31-
$config['queue_connection'] ?? null,
32-
$config['level'] ?? null)
28+
new LogToDbCustomLoggingHandler($config, $processors)
3329
],
34-
[
35-
new IntrospectionProcessor()
36-
]);
30+
$processors
31+
);
3732
}
3833

3934
}

src/Models/LogToDbCreateObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function setContextAttribute(array $value)
5252
if (isset($value['exception'])) {
5353
if (!empty($value['exception'])) {
5454
$exception = $value['exception'];
55-
if (strpos(get_class($exception), "\Exception") !== false) {
55+
if (strpos(get_class($exception), "Exception") !== false) {
5656
$newexception = [];
5757
$newexception['class'] = get_class($exception);
5858
if (method_exists($exception, 'getMessage')) {

0 commit comments

Comments
 (0)