Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ You will need to add an array under 'channels' for Log-to-DB here like so:
```php
'channels' => [
'stack' => [
'name' => 'Log Stack',
'driver' => 'stack',
'channels' => ['database', 'mongodb'],
],
Expand All @@ -47,7 +48,7 @@ You will need to add an array under 'channels' for Log-to-DB here like so:
* driver = Required to trigger the log driver.
* via = The Log handler class.
* level = The minimum error level to trigger this Log Channel.
* name = The channel name that will be stored with the Log event.
* 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.
* connection = The DB connection from config/database.php to use (default: 'default').
* collection = The DB table or collection name. (Default: log).
* detailed = Store detailed log on Exceptions like stack-trace (default: true).
Expand Down
10 changes: 6 additions & 4 deletions src/LogToDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ function __construct(string $channelConnection = null,
if (isset($config['detailed'])) {
$this->detailed = $config['detailed'];
}
if (isset($config['max_rows'])) {
$this->maxRows = (int)$config['max_rows'];
}
if (isset($config['queue_db_saves'])) {
$this->saveWithQueue = $config['queue_db_saves'];
}
Expand Down Expand Up @@ -217,6 +214,11 @@ public function newFromMonolog(array $record)
{
if (!empty($this->connection)) {
if ($this->saveWithQueue) {
if (isset($record['context']['exception']) and !empty($record['context']['exception'])) {
if (strpos(get_class($record['context']['exception']), "Exception") !== false) {
dispatch_now(new SaveNewLogEvent($this, $record));
}
}
if (empty($this->saveWithQueueName) and empty($this->saveWithQueueConnection)) {
dispatch(new SaveNewLogEvent($this, $record));
} else if (!empty($this->saveWithQueueName) and !empty($this->saveWithQueueConnection)) {
Expand All @@ -230,7 +232,7 @@ public function newFromMonolog(array $record)
dispatch(new SaveNewLogEvent($this, $record))
->onQueue($this->saveWithQueueName);
}
} else {
} else {
$log = CreateLogFromRecord::generate(
$this->connection,
$this->collection,
Expand Down
89 changes: 48 additions & 41 deletions src/LogToDbCustomLoggingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,54 @@ class LogToDbCustomLoggingHandler extends AbstractProcessingHandler
private $connection;
private $collection;
private $detailed;
private $maxRows;
private $saveWithQueue;
private $saveWithQueueName;
private $saveWithQueueConnection;

/**
* LogToDbHandler constructor.
*
* @param string $connection The DB connection.
* @param string $collection The Collection/Table name for DB.
* @param bool $detailed Detailed error logging.
* @param int $maxRows The Maximum number of rows/objects before auto purge.
* @param bool $enableQueue disable|enable enqueuing log db update
* @param string $queueName optional name of queue
* @param string $queueConnection optional name of queue connection
* @param int $level The minimum logging level at which this handler will be triggered
* @param array $config Logging configuration from logging.php
* @param array $processors collection of log processors
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
*/
function __construct($connection,
$collection,
$detailed,
$enableQueue,
$queueName,
$queueConnection,
$level = Logger::DEBUG, bool $bubble = true)
function __construct(array $config,
array $processors,
bool $bubble = true)
{
//Set default level debug
$level = 'debug';

//Log default config if present
$config = config('logtodb');
$defaultConfig = config('logtodb');

if (!empty($defaultConfig)) {
if (isset($defaultConfig['connection'])) {
$this->connection = $defaultConfig['connection'];
}
if (isset($defaultConfig['collection'])) {
$this->collection = $defaultConfig['collection'];
}
if (isset($defaultConfig['detailed'])) {
$this->detailed = $defaultConfig['detailed'];
}
if (isset($defaultConfig['queue_db_saves'])) {
$this->saveWithQueue = $defaultConfig['queue_db_saves'];
}
if (isset($defaultConfig['queue_db_name'])) {
$this->saveWithQueueName = $defaultConfig['queue_db_name'];
}
if (isset($defaultConfig['queue_db_connection'])) {
$this->saveWithQueueConnection = $defaultConfig['queue_db_connection'];
}
}


//Override default config with array in logging.php
if (!empty($config)) {
if (isset($config['level'])) {
$level = $config['level'];
}
if (isset($config['connection'])) {
$this->connection = $config['connection'];
}
Expand All @@ -54,35 +72,24 @@ function __construct($connection,
if (isset($config['detailed'])) {
$this->detailed = $config['detailed'];
}
if (isset($config['queue_db_saves'])) {
$this->saveWithQueue = $config['queue_db_saves'];
if (isset($config['queue'])) {
$this->saveWithQueue = $config['queue'];
}
if (isset($config['queue_db_name'])) {
$this->saveWithQueueName = $config['queue_db_name'];
if (isset($config['queue_name'])) {
$this->saveWithQueueName = $config['queue_name'];
}
if (isset($config['queue_db_connection'])) {
$this->saveWithQueueConnection = $config['queue_db_connection'];
if (isset($config['queue_connection'])) {
$this->saveWithQueueConnection = $config['queue_connection'];
}
}

//override with config array in logging.php
if (!empty($connection)) {
$this->connection = $connection;
}
if (!empty($collection)) {
$this->collection = $collection;
}
if (!empty($detailed)) {
$this->detailed = $detailed;
}
if (!empty($collection)) {
$this->saveWithQueue = $enableQueue;
}
if (!empty($enableQueue)) {
$this->saveWithQueueName = $queueName;
}
if (!empty($queueConnection)) {
$this->saveWithQueueConnection = $queueConnection;

//Set the processors
if (!empty($processors))
{
foreach ($processors as $processor) {
$this->pushProcessor($processor);
}
}

parent::__construct($level, $bubble);
Expand Down
17 changes: 6 additions & 11 deletions src/LogToDbHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,15 @@ class LogToDbHandler
*/
public function __invoke(array $config)
{
$processors = [
new IntrospectionProcessor()
];
return new Logger($config['name'] ?? 'LogToDB',
[
new LogToDbCustomLoggingHandler(
$config['connection'] ?? null,
$config['collection'] ?? null,
$config['detailed'] ?? null,
$config['queue'] ?? null,
$config['queue_name'] ?? null,
$config['queue_connection'] ?? null,
$config['level'] ?? null)
new LogToDbCustomLoggingHandler($config, $processors)
],
[
new IntrospectionProcessor()
]);
$processors
);
}

}
2 changes: 1 addition & 1 deletion src/Models/LogToDbCreateObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function setContextAttribute(array $value)
if (isset($value['exception'])) {
if (!empty($value['exception'])) {
$exception = $value['exception'];
if (strpos(get_class($exception), "\Exception") !== false) {
if (strpos(get_class($exception), "Exception") !== false) {
$newexception = [];
$newexception['class'] = get_class($exception);
if (method_exists($exception, 'getMessage')) {
Expand Down