#mqtt #logging #log #logger

已撤回 log4s-mqtt

基于paho MQTT的log4rs的MQTT追加器

使用旧的Rust 2015

1.0.0 2023年9月20日

#154 in #mqtt

MIT/Apache

14KB
142 代码行

crates.io MIT/Apache 2.0

log4rs-mqtt

log4rs-mqtt - 基于PAHO MQTT的log4rs MQTT追加器。

在docs.rs上的文档

功能

  • 指定要使用的MQTT服务器。
  • 指定用于日志的MQTT主题。

用法

将此添加到您的Cargo.toml中

[dependencies]
log4rs-mqtt = "1.0"

基于配置文件的初始化

示例配置文件

appenders:
  mqtt:
    kind: mqtt
    mqtt_server: mqtt://mosquito.cluster.local:1883
    mqtt_client_id: log_client
    topic: logs
    qos: 1
    encoder:
      pattern: "{M} - {m}"
root:
  level: info
  appenders:
    - mqtt

示例代码

#[macro_use]
extern crate log;
extern crate log4rs;
extern crate log4rs_mqtt;

fn main() {
    let mut deserializers = log4rs::file::Deserializers::new();
    log4rs_mqtt::register(&mut deserializers);

    // Note that configuration file should have right extension, otherwise log4rs will fail to
    // recognize format.
    log4rs::init_file("test.yaml", deserializers).unwrap();

    info!("Example information message");
    warn!("Example warning message");
    error!("Example error message");
}

手动初始化

示例代码

#[macro_use]
extern crate log;
extern crate log4rs;
extern crate log4rs_mqtt;

fn main() {
    // Use custom PatternEncoder to avoid duplicate timestamps in logs.
    let encoder = Box::new(log4rs::encode::pattern::PatternEncoder::new("{M} - {m}"));

    let appender = Box::new(
        log4rs_mqtt::MqttAppender::builder()
            .encoder(encoder)
            .mqtt_server("mqtt://mosquitto.cluster.local:1883")
            .mqtt_client_id("log_client")
            .qos(1)
            .topic("logs")
            .build(),
    );

    let config = log4rs::config::Config::builder()
        .appender(log4rs::config::Appender::builder().build(
            "mqtt",
            appender,
        ))
        .build(log4rs::config::Root::builder().appender("mqtt").build(
            log::LevelFilter::Info,
        ))
        .unwrap();
    log4rs::init_config(config).unwrap();

    info!("Example information message");
    warn!("Example warning message");
    error!("Example error message");
}

依赖关系

~19–31MB
~570K SLoC