From af555c337c913f8ecc3e5d4709a8cf7d2bf23ecc Mon Sep 17 00:00:00 2001 From: Gaius Date: Fri, 24 May 2024 15:29:21 +0800 Subject: [PATCH] feat: redirect stderr to stderr log file (#494) Signed-off-by: Gaius --- Cargo.lock | 18 +++++++++--------- Cargo.toml | 16 ++++++++-------- dragonfly-client/Cargo.toml | 2 +- dragonfly-client/src/tracing/mod.rs | 27 +++++++++++++++++++++++---- 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f5341cc..29b09511 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -959,7 +959,7 @@ dependencies = [ [[package]] name = "dragonfly-client" -version = "0.1.69" +version = "0.1.70" dependencies = [ "anyhow", "bytes", @@ -1012,7 +1012,7 @@ dependencies = [ "tower", "tracing", "tracing-appender", - "tracing-log 0.1.4", + "tracing-log 0.2.0", "tracing-opentelemetry", "tracing-subscriber", "url", @@ -1022,7 +1022,7 @@ dependencies = [ [[package]] name = "dragonfly-client-backend" -version = "0.1.69" +version = "0.1.70" dependencies = [ "dragonfly-client-core", "futures", @@ -1040,7 +1040,7 @@ dependencies = [ [[package]] name = "dragonfly-client-config" -version = "0.1.69" +version = "0.1.70" dependencies = [ "dragonfly-client-core", "home", @@ -1059,7 +1059,7 @@ dependencies = [ [[package]] name = "dragonfly-client-core" -version = "0.1.69" +version = "0.1.70" dependencies = [ "libloading", "reqwest", @@ -1070,7 +1070,7 @@ dependencies = [ [[package]] name = "dragonfly-client-init" -version = "0.1.69" +version = "0.1.70" dependencies = [ "anyhow", "clap", @@ -1086,7 +1086,7 @@ dependencies = [ [[package]] name = "dragonfly-client-storage" -version = "0.1.69" +version = "0.1.70" dependencies = [ "base16ct", "blake3", @@ -1110,7 +1110,7 @@ dependencies = [ [[package]] name = "dragonfly-client-util" -version = "0.1.69" +version = "0.1.70" dependencies = [ "dragonfly-api", "dragonfly-client-core", @@ -1568,7 +1568,7 @@ dependencies = [ [[package]] name = "hdfs" -version = "0.1.69" +version = "0.1.70" dependencies = [ "dragonfly-client-backend", "dragonfly-client-core", diff --git a/Cargo.toml b/Cargo.toml index 0ae01802..f5233a33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ members = [ ] [workspace.package] -version = "0.1.69" +version = "0.1.70" authors = ["The Dragonfly Developers"] homepage = "https://d7y.io/" repository = "https://github.com/dragonflyoss/client.git" @@ -22,13 +22,13 @@ readme = "README.md" edition = "2021" [workspace.dependencies] -dragonfly-client = { path = "dragonfly-client", version = "0.1.69" } -dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.69" } -dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.69" } -dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.69" } -dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.69" } -dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.69" } -dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.69" } +dragonfly-client = { path = "dragonfly-client", version = "0.1.70" } +dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.70" } +dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.70" } +dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.70" } +dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.70" } +dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.70" } +dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.70" } thiserror = "1.0" dragonfly-api = "2.0.113" reqwest = { version = "0.11.27", features = ["stream", "native-tls", "default-tls", "rustls-tls"] } diff --git a/dragonfly-client/Cargo.toml b/dragonfly-client/Cargo.toml index 359e8c4a..7c8e5f25 100644 --- a/dragonfly-client/Cargo.toml +++ b/dragonfly-client/Cargo.toml @@ -54,7 +54,7 @@ http.workspace = true openssl.workspace = true clap.workspace = true anyhow.workspace = true -tracing-log = "0.1" +tracing-log = "0.2" tracing-subscriber = { version = "0.3", features = ["env-filter"] } tracing-appender = "0.2.3" tracing-opentelemetry = "0.18.0" diff --git a/dragonfly-client/src/tracing/mod.rs b/dragonfly-client/src/tracing/mod.rs index 66bc3045..59fbdc63 100644 --- a/dragonfly-client/src/tracing/mod.rs +++ b/dragonfly-client/src/tracing/mod.rs @@ -15,7 +15,9 @@ */ use opentelemetry::sdk::propagation::TraceContextPropagator; -use std::path::PathBuf; +use std::fs::OpenOptions; +use std::os::unix::io::AsRawFd; +use std::path::{Path, PathBuf}; use tracing::{info, Level}; use tracing_appender::non_blocking::WorkerGuard; use tracing_appender::rolling::{RollingFileAppender, Rotation}; @@ -71,16 +73,16 @@ pub fn init_tracing( .with_thread_names(false) .with_thread_ids(false) .compact(); - guards.push(rolling_writer_guard); + // Setup env filter for log level. let env_filter = EnvFilter::try_from_default_env() .unwrap_or_else(|_| EnvFilter::default().add_directive(log_level.into())); let subscriber = Registry::default() .with(env_filter) - .with(stdout_logging_layer) - .with(file_logging_layer); + .with(file_logging_layer) + .with(stdout_logging_layer); // Setup jaeger layer. if let Some(jaeger_addr) = jaeger_addr { @@ -108,5 +110,22 @@ pub fn init_tracing( log_level ); + // Redirect stderr to file. + redirect_stderr_to_file(log_dir); + guards } + +// Redirect stderr to file. +fn redirect_stderr_to_file(log_dir: &Path) { + let log_path = log_dir.join("stderr.log"); + let file = OpenOptions::new() + .create(true) + .append(true) + .open(log_path) + .unwrap(); + + unsafe { + libc::dup2(file.as_raw_fd(), libc::STDERR_FILENO); + } +}