syslog-too 0.1.1

A small library to send log messages to syslog locally and over TCP or UDP using Rust. A continuation of the syslog crate.
Documentation

syslog-too: Simple facade library used to create and send syslog messages in Rust.

pipeline status Latest Release

Introduction

A small library to send log messages to syslog locally and over TCP or UDP using Rust. A continuation of syslog.

Installation

syslog-too is available on crates.io and can be included in your Cargo enabled by executing the following command in the root of your project:

cargo add syslog-too

documentation

Reference documentation is available on docs.rs.

Example

extern crate syslog_too;

use syslog_too::{Facility, Formatter3164};

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

  match syslog_too::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_too::Logger implements Log from the log crate, so it can be used as backend for other logging systems:

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

use syslog_too::{Facility, Formatter3164, Logger3164};
use log::{SetLoggerError, LevelFilter};

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

    let logger = syslog_too::unix(formatter).expect("could not connect to syslog");
    log::set_boxed_logger(Box::new(Logger3164::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_too::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")