Skip to content

Commit 17272bc

Browse files
mantonoclaude
andcommitted
feat: Add JSON log format for proper Datadog parsing
Configure env_logger to output structured JSON logs with timestamp, level, target, and message fields. This ensures Datadog correctly parses and categorizes log levels instead of treating all logs as a single severity level. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5734b31 commit 17272bc

File tree

3 files changed

+217
-1
lines changed

3 files changed

+217
-1
lines changed

Cargo.lock

Lines changed: 204 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ tokio = { version = "1", features = ["full"] }
1414
axum = { version = "0.6.18", features = ["headers", "http1", "json", "tokio", "query"] }
1515
log = { version = "0.4", optional = true }
1616
env_logger = { version = "0.11.8", optional = true }
17+
chrono = { version = "0.4", optional = true }
1718
clap = { version = "4.5.53", features = ["color", "derive", "help", "usage", "std", "env"] }
1819
redis = { version = "0.25.4", features = [ "ahash", "aio", "tokio-comp" ] }
1920
sha2 = "0.10.9"
@@ -23,5 +24,5 @@ tower = "0.4"
2324

2425
[features]
2526
default = ["logging"]
26-
logging = ["dep:log", "dep:env_logger"]
27+
logging = ["dep:log", "dep:env_logger", "dep:chrono"]
2728
redis = []

src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ async fn main() {
2121
env_logger::Builder::new()
2222
.target(env_logger::Target::Stdout)
2323
.filter_level(cfg.log_level().to_level_filter())
24+
.format(|buf, record| {
25+
use std::io::Write;
26+
writeln!(
27+
buf,
28+
"{{\"timestamp\":\"{}\",\"level\":\"{}\",\"target\":\"{}\",\"message\":\"{}\"}}",
29+
chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
30+
record.level(),
31+
record.target(),
32+
record.args()
33+
)
34+
})
2435
.init();
2536

2637
if cfg.api_key().is_none() {

0 commit comments

Comments
 (0)