i am trying to using tracing & tracing_subscriber with axum
it's my code snippet
main function:
fn main() -> Result<(), io::Error> {
let rt = runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()?;
tracing_subscriber::fmt().event_format(format().json()).init();
if let Err(e) = rt.block_on(axum_main()) {
e.source();
}
Ok(())
}
and the function of axum_main()
let app: Router<()> = Router::new().route(
"/",
routing::get(|| {
async {
tracing::info!("wowo here");
"HELLO,world"
}.instrument(tracing::info_span!("hello_span"))
}),
);
let addr: SocketAddr = "192.168.42.200:9090".parse().unwrap();
let socket = TcpSocket::new_v4()?;
#[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
{
socket.set_reuseport(true)?;
socket.set_reuseaddr(true)?;
}
socket.bind(addr)?;
let listener = socket.listen(1024)?;
let r = axum::serve(listener, app.into_make_service()).await?;
Ok(r)
it's really a simple code, but when i use cargo run
, and visit http://192.168.42.200:9090/
it report a panic with:
thread 'main' panicked at /root/.cargo/registry/src/rsproxy-e3de039b2554c837/tracing-subscriber-0.3.19/src/fmt/format/json.rs:197:49:
span 'hello_span' had malformed fields! this is a bug.
error: EOF while parsing a value at line 1 column 0
fields: FormattedFields { fields: "", formatter: tracing_subscriber::fmt::format::DefaultFields, was_ansi: true }
stack backtrace:
0: rust_begin_unwind
at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/panicking.rs:692:5
1: core::panicking::panic_fmt
at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/panicking.rs:75:14
2: <tracing_subscriber::fmt::format::json::SerializableSpan<Span,N> as serde::ser::Serialize>::serialize
at /root/.cargo/registry/src/rsproxy-e3de039b2554c837/tracing-subscriber-0.3.19/src/fmt/format/json.rs:197:49
3: <serde_json::ser::Compound<W,F> as serde::ser::SerializeMap>::serialize_value
The Goal: How can i resolve it (i want my log event output as json)? or Am I using it improperly?
when i switch this line:
tracing_subscriber::fmt().event_format(format().json()).init()
totracing_subscriber::fmt().event_format(format()pact()).init()
everythings works well
appendix:
my Cargo.toml
dependencies:
[dependencies]
axum = "0.8.1"
tokio = { version = "1.44.1", features = ["macros", "rt"] }
tracing = "0.1.41"
tracing-subscriber = {version = "0.3.19",features = ["env-filter","chrono","json"]}
i am trying to using tracing & tracing_subscriber with axum
it's my code snippet
main function:
fn main() -> Result<(), io::Error> {
let rt = runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()?;
tracing_subscriber::fmt().event_format(format().json()).init();
if let Err(e) = rt.block_on(axum_main()) {
e.source();
}
Ok(())
}
and the function of axum_main()
let app: Router<()> = Router::new().route(
"/",
routing::get(|| {
async {
tracing::info!("wowo here");
"HELLO,world"
}.instrument(tracing::info_span!("hello_span"))
}),
);
let addr: SocketAddr = "192.168.42.200:9090".parse().unwrap();
let socket = TcpSocket::new_v4()?;
#[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
{
socket.set_reuseport(true)?;
socket.set_reuseaddr(true)?;
}
socket.bind(addr)?;
let listener = socket.listen(1024)?;
let r = axum::serve(listener, app.into_make_service()).await?;
Ok(r)
it's really a simple code, but when i use cargo run
, and visit http://192.168.42.200:9090/
it report a panic with:
thread 'main' panicked at /root/.cargo/registry/src/rsproxy.cn-e3de039b2554c837/tracing-subscriber-0.3.19/src/fmt/format/json.rs:197:49:
span 'hello_span' had malformed fields! this is a bug.
error: EOF while parsing a value at line 1 column 0
fields: FormattedFields { fields: "", formatter: tracing_subscriber::fmt::format::DefaultFields, was_ansi: true }
stack backtrace:
0: rust_begin_unwind
at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/panicking.rs:692:5
1: core::panicking::panic_fmt
at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/panicking.rs:75:14
2: <tracing_subscriber::fmt::format::json::SerializableSpan<Span,N> as serde::ser::Serialize>::serialize
at /root/.cargo/registry/src/rsproxy.cn-e3de039b2554c837/tracing-subscriber-0.3.19/src/fmt/format/json.rs:197:49
3: <serde_json::ser::Compound<W,F> as serde::ser::SerializeMap>::serialize_value
The Goal: How can i resolve it (i want my log event output as json)? or Am I using it improperly?
when i switch this line:
tracing_subscriber::fmt().event_format(format().json()).init()
totracing_subscriber::fmt().event_format(format()pact()).init()
everythings works well
appendix:
my Cargo.toml
dependencies:
[dependencies]
axum = "0.8.1"
tokio = { version = "1.44.1", features = ["macros", "rt"] }
tracing = "0.1.41"
tracing-subscriber = {version = "0.3.19",features = ["env-filter","chrono","json"]}
Share
Improve this question
edited Mar 21 at 8:30
Stop
asked Mar 21 at 8:13
StopStop
133 bronze badges
1 Answer
Reset to default 0Use this instead:
tracing_subscriber::fmt().json().init();
From this issue, the panic "is an unfortunate consequence of having separate field and event formatters". The JSON event formatter is expecting the JSON fields to be formatted a specific way, but you didn't have the matching .fmt_fields(...)
. The above does that all in one.