feat(logging): add a journald feature to log to systemd

This commit is contained in:
Harsh Shandilya 2022-03-10 14:20:20 +05:30
parent 28fa08a67b
commit 139e833d04
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
3 changed files with 26 additions and 0 deletions

12
Cargo.lock generated
View file

@ -93,6 +93,7 @@ dependencies = [
"serde_derive", "serde_derive",
"toml", "toml",
"tracing", "tracing",
"tracing-journald",
"tracing-subscriber", "tracing-subscriber",
] ]
@ -510,6 +511,17 @@ dependencies = [
"valuable", "valuable",
] ]
[[package]]
name = "tracing-journald"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36a63bcca9cab23195673d7c3c7db8aff5fc062b82cd1e51c396d9dc26968c3c"
dependencies = [
"libc",
"tracing-core",
"tracing-subscriber",
]
[[package]] [[package]]
name = "tracing-log" name = "tracing-log"
version = "0.1.2" version = "0.1.2"

View file

@ -20,7 +20,11 @@ serde = "1.0.136"
serde_derive = "1.0.136" serde_derive = "1.0.136"
toml = "0.5.8" toml = "0.5.8"
tracing = "0.1.32" tracing = "0.1.32"
tracing-journald = { version = "0.2.3", optional = true }
tracing-subscriber = { version = "0.3.9", features = ["env-filter"] } tracing-subscriber = { version = "0.3.9", features = ["env-filter"] }
[dev-dependencies] [dev-dependencies]
assay = "0.1.1" assay = "0.1.1"
[features]
journald = ["tracing-journald"]

View file

@ -10,6 +10,8 @@ use anyhow::{anyhow, Result};
use clipboard::{ClipboardContext, ClipboardProvider}; use clipboard::{ClipboardContext, ClipboardProvider};
use dirs::config_dir; use dirs::config_dir;
use tracing::{debug, error, Level}; use tracing::{debug, error, Level};
#[cfg(journald)]
use tracing_journald::Subscriber;
use tracing_subscriber::FmtSubscriber; use tracing_subscriber::FmtSubscriber;
use crate::config::{Act, Match, Replacements}; use crate::config::{Act, Match, Replacements};
@ -47,6 +49,7 @@ fn print_version() {
); );
} }
#[cfg(not(journald))]
fn configure_tracing() { fn configure_tracing() {
let subscriber = FmtSubscriber::builder() let subscriber = FmtSubscriber::builder()
.with_max_level(Level::TRACE) .with_max_level(Level::TRACE)
@ -56,6 +59,13 @@ fn configure_tracing() {
.expect("setting default subscriber failed"); .expect("setting default subscriber failed");
} }
#[cfg(journald)]
fn configure_tracing() {
let sub = Registry::default()
.with(Subscriber::new().unwrap().with_field_prefix(None));
tracing::collect::with_default(sub, f);
}
fn get_config_path() -> Result<PathBuf> { fn get_config_path() -> Result<PathBuf> {
let mut config_path = let mut config_path =
config_dir().ok_or_else(|| anyhow!("Failed to get config dir"))?; config_dir().ok_or_else(|| anyhow!("Failed to get config dir"))?;