Skip to content

Instantly share code, notes, and snippets.

@imbolc
Created February 16, 2021 13:51
Show Gist options
  • Save imbolc/f75f924f66c129f0e1e910f59fb75c0d to your computer and use it in GitHub Desktop.
Save imbolc/f75f924f66c129f0e1e910f59fb75c0d to your computer and use it in GitHub Desktop.
Rust logging into journald configured by the `RUST_LOG` environment variable
anyhow = "1"
log = "0.4"
tracing = { git = "https://github.com/tokio-rs/tracing", rev = "f81426b" }
tracing-subscriber = { git = "https://github.com/tokio-rs/tracing", rev = "f81426b" }
tracing-journald = { git = "https://github.com/tokio-rs/tracing", rev = "f81426b" }
use anyhow::{Context, Result};
pub fn init_journald_logging() -> Result<()> {
use tracing_subscriber::{prelude::*, registry, EnvFilter};
registry()
.with(EnvFilter::try_from_default_env().context("couldn't parse `RUST_LOG` env var")?)
.with(tracing_journald::subscriber().context("couldn't connect to journald")?)
.init();
Ok(())
}
use anyhow::Result;
fn main() -> Result<()> {
lib::init_journald_logging()?;
log::info!("Tracing intercepts log events, so you can just continue using it");
tracing::error!(foo = "bar", "Or you can use events to pass structural data");
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment