#unix-socket #logging #tcp-udp #logs #sockets #message #formatter

syslog

Syslog message formatter and writer, supporting unix sockets, UDP and TCP exporters

20 stable releases (6 major)

Uses old Rust 2015

7.0.0 Jul 26, 2024
6.1.1 Apr 18, 2024
6.1.0 Apr 26, 2023
6.0.1 Dec 19, 2021
1.0.3 Mar 11, 2015

#12 in Unix APIs

Download history 21393/week @ 2024-07-26 18409/week @ 2024-08-02 19607/week @ 2024-08-09 18818/week @ 2024-08-16 15896/week @ 2024-08-23 16372/week @ 2024-08-30 29954/week @ 2024-09-06 39543/week @ 2024-09-13 40300/week @ 2024-09-20 25907/week @ 2024-09-27 25462/week @ 2024-10-04 23233/week @ 2024-10-11 23986/week @ 2024-10-18 26678/week @ 2024-10-25 21405/week @ 2024-11-01 17988/week @ 2024-11-08

94,632 downloads per month
Used in 91 crates (61 directly)

MIT license

40KB
937 lines

Sending to Syslog in Rust

Build Status Codecov crates.io Version

A small library to send syslog messages to unix sockets, TCP or UDP.

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.7–24MB
~330K SLoC