15 stable releases (5 major)

5.0.0 Nov 1, 2019
4.0.1 Aug 27, 2018
4.0.0 Apr 7, 2018
3.3.0 Aug 18, 2017
0.0.1 Jan 18, 2015

#19 in Debugging

Download history 2679/week @ 2019-07-19 2944/week @ 2019-07-26 4526/week @ 2019-08-02 3921/week @ 2019-08-09 3429/week @ 2019-08-16 3808/week @ 2019-08-23 3324/week @ 2019-08-30 3580/week @ 2019-09-06 4159/week @ 2019-09-13 3777/week @ 2019-09-20 3582/week @ 2019-09-27 4990/week @ 2019-10-04 4317/week @ 2019-10-11 4636/week @ 2019-10-18 3928/week @ 2019-10-25

17,177 downloads per month
Used in 19 crates (16 directly)

MIT license

24KB
509 lines

Sending to Syslog in Rust

Build Status Coverage Status

A small library to write to local syslog.

Installation

syslog is available on crates.io and can be included in your Cargo enabled project like this:

[dependencies]
syslog = "^4.0"

documentation

Reference documentation is available here.

Example

extern crate syslog;

use syslog::{Facility, Formatter3164};

fn main() {
  let formatter = Formatter3164 {
    facility: Facility::LOG_USER,
    hostname: None,
    process: "myprogram".into(),
    pid: 42,
  };

  match syslog::unix(formatter) {
    Err(e)         => println!("impossible to connect to syslog: {:?}", e),
    Ok(mut writer) => {
      writer.err("hello world").expect("could not write error message");
    }
  }
}

The struct syslog::Logger implements Log from the log crate, so it can be used as backend for other logging systems:

extern crate syslog;
#[macro_use]
extern crate log;

use syslog::{Facility, Formatter3164, BasicLogger};
use log::{SetLoggerError, LevelFilter};

fn main() {
    let formatter = Formatter3164 {
        facility: Facility::LOG_USER,
        hostname: None,
        process: "myprogram".into(),
        pid: 0,
    };

    let logger = syslog::unix(formatter).expect("could not connect to syslog");
    log::set_boxed_logger(Box::new(BasicLogger::new(logger)))
            .map(|()| log::set_max_level(LevelFilter::Info));

    info!("hello world");
}

There are 3 functions to create loggers:

  • the unix function sends to the local syslog through a Unix socket: syslog::unix(formatter)
  • the tcp function takes an address for a remote TCP syslog server: tcp(formatter, "127.0.0.1:4242")
  • the udp function takes an address for a local port, and the address remote UDP syslog server: udp(formatter, "127.0.0.1:1234", "127.0.0.1:4242")

Dependencies

~200–335KB