19 stable releases (5 major)

Uses old Rust 2015

6.1.1 Apr 18, 2024
6.1.0 Apr 26, 2023
6.0.1 Dec 19, 2021
5.0.0 Nov 1, 2019
1.0.3 Mar 11, 2015

#95 in Unix APIs

Download history 17791/week @ 2024-01-05 17579/week @ 2024-01-12 19976/week @ 2024-01-19 17987/week @ 2024-01-26 19332/week @ 2024-02-02 18899/week @ 2024-02-09 18166/week @ 2024-02-16 18748/week @ 2024-02-23 19337/week @ 2024-03-01 20030/week @ 2024-03-08 18553/week @ 2024-03-15 20087/week @ 2024-03-22 18252/week @ 2024-03-29 20260/week @ 2024-04-05 19766/week @ 2024-04-12 16962/week @ 2024-04-19

78,951 downloads per month
Used in 72 crates (57 directly)

MIT license

31KB
672 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 = "^6.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

~0.8–1.1MB
~18K SLoC