diff --git a/readme.md b/readme.md index 5aab59c..a581ecd 100644 --- a/readme.md +++ b/readme.md @@ -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'], ], @@ -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). diff --git a/src/LogToDB.php b/src/LogToDB.php index 05fa704..a99adde 100644 --- a/src/LogToDB.php +++ b/src/LogToDB.php @@ -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']; } @@ -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)) { @@ -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, diff --git a/src/LogToDbCustomLoggingHandler.php b/src/LogToDbCustomLoggingHandler.php index ccfb606..e21534f 100644 --- a/src/LogToDbCustomLoggingHandler.php +++ b/src/LogToDbCustomLoggingHandler.php @@ -15,7 +15,6 @@ class LogToDbCustomLoggingHandler extends AbstractProcessingHandler private $connection; private $collection; private $detailed; - private $maxRows; private $saveWithQueue; private $saveWithQueueName; private $saveWithQueueConnection; @@ -23,28 +22,47 @@ class LogToDbCustomLoggingHandler extends AbstractProcessingHandler /** * 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']; } @@ -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); diff --git a/src/LogToDbHandler.php b/src/LogToDbHandler.php index 38d53c5..afc96f9 100644 --- a/src/LogToDbHandler.php +++ b/src/LogToDbHandler.php @@ -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 + ); } } \ No newline at end of file diff --git a/src/Models/LogToDbCreateObject.php b/src/Models/LogToDbCreateObject.php index b42a220..7f8ec91 100644 --- a/src/Models/LogToDbCreateObject.php +++ b/src/Models/LogToDbCreateObject.php @@ -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')) {