#syslog #logging #log-level #log #nix #logger

log4rs-syslog

Syslog appender for log4rs based on libc's syslog() function (*nix only)

10 stable releases

Uses old Rust 2015

3.0.3 May 7, 2018
3.0.2 Apr 20, 2018
3.0.1 Jan 15, 2018
3.0.0 Dec 28, 2017
1.0.3 Jul 10, 2017

#470 in Unix APIs

Download history 145/week @ 2024-04-10 208/week @ 2024-04-17 285/week @ 2024-04-24 190/week @ 2024-05-01 122/week @ 2024-05-08 152/week @ 2024-05-15 187/week @ 2024-05-22 271/week @ 2024-05-29 290/week @ 2024-06-05 139/week @ 2024-06-12 240/week @ 2024-06-19 94/week @ 2024-06-26 148/week @ 2024-07-03 251/week @ 2024-07-10 356/week @ 2024-07-17 331/week @ 2024-07-24

1,103 downloads per month
Used in 2 crates

MIT/Apache

24KB
407 lines

Build Status crates.io MIT/Apache 2.0 POSIX-only build tooling

log4rs-syslog

log4rs-syslog - very simple syslog appender for the log4rs based on the libc's syslog() function. Supports only *nix systems.

Documentation on docs.rs

Features:

  • Logging with or without calling openlog() with identification string, logging options and facility.
  • Custom mapping between rust's log crate log levels and syslog's log levels.

Limitations:

  • When there are multiple syslog appenders, openlog() configuration of last built appender is used.
  • openlog() configuration applied when log4rs_syslog::SyslogAppenderBuilder::build() called, not on log4rs::init_config() or log4rs::Handle::set_config().

There is no proper way to fix this limitations while using libc's interface.

Breaking changes

2.0 → 3.0

  • Update to log 0.4 and log4rs 0.8.

1.0 → 2.0

  • log4rs_syslog::register_deserializer() renamed to log4rs_syslog::register().
  • kind in deserializable configuration file changed from syslog to libc-syslog.
  • Log option constants changed from log4rs_syslog::LOG_* to log4rs_syslog::LogOption::LOG_* due to changes in bitflags crate.

Usage

Add this to your Cargo.toml:

[dependencies]
log4rs-syslog = "3.0"

Initialization based on configuration file

Example configuration file:

appenders:
  syslog:
    kind: libc-syslog
    openlog:
      ident: log4rs-syslog-example
      option: LOG_PID | LOG_NDELAY | LOG_CONS
      facility: Daemon
    encoder:
      pattern: "{M} - {m}"
root:
  level: trace
  appenders:
    - syslog

Example code:

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

fn main() {
    let mut deserializers = log4rs::file::Deserializers::new();
    log4rs_syslog::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();

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

    println!("Check your logs for new messages");
}

Manual initialization

Example code:

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

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_syslog::SyslogAppender::builder()
            .encoder(encoder)
            .openlog(
                "log4rs-syslog-example",
                log4rs_syslog::LogOption::LOG_PID,
                log4rs_syslog::Facility::Daemon,
            )
            .build(),
    );

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

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

    println!("Check your logs for new messages");
}

Running examples

git clone --branch b3.0.0 https://github.com/im-0/log4rs-syslog
cd log4rs-syslog
cargo run --example manually
cargo run --example from_conf

Dependencies

~1.5–2.2MB
~35K SLoC