Skip to content

Commit 7a85ac3

Browse files
ffuugootimvisee
authored andcommitted
Replace env_logger with tracing (#2381)
* Replace `env_logger` with `tracing`/`tracing-subscriber`/`tracing-log` TODO: - Implement slog drain that would *properly* convert slog records into tracing events (`slog-stdlog` is fine for now, though) * fixup! Replace `env_logger` with `tracing`/`tracing-subscriber`/`tracing-log` Fix `consensus` test * fixup! fixup! Replace `env_logger` with `tracing`/`tracing-subscriber`/`tracing-log` Forget to `unwrap` the result 🙈 * fixup! fixup! Replace `env_logger` with `tracing`/`tracing-subscriber`/`tracing-log` `tracing_subscriber::fmt::init` initializes `tracing_log::LogTracer` automatically with default `tracing-subscriber` features enabled 💁‍♀️ * Update `DEVELOPMENT.md` documentation regarding `tracing` * Update default log level for development to make it less noisy --------- Co-authored-by: timvisee <[email protected]>
1 parent 98e6f44 commit 7a85ac3

File tree

8 files changed

+54
-120
lines changed

8 files changed

+54
-120
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-17
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,15 @@ multiling-japanese = ["segment/multiling-japanese"]
2020
multiling-korean = ["segment/multiling-korean"]
2121
service_debug = ["parking_lot", "parking_lot/deadlock_detection"]
2222
tracing = [
23-
"dep:tracing",
2423
"api/tracing",
2524
"collection/tracing",
2625
"segment/tracing",
2726
"storage/tracing",
2827
]
29-
tracing-log = ["tracing/log"]
30-
tracing-log-always = ["tracing/log-always"]
31-
tracing-logger = [
32-
"tracing",
33-
"tracing-subscriber/ansi",
34-
"tracing-subscriber/env-filter",
35-
"tracing-subscriber/fmt",
36-
"tracing-subscriber/tracing-log",
37-
"dep:tracing-log",
38-
]
3928
console = ["console-subscriber"]
40-
console-subscriber = ["tracing", "tracing-subscriber", "dep:console-subscriber"]
29+
console-subscriber = ["tracing", "dep:console-subscriber"]
4130
tracy = ["tracing-tracy"]
42-
tracing-tracy = ["tracing", "tracing-subscriber", "dep:tracing-tracy"]
31+
tracing-tracy = ["tracing", "dep:tracing-tracy"]
4332
tokio-tracing = ["tokio/tracing"]
4433

4534
[dev-dependencies]
@@ -57,7 +46,6 @@ parking_lot = { version = "0.12.1", features=["deadlock_detection"], optional =
5746
num_cpus = "1.16"
5847
thiserror = "1.0"
5948
log = "0.4"
60-
env_logger = "0.10.0"
6149
atty = "0.2"
6250
colored = "2"
6351
serde = { version = "~1.0", features = ["derive"] }
@@ -108,9 +96,9 @@ actix-multipart = "0.6.0"
10896
constant_time_eq = "0.3.0"
10997

11098
# Profiling
111-
tracing = { version = "0.1", features = ["async-await"], optional = true }
112-
tracing-subscriber = { version = "0.3", default-features = false, features = ["registry", "std"], optional = true }
113-
tracing-log = { version = "0.1", default-features = false, features = ["log-tracer", "std"], optional = true }
99+
tracing = { version = "0.1", features = ["async-await"] }
100+
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
101+
tracing-log = { version = "0.1", default-features = false, features = ["log-tracer", "std"] }
114102
console-subscriber = { version = "0.1", default-features = false, features = ["parking_lot"], optional = true }
115103
tracing-tracy = { version = "0.10.2", features = ["ondemand"], optional = true }
116104

config/development.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
debug: true
2-
log_level: DEBUG
2+
log_level: DEBUG,hyper=INFO,h2=INFO,tower=INFO,rustls=INFO,raft=INFO
33

44
service:
55
host: 127.0.0.1

docs/DEVELOPMENT.md

+9-17
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,7 @@ Use [pprof](https://github.com/google/pprof) and the following command to genera
140140
Qdrant have basic [`tracing`] support with [`Tracy`] profiler and [`tokio-console`] integrations
141141
that can be enabled with optional features.
142142

143-
- `tracing-log` and `tracing-log-always` features enable [`tracing/log` and `tracing/log-always`][tracing-log-features]
144-
features of the `tracing` crate, which cause `tracing` crate to produce logs with the `log` crate
145-
- if `tracing-log` feature enabled, the logs would only be produced if no other tracing subscriber is enabled
146-
(e.g., if neither `tracy`, nor `console` feature is enabled)
147-
- if `tracing-log-always` feature is enabled, the logs would _always_ be produced
148-
- useful for "quick and dirty" `tracing`-enabled debugging, that does not require any additional setup
149-
- `tracing-logger` feature replaces default logger with an alternative "tracing-compatible" logger,
150-
that also prints [tracing span] information for `tracing` logs (which is _super-useful_ in times)
151-
- note, that `tracing-logger` only reads "filters" from default `RUST_LOG` env-var currently
152-
- note, that if both `tracing-log-always` and `tracing-logger` features are enabled it will cause some logs
153-
to be printed twice
143+
- [`tracing`] is an _optional_ dependency that can be enabled with `tracing` feature
154144
- `tracy` feature enables [`Tracy`] profiler integration
155145
- `console` feature enables [`tokio-console`] integration
156146
- note, that you'll also have to [pass `--cfg tokio_unstable` arguments to `rustc`][tokio-tracing] to enable this feature
@@ -162,15 +152,16 @@ that can be enabled with optional features.
162152
- note, that you'll also have to [pass `--cfg tokio_unstable` arguments to `rustc`][tokio-tracing] to enable this feature
163153
- this is required (and enabled automatically) by the `console` feature
164154
- but you can enable it explicitly with the `tracy` feature, to see Tokio traces in [`Tracy`] profiler
165-
- `tracing` feature simply enables optional [`tracing`] crate dependency (without any integrations)
166-
- this is required (and enabled automatically) by all of the above features
167155

168156
Qdrant code is **not** instrumented by default, so you'll have to manually add `#[tracing::instrument]` attributes
169157
on functions and methods that you want to profile.
170158
159+
Qdrant uses [`tracing-log`] as the [`log`] backend, so `log` and `log-always` features of the [`tracing`] crate
160+
[should _not_ be enabled][tracing-log-warning]!
161+
171162
```rust
172-
// `tracing` crate is an *optional* dependency, so if you want the code to compile when `tracing`
173-
// feature is disabled, you have to use `#[cfg_attr(...)]`...
163+
// `tracing` crate is an *optional* dependency in `lib/*` crates, so if you want the code to compile
164+
// when `tracing` feature is disabled, you have to use `#[cfg_attr(...)]`...
174165
//
175166
// See https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute
176167
#[cfg_attr(feature = "tracing", tracing::instrument)]
@@ -187,12 +178,13 @@ fn some_other_function() {
187178
}
188179
```
189180
190-
[tracing-log-features]: https://docs.rs/tracing/latest/tracing/#emitting-log-records
191-
[tracing span]: https://docs.rs/tracing/latest/tracing/index.html#spans
192181
[`tracing`]: https://docs.rs/tracing/latest/tracing/
193182
[`Tracy`]: https://github.com/wolfpld/tracy
194183
[`tokio-console`]: https://docs.rs/tokio-console/latest/tokio_console/
195184
[tokio-tracing]: https://docs.rs/tokio/latest/tokio/#unstable-features
185+
[`tracing-log`]: https://docs.rs/tracing-log/latest/tracing_log/
186+
[`log`]: https://docs.rs/log/latest/log/
187+
[tracing-log-warning]: https://docs.rs/tracing-log/latest/tracing_log/#caution-mixing-both-conversions
196188
197189
## API changes
198190

src/consensus.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ mod tests {
11391139
let mut settings = crate::Settings::new(None).expect("Can't read config.");
11401140
settings.storage.storage_path = storage_dir.path().to_str().unwrap().to_string();
11411141
std::env::set_var("RUST_LOG", log::Level::Debug.as_str());
1142-
env_logger::init();
1142+
tracing_subscriber::fmt::init();
11431143
let search_runtime =
11441144
crate::create_search_runtime(settings.storage.performance.max_search_threads)
11451145
.expect("Can't create search runtime.");

src/main.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::greeting::welcome;
4343
use crate::migrations::single_to_cluster::handle_existing_collections;
4444
use crate::settings::Settings;
4545
use crate::snapshots::{recover_full_snapshot, recover_snapshots};
46-
use crate::startup::{remove_started_file_indicator, setup_logger, touch_started_file_indicator};
46+
use crate::startup::{remove_started_file_indicator, touch_started_file_indicator};
4747

4848
#[cfg(not(target_env = "msvc"))]
4949
#[global_allocator]
@@ -116,11 +116,7 @@ fn main() -> anyhow::Result<()> {
116116

117117
let reporting_id = TelemetryCollector::generate_id();
118118

119-
if cfg!(feature = "tracing-logger") {
120-
tracing::setup(&settings.log_level)?;
121-
} else {
122-
setup_logger(&settings.log_level);
123-
}
119+
tracing::setup(&settings.log_level)?;
124120

125121
setup_panic_hook(reporting_enabled, reporting_id.to_string());
126122

src/startup.rs

-26
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use std::backtrace::Backtrace;
44
use std::panic;
55
use std::path::PathBuf;
66

7-
use log::LevelFilter;
8-
97
use crate::common::error_reporting::ErrorReporter;
108

119
const DEFAULT_INITIALIZED_FILE: &str = ".qdrant-initialized";
@@ -16,30 +14,6 @@ fn get_init_file_path() -> PathBuf {
1614
.unwrap_or_else(|_| DEFAULT_INITIALIZED_FILE.into())
1715
}
1816

19-
pub fn setup_logger(log_level: &str) {
20-
let is_info = log_level.to_ascii_uppercase() == "INFO";
21-
let mut log_builder = env_logger::Builder::new();
22-
23-
log_builder
24-
// Timestamp in millis
25-
.format_timestamp_millis()
26-
// Parse user defined log level configuration
27-
.parse_filters(log_level)
28-
// h2 is very verbose and we have many network operations,
29-
// so it is limited to only errors
30-
.filter_module("h2", LevelFilter::Error)
31-
.filter_module("tower", LevelFilter::Warn);
32-
33-
if is_info {
34-
// Additionally filter verbose modules if no extended logging configuration is provided
35-
log_builder
36-
.filter_module("wal", LevelFilter::Warn)
37-
.filter_module("raft::raft", LevelFilter::Warn);
38-
};
39-
40-
log_builder.init();
41-
}
42-
4317
pub fn setup_panic_hook(reporting_enabled: bool, reporting_id: String) {
4418
panic::set_hook(Box::new(move |panic_info| {
4519
let backtrace = Backtrace::force_capture().to_string();

src/tracing.rs

+36-51
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
1-
#[cfg_attr(not(feature = "tracing-logger"), allow(unused_variables))]
2-
pub fn setup(log_level: &str) -> anyhow::Result<()> {
3-
// Use `console` and/or `tracy` features to enable both `tracing-subscriber` and the layer(s)
4-
#[cfg(feature = "tracing-subscriber")]
5-
{
6-
use tracing_subscriber::prelude::*;
7-
8-
let reg = tracing_subscriber::registry();
9-
10-
// Use `console` feature to enable both `tracing-subscriber` and `console-subscriber`
11-
#[cfg(feature = "console-subscriber")]
12-
let reg = reg.with(console_subscriber::spawn());
13-
14-
// Note, that `console-subscriber` requires manually enabling
15-
// `--cfg tokio_unstable` rust flags during compilation!
16-
//
17-
// Otherwise `console_subscriber::spawn` call panics!
18-
//
19-
// See https://docs.rs/tokio/latest/tokio/#unstable-features
20-
#[cfg(all(feature = "console-subscriber", not(tokio_unstable)))]
21-
eprintln!(
22-
"`console-subscriber` requires manually enabling \
23-
`--cfg tokio_unstable` rust flags during compilation!"
24-
);
25-
26-
// Use `tracy` feature to enable both `tracing-subscriber` and `tracing-tracy`
27-
#[cfg(feature = "tracing-tracy")]
28-
let reg = reg.with(tracing_tracy::TracyLayer::new().with_filter(
29-
tracing_subscriber::filter::filter_fn(|metadata| metadata.is_span()),
30-
));
1+
use tracing_subscriber::prelude::*;
2+
use tracing_subscriber::{filter, fmt};
313

32-
#[cfg(all(feature = "tracing-log-always", feature = "tracing-logger"))]
33-
eprintln!(
34-
"Both `tracing-log-always` and `tracing-logger` features are enabled at the same time. \
35-
This will cause some logs to be printed twice!"
36-
);
37-
38-
#[cfg(feature = "tracing-logger")]
39-
let reg = reg.with(
40-
tracing_subscriber::fmt::layer()
41-
.with_ansi(true)
42-
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::NEW)
43-
.with_filter(
44-
tracing_subscriber::filter::EnvFilter::builder().parse_lossy(log_level),
45-
),
46-
);
47-
48-
#[cfg(feature = "tracing-logger")]
49-
tracing_log::LogTracer::init()?;
50-
51-
tracing::subscriber::set_global_default(reg)?;
52-
}
4+
pub fn setup(log_level: &str) -> anyhow::Result<()> {
5+
tracing_log::LogTracer::init()?;
6+
7+
let reg = tracing_subscriber::registry().with(
8+
fmt::layer()
9+
.with_ansi(true)
10+
.with_span_events(fmt::format::FmtSpan::NEW)
11+
.with_filter(filter::EnvFilter::builder().parse_lossy(log_level)),
12+
);
13+
14+
// Use `console` or `console-subscriber` feature to enable `console-subscriber`
15+
//
16+
// Note, that `console-subscriber` requires manually enabling
17+
// `--cfg tokio_unstable` rust flags during compilation!
18+
//
19+
// Otherwise `console_subscriber::spawn` call panics!
20+
//
21+
// See https://docs.rs/tokio/latest/tokio/#unstable-features
22+
#[cfg(all(feature = "console-subscriber", tokio_unstable))]
23+
let reg = reg.with(console_subscriber::spawn());
24+
25+
#[cfg(all(feature = "console-subscriber", not(tokio_unstable)))]
26+
eprintln!(
27+
"`console-subscriber` requires manually enabling \
28+
`--cfg tokio_unstable` rust flags during compilation!"
29+
);
30+
31+
// Use `tracy` or `tracing-tracy` feature to enable `tracing-tracy`
32+
#[cfg(feature = "tracing-tracy")]
33+
let reg = reg.with(tracing_tracy::TracyLayer::new().with_filter(
34+
tracing_subscriber::filter::filter_fn(|metadata| metadata.is_span()),
35+
));
36+
37+
tracing::subscriber::set_global_default(reg)?;
5338

5439
Ok(())
5540
}

0 commit comments

Comments
 (0)