Skip to content

Commit 94d21d2

Browse files
authored
feat: support to specify max files of tracing (#185)
Signed-off-by: Gaius <[email protected]>
1 parent 99cdf91 commit 94d21d2

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dragonfly-client"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
authors = ["The Dragonfly Developers"]
55
homepage = "https://d7y.io/"
66
repository = "https://github.com/dragonflyoss/client.git"
@@ -28,7 +28,7 @@ home = "0.5.4"
2828
tracing = "0.1"
2929
tracing-log = "0.1"
3030
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
31-
tracing-appender = "0.2"
31+
tracing-appender = "0.2.3"
3232
tracing-opentelemetry = "0.18.0"
3333
humantime = "2.1.0"
3434
serde = { version = "1.0", features = ["derive"] }

src/bin/dfdaemon/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct Args {
5959
short = 'l',
6060
long,
6161
default_value = "info",
62-
help = "Set the logging level [trace, debug, info, warn, error]"
62+
help = "Specify the logging level [trace, debug, info, warn, error]"
6363
)]
6464
log_level: Level,
6565

@@ -69,6 +69,13 @@ struct Args {
6969
help = "Specify the log directory"
7070
)]
7171
log_dir: PathBuf,
72+
73+
#[arg(
74+
long,
75+
default_value_t = 24,
76+
help = "Specify the max number of log files"
77+
)]
78+
log_max_files: usize,
7279
}
7380

7481
#[tokio::main]
@@ -89,6 +96,7 @@ async fn main() -> Result<(), anyhow::Error> {
8996
dfdaemon::NAME,
9097
&args.log_dir,
9198
args.log_level,
99+
args.log_max_files,
92100
config.tracing.addr.to_owned(),
93101
);
94102

src/bin/dfget/main.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ struct Args {
7272
long = "timeout",
7373
value_parser= humantime::parse_duration,
7474
default_value = "2h",
75-
help = "Set the timeout for downloading a file"
75+
help = "Specify the timeout for downloading a file"
7676
)]
7777
timeout: Duration,
7878

7979
#[arg(
8080
long = "piece-length",
8181
default_value_t = 4194304,
82-
help = "Set the byte length of the piece"
82+
help = "Specify the byte length of the piece"
8383
)]
8484
piece_length: u64,
8585

8686
#[arg(
8787
long = "download-rate-limit",
8888
default_value_t = 2147483648,
89-
help = "Set the rate limit of the downloading in bytes per second"
89+
help = "Specify the rate limit of the downloading in bytes per second"
9090
)]
9191
download_rate_limit: u64,
9292

@@ -102,7 +102,7 @@ struct Args {
102102
short = 'p',
103103
long = "priority",
104104
default_value_t = 6,
105-
help = "Set the priority for scheduling task"
105+
help = "Specify the priority for scheduling task"
106106
)]
107107
priority: i32,
108108

@@ -124,7 +124,7 @@ struct Args {
124124
short = 'H',
125125
long = "header",
126126
required = false,
127-
help = "Set the header for downloading file, e.g. --header='Content-Type: application/json' --header='Accept: application/json'"
127+
help = "Specify the header for downloading file, e.g. --header='Content-Type: application/json' --header='Accept: application/json'"
128128
)]
129129
header: Option<Vec<String>>,
130130

@@ -146,7 +146,7 @@ struct Args {
146146
short = 'l',
147147
long,
148148
default_value = "info",
149-
help = "Set the logging level [trace, debug, info, warn, error]"
149+
help = "Specify the logging level [trace, debug, info, warn, error]"
150150
)]
151151
log_level: Level,
152152

@@ -157,6 +157,13 @@ struct Args {
157157
)]
158158
log_dir: PathBuf,
159159

160+
#[arg(
161+
long,
162+
default_value_t = 24,
163+
help = "Specify the max number of log files"
164+
)]
165+
log_max_files: usize,
166+
160167
#[arg(
161168
short = 'c',
162169
long = "dfdaemon-config",
@@ -175,7 +182,7 @@ struct Args {
175182
#[arg(
176183
long = "dfdaemon-log-level",
177184
default_value = "info",
178-
help = "Set the dfdaemon's logging level [trace, debug, info, warn, error]"
185+
help = "Specify the dfdaemon's logging level [trace, debug, info, warn, error]"
179186
)]
180187
dfdaemon_log_level: Level,
181188

@@ -185,6 +192,13 @@ struct Args {
185192
help = "Specify the dfdaemon's log directory"
186193
)]
187194
dfdaemon_log_dir: PathBuf,
195+
196+
#[arg(
197+
long,
198+
default_value_t = 24,
199+
help = "Specify the dfdaemon's max number of log files"
200+
)]
201+
dfdaemon_log_max_files: usize,
188202
}
189203

190204
#[tokio::main]
@@ -193,14 +207,21 @@ async fn main() -> Result<(), anyhow::Error> {
193207
let args = Args::parse();
194208

195209
// Initialize tracting.
196-
let _guards = init_tracing(dfget::NAME, &args.log_dir, args.log_level, None);
210+
let _guards = init_tracing(
211+
dfget::NAME,
212+
&args.log_dir,
213+
args.log_level,
214+
args.log_max_files,
215+
None,
216+
);
197217

198218
// Get or create dfdaemon download client.
199219
let dfdaemon_download_client = get_or_create_dfdaemon_download_client(
200220
args.dfdaemon_config,
201221
args.endpoint.clone(),
202222
args.dfdaemon_log_dir,
203223
args.dfdaemon_log_level,
224+
args.dfdaemon_log_max_files,
204225
args.dfdaemon_lock_path,
205226
)
206227
.await
@@ -269,6 +290,7 @@ async fn get_or_create_dfdaemon_download_client(
269290
endpoint: PathBuf,
270291
log_dir: PathBuf,
271292
log_level: Level,
293+
log_max_files: usize,
272294
lock_path: PathBuf,
273295
) -> Result<DfdaemonDownloadClient, anyhow::Error> {
274296
// Get dfdaemon download client and check its health.
@@ -288,7 +310,7 @@ async fn get_or_create_dfdaemon_download_client(
288310
}
289311

290312
// Spawn a dfdaemon process.
291-
let child = spawn_dfdaemon(config_path, log_dir, log_level)?;
313+
let child = spawn_dfdaemon(config_path, log_dir, log_level, log_max_files)?;
292314
info!("spawn dfdaemon process: {:?}", child);
293315

294316
// Initialize the timeout of checking dfdaemon's health.
@@ -333,6 +355,7 @@ fn spawn_dfdaemon(
333355
config_path: PathBuf,
334356
log_dir: PathBuf,
335357
log_level: Level,
358+
log_max_files: usize,
336359
) -> Result<Child, anyhow::Error> {
337360
// Create dfdaemon command.
338361
let mut cmd = Command::new("dfdaemon");
@@ -343,7 +366,9 @@ fn spawn_dfdaemon(
343366
.arg("--log-dir")
344367
.arg(log_dir)
345368
.arg("--log-level")
346-
.arg(log_level.to_string());
369+
.arg(log_level.to_string())
370+
.arg("--log-max-files")
371+
.arg(log_max_files.to_string());
347372

348373
// Redirect stdin, stdout, stderr to /dev/null.
349374
cmd.stdin(Stdio::null())

src/bin/dfstore/main.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct Args {
4545
short = 'l',
4646
long,
4747
default_value = "info",
48-
help = "Set the logging level [trace, debug, info, warn, error]"
48+
help = "Specify the logging level [trace, debug, info, warn, error]"
4949
)]
5050
log_level: Level,
5151

@@ -56,6 +56,13 @@ struct Args {
5656
)]
5757
log_dir: PathBuf,
5858

59+
#[arg(
60+
long,
61+
default_value_t = 24,
62+
help = "Specify the max number of log files"
63+
)]
64+
log_max_files: usize,
65+
5966
#[command(subcommand)]
6067
command: Command,
6168
}
@@ -95,5 +102,11 @@ fn main() {
95102
let args = Args::parse();
96103

97104
// Initialize tracing.
98-
let _guards = init_tracing(dfstore::NAME, &args.log_dir, args.log_level, None);
105+
let _guards = init_tracing(
106+
dfstore::NAME,
107+
&args.log_dir,
108+
args.log_level,
109+
args.log_max_files,
110+
None,
111+
);
99112
}

src/tracing/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub fn init_tracing(
2626
name: &str,
2727
log_dir: &PathBuf,
2828
log_level: Level,
29+
log_max_files: usize,
2930
jaeger_addr: Option<String>,
3031
) -> Vec<WorkerGuard> {
3132
let mut guards = vec![];
@@ -43,7 +44,13 @@ pub fn init_tracing(
4344
guards.push(stdout_guard);
4445

4546
// Setup file layer.
46-
let rolling_appender = RollingFileAppender::new(Rotation::DAILY, log_dir, name);
47+
let rolling_appender = RollingFileAppender::builder()
48+
.rotation(Rotation::HOURLY)
49+
.filename_prefix(name)
50+
.max_log_files(log_max_files)
51+
.build(log_dir)
52+
.expect("failed to create rolling file appender");
53+
4754
let (rolling_writer, rolling_writer_guard) = tracing_appender::non_blocking(rolling_appender);
4855
let file_logging_layer = Layer::new()
4956
.with_writer(rolling_writer)

0 commit comments

Comments
 (0)