Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: Use info log level for crates named zksync_* by default (matter…
Browse files Browse the repository at this point in the history
…-labs#2296)

## What ❔

Makes `zksync=info` default log entry, which later can be
overridden/extended.

## Why ❔

Provides a good enough default for any env, simplifies configuration of
the system.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
  • Loading branch information
popzxc authored Jun 21, 2024
1 parent 6fd4258 commit 9303142
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
4 changes: 3 additions & 1 deletion core/bin/block_reverter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ async fn main() -> anyhow::Result<()> {
.parse()
.context("Invalid log format")?;

let mut builder = vlog::ObservabilityBuilder::new().with_log_format(log_format);
let mut builder = vlog::ObservabilityBuilder::new()
.with_log_format(log_format)
.disable_default_logs(); // It's a CLI application, so we only need to show logs that were actually requested.
if let Some(sentry_url) = observability_config.sentry_url {
builder = builder
.with_sentry_url(&sentry_url)
Expand Down
42 changes: 36 additions & 6 deletions core/lib/vlog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub struct OpenTelemetryOptions {
/// Currently capable of configuring logging output and sentry integration.
#[derive(Debug, Default)]
pub struct ObservabilityBuilder {
disable_default_logs: bool,
log_format: LogFormat,
log_directives: Option<String>,
sentry_url: Option<Dsn>,
Expand Down Expand Up @@ -176,6 +177,14 @@ impl ObservabilityBuilder {
self
}

/// Disables logs enabled by default.
/// May be used, for example, in interactive CLI applications, where the user may want to fully control
/// the verbosity.
pub fn disable_default_logs(mut self) -> Self {
self.disable_default_logs = true;
self
}

/// Enables Sentry integration.
/// Returns an error if the provided Sentry URL is invalid.
pub fn with_sentry_url(
Expand Down Expand Up @@ -254,15 +263,36 @@ impl ObservabilityBuilder {
subscriber.with(layer)
}

/// Builds a filter for the logs.
///
/// Unless `disable_default_logs` was set, uses `zksync=info` as a default which is then merged
/// with user-defined directives. Provided directives can extend/override the default value.
///
/// The provided default convers all the crates with a name starting with `zksync` (per `tracing`
/// [documentation][1]), which is a good enough default for any project.
///
/// If `log_directives` are provided via `with_log_directives`, they will be used.
/// Otherwise, the value will be parsed from the environment variable `RUST_LOG`.
///
/// [1]: https://docs.rs/tracing-subscriber/0.3.18/tracing_subscriber/filter/targets/struct.Targets.html#filtering-with-targets
fn build_filter(&self) -> EnvFilter {
let mut directives = if self.disable_default_logs {
"".to_string()
} else {
"zksync=info,".to_string()
};
if let Some(log_directives) = &self.log_directives {
directives.push_str(log_directives);
} else if let Ok(env_directives) = std::env::var(EnvFilter::DEFAULT_ENV) {
directives.push_str(&env_directives);
};
EnvFilter::new(directives)
}

/// Initializes the observability subsystem.
pub fn build(self) -> ObservabilityGuard {
// Initialize logs.

let env_filter = if let Some(log_directives) = self.log_directives {
tracing_subscriber::EnvFilter::new(log_directives)
} else {
tracing_subscriber::EnvFilter::from_default_env()
};
let env_filter = self.build_filter();

match self.log_format {
LogFormat::Plain => {
Expand Down

0 comments on commit 9303142

Please sign in to comment.