Crate log4rs_mqtt

source ·
Expand description

MQTT Appender for log4rs.

This will format log messages, remove trailing newlines, and publish the message to an MQTT topic. You can programatically create one or use a log4rs.yml definition. In order to use the YAML you’ll need to register the MqttAppenderDeserializer with register.

Examples

Create an MQTT logger programatically

let mqtt_log = MqttAppender::builder()
    .topic("logs")
    .client_id("log_client")
    .build();
let log_config = Config::builder()
    .appender(Appender::builder().build("mqtt", Box::new(mqtt_log)))
    .build(Root::builder().appender("mqtt").build(LevelFilter::Info))
    .unwrap();
log4rs::init_config(log_config).unwrap();

Create an MQTT logger with YAML

appenders:
  mqtt:
    kind: mqtt
    mqtt_server: mqtt://mosquitto.local:1883
    mqtt_client_id: app_logger
root:
  level: info
  appenders:
    - mqtt

Warning

Ensure that paho_mqtt_c and paho_mqtt log targets do not log with this appender, especially when logging DEBUG. You’ll get a recursive call to the MQTT manager setup and the RwLock will level release. If you see sudden application hangs, try decreasing the level or removing the MQTT logger and see if that fixes the problem.

Structs

Functions

  • Register deserializer for creating MQTT appender based on log4rs configuration file.