Skip to content

Commit 8cdaa9b

Browse files
committed
Auto merge of #2064 - jtgeibel:prod/interlaced-logs, r=carols10cents
Consolidate logging into a single `println!()` In production I observed serveral log lines with only " SLOW REQUEST". Occasionally another thread will obtain the lock on stdout between our calls to `print!()`. This implementation consolidates the entire log line into a single `println!()` and avoids an allocation unless an error is being logged. r? @smarnach
2 parents 2c99e22 + e8dd729 commit 8cdaa9b

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/middleware/log_request.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,23 @@ impl Handler for LogRequests {
3636
.find::<u64>()
3737
.map_or(String::new(), |l| format!(" metadata_length={}", l));
3838

39-
print!(
39+
let slow_request = if response_time > 1000 {
40+
" SLOW REQUEST"
41+
} else {
42+
""
43+
};
44+
45+
let error = if let Err(ref e) = res {
46+
format!(" error=\"{}\"", e)
47+
} else {
48+
String::new()
49+
};
50+
51+
println!(
4052
"at={level} method={method} path=\"{path}\" \
4153
request_id={request_id} fwd=\"{ip}\" service={time_ms}ms \
4254
status={status} user_agent=\"{user_agent}\"\
43-
{metadata_length}",
55+
{metadata_length}{error}{slow_request}",
4456
level = level,
4557
method = req.method(),
4658
path = FullPath(req),
@@ -50,18 +62,10 @@ impl Handler for LogRequests {
5062
request_id = request_header(req, "X-Request-Id"),
5163
status = response_code,
5264
metadata_length = metadata_length,
65+
error = error,
66+
slow_request = slow_request,
5367
);
5468

55-
if let Err(ref e) = res {
56-
print!(" error=\"{}\"", e.to_string());
57-
}
58-
59-
if response_time > 1000 {
60-
print!(" SLOW REQUEST");
61-
}
62-
63-
println!();
64-
6569
res
6670
}
6771
}

0 commit comments

Comments
 (0)