main: Log to journal if possible
dustin/luci/pipeline/head This commit looks good Details

If `luci` is running as a systemd service and the systemd-journal socket
is available, logs will now be sent there instead of to the standard
error stream.  This has the advantage of logging individual fields,
including level/severity, code location, span variables, etc., rather
than a single string.
main
Dustin 2024-08-10 07:10:56 -05:00
parent 154732df72
commit 99e7f6135b
3 changed files with 36 additions and 5 deletions

12
Cargo.lock generated
View File

@ -662,6 +662,7 @@ dependencies = [
"tokio",
"toml",
"tracing",
"tracing-journald",
"tracing-subscriber",
"url",
]
@ -1540,6 +1541,17 @@ dependencies = [
"valuable",
]
[[package]]
name = "tracing-journald"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba316a74e8fc3c3896a850dba2375928a9fa171b085ecddfc7c054d39970f3fd"
dependencies = [
"libc",
"tracing-core",
"tracing-subscriber",
]
[[package]]
name = "tracing-log"
version = "0.2.0"

View File

@ -19,5 +19,6 @@ thiserror = "1.0.63"
tokio = { version = "1.39.1", default-features = false, features = ["rt", "macros", "rt-multi-thread", "signal", "process"] }
toml = "0.8.15"
tracing = "0.1.40"
tracing-journald = "0.3.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
url = { version = "2.5.2", features = ["serde"] }

View File

@ -11,16 +11,34 @@ use std::time::Duration;
use tokio::signal::unix::SignalKind;
use tokio::sync::Notify;
use tracing::{debug, error, info};
use tracing_subscriber::prelude::*;
use config::Configuration;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt()
.without_time()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_writer(std::io::stderr)
.init();
match tracing_journald::layer() {
Ok(l) => {
let subscriber = tracing_subscriber::Registry::default()
.with(l)
.with(tracing_subscriber::EnvFilter::from_default_env());
if let Err(e) = tracing::subscriber::set_global_default(subscriber)
{
eprintln!("Failed to set global tracing subscriber: {}", e);
}
}
Err(e) => {
if e.kind() != std::io::ErrorKind::NotFound {
eprintln!("Unable to write to journald socket: {}", e);
}
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::from_default_env(),
)
.with_writer(std::io::stderr)
.init();
}
};
let mut args = std::env::args_os();
let config_path = args.nth(1).map(PathBuf::from);