Allow broadcasting to a specific log channel (#40)

* Allow broadcasting to a specific log channel

* Fix  check condition

* Update src/ConnectionManager.php

* Update config/mqtt-client.php

---------

Co-authored-by: Namoshek <Namoshek@users.noreply.github.com>
This commit is contained in:
bsdnomad 2023-05-15 21:04:29 +03:00 committed by GitHub
parent 0669643386
commit b059586d08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View File

@ -55,6 +55,10 @@ return [
// with the log level as configured. // with the log level as configured.
'enable_logging' => env('MQTT_ENABLE_LOGGING', true), 'enable_logging' => env('MQTT_ENABLE_LOGGING', true),
// Which logging channel to use for logs produced by the MQTT client.
// If left empty, the default log channel or stack is being used.
'log_channel' => env('MQTT_LOG_CHANNEL', null),
// Defines which repository implementation shall be used. Currently, // Defines which repository implementation shall be used. Currently,
// only a MemoryRepository is supported. // only a MemoryRepository is supported.
'repository' => MemoryRepository::class, 'repository' => MemoryRepository::class,

View File

@ -139,11 +139,16 @@ class ConnectionManager
$cleanSession = (bool) Arr::get($config, 'use_clean_session', true); $cleanSession = (bool) Arr::get($config, 'use_clean_session', true);
$repository = Arr::get($config, 'repository', Repository::class); $repository = Arr::get($config, 'repository', Repository::class);
$loggingEnabled = (bool) Arr::get($config, 'enable_logging', true); $loggingEnabled = (bool) Arr::get($config, 'enable_logging', true);
$logChannel = Arr::get($config, 'log_channel', null);
$settings = $this->buildConnectionSettings(Arr::get($config, 'connection_settings', [])); $settings = $this->buildConnectionSettings(Arr::get($config, 'connection_settings', []));
$repository = $this->application->make($repository); $repository = $this->application->make($repository);
$logger = $loggingEnabled ? $this->application->make('log') : null; $logger = $loggingEnabled ? $this->application->make('log') : null;
if($logger && $logChannel) {
$logger = $logger->channel($logChannel);
}
$client = new MqttClient($host, $port, $clientId, $protocol, $repository, $logger); $client = new MqttClient($host, $port, $clientId, $protocol, $repository, $logger);
$client->connect($settings, $cleanSession); $client->connect($settings, $cleanSession);