diff --git a/samples/net/mqtt_publisher/README.rst b/samples/net/mqtt_publisher/README.rst index 4f5f90bb821..e98c15623e1 100644 --- a/samples/net/mqtt_publisher/README.rst +++ b/samples/net/mqtt_publisher/README.rst @@ -221,6 +221,27 @@ broker or uses a different port number, modify the following values: #define SOCKS5_PROXY_ADDR SERVER_ADDR #define SOCKS5_PROXY_PORT 1080 +MQTT logging backend +==================== + +The sample can be configured to use an MQTT logging backend, which allows log +messages from the application to be published to an MQTT broker. This feature +uses the same MQTT client instance as the main sample application. + +The MQTT logging backend uses the :c:func:`log_backend_mqtt_client_set` API to +register an MQTT client for publishing log messages. The backend only uses the +client's :c:func:`mqtt_publish` function and does not manage the client's +connection lifecycle - this remains the application's responsibility. + +To enable the MQTT logging backend in the sample, build it with +``-DEXTRA_CONF_FILE=overlay-log-backend-mqtt.conf`` parameter. + +Key configuration options available: + +- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_TOPIC_DEFAULT`: Topic for publishing logs (default: "zephyr/logs") +- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_QOS`: QoS level for log messages (default: 0) +- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_RETAIN`: Whether to retain log messages (default: disabled) +- :kconfig:option:`CONFIG_LOG_BACKEND_MQTT_MAX_MSG_SIZE`: Maximum log message size (default: 256 bytes) Running on cc3220sf_launchxl ============================ diff --git a/samples/net/mqtt_publisher/overlay-log-backend-mqtt.conf b/samples/net/mqtt_publisher/overlay-log-backend-mqtt.conf new file mode 100644 index 00000000000..fc6dd18b758 --- /dev/null +++ b/samples/net/mqtt_publisher/overlay-log-backend-mqtt.conf @@ -0,0 +1,5 @@ +CONFIG_LOG_BACKEND_MQTT=y +CONFIG_LOG_BACKEND_MQTT_QOS=0 +CONFIG_LOG_BACKEND_MQTT_RETAIN=n +CONFIG_LOG_BACKEND_MQTT_MAX_MSG_SIZE=1024 +CONFIG_LOG_MODE_DEFERRED=y diff --git a/samples/net/mqtt_publisher/src/main.c b/samples/net/mqtt_publisher/src/main.c index 2cf61ff6ead..03050685b41 100644 --- a/samples/net/mqtt_publisher/src/main.c +++ b/samples/net/mqtt_publisher/src/main.c @@ -11,6 +11,9 @@ LOG_MODULE_REGISTER(net_mqtt_publisher_sample, LOG_LEVEL_DBG); #include #include #include +#if defined(CONFIG_LOG_BACKEND_MQTT) +#include +#endif #include #include @@ -172,6 +175,10 @@ void mqtt_evt_handler(struct mqtt_client *const client, } #endif +#if defined(CONFIG_LOG_BACKEND_MQTT) + log_backend_mqtt_client_set(client); +#endif + break; case MQTT_EVT_DISCONNECT: @@ -180,6 +187,10 @@ void mqtt_evt_handler(struct mqtt_client *const client, connected = false; clear_fds(); +#if defined(CONFIG_LOG_BACKEND_MQTT) + log_backend_mqtt_client_set(NULL); +#endif + break; case MQTT_EVT_PUBACK: