285 words
1 minute
sysunixlog
Anubhav Gain
2023-12-05

Rust System Log (Unix & Like-Unix)#

In this blog post, we’ll explore a simple Rust program that demonstrates how to log a message to the system log on a Linux-based system using the syslog crate.

Prerequisites#

Before running this program, ensure you have the following installed on your system:

  • Rust toolchain (including Cargo)
  • Systemd or another syslog daemon (such as rsyslog or syslog-ng)

Building the Program#

  1. Clone or download this repository to your local machine.
  2. Navigate to the project directory in your terminal.
    Terminal window
    cd sysunixlog
  3. Build the program using Cargo:
    Terminal window
    cargo build --release

Running the Program#

After successfully building the program, execute it using:

Terminal window
cargo run --release

or run the binary directly:

Terminal window
./target/release/sysunixlog

Checking the System Log#

For systemd users:#

Use the journalctl command to view logs managed by systemd. To filter logs from your program, you can use:

Terminal window
journalctl | grep myprogram

Replace “myprogram” with the name specified in your Rust code.

For traditional syslog users:#

Use the tail command to view the syslog file. For example:

Terminal window
sudo tail -f /var/log/syslog | grep myprogram

Replace “myprogram” with the name specified in your Rust code.

Troubleshooting#

  • Ensure that the syslog service is running on your system.
  • Check permissions if you encounter issues with logging to the system log.
  • If you encounter any issues, feel free to open an issue in this repository for assistance.

Now, let’s take a look at the Rust code:

extern crate syslog;
use syslog::{Facility, Formatter3164};
fn main() {
// Initialize the logger
let formatter = Formatter3164 {
facility: Facility::LOG_USER,
hostname: None,
process: "sysunixlog".into(),
pid: 0,
};
match syslog::unix(formatter) {
Err(e) => println!("impossible to connect to syslog: {}", e),
Ok(mut writer) => {
// Log the message
writer.err("Genesis of Generic Germination U0001F49A, Anubhav!").expect("could not write error message");
}
}
}

And there you have it! With just a few simple steps, you can log messages to the system log in Rust. Happy logging! 🚀

sysunixlog
https://mranv.pages.dev/posts/sysunixlog/
Author
Anubhav Gain
Published at
2023-12-05
License
CC BY-NC-SA 4.0