Skip to content

Commit

Permalink
feat(cli): logging support (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Oct 6, 2023
1 parent a2a5f4b commit 075b9c4
Show file tree
Hide file tree
Showing 185 changed files with 1,308 additions and 907 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom
- Add option `--javascript-formatter-indent-width`, and deprecated the option `--javascript-formatter-indent-size`. Contributed by @ematipico
- Add option `--json-formatter-indent-width`, and deprecated the option `--json-formatter-indent-size`. Contributed by @ematipico
- Add option `--daemon-logs` to `biome rage`. The option is required to view Biome daemon server logs. Contributed by @unvalley
- Add support for logging. By default, Biome doesn't log anything other than diagnostics. Logging can be enabled with the new option `--log-level`:

```shell
biome format --log-level=info ./src
```
There are four different levels of logging, from the most verbose to the least verbose: `debug`, `info`, `warn` and `error`. Here's how an `INFO` log will look like:

```
2023-10-05T08:27:01.954727Z INFO Analyze file ./website/src/playground/components/Resizable.tsx
at crates/biome_service/src/file_handlers/javascript.rs:298 on biome::worker_5
in Pulling diagnostics with categories: RuleCategories(SYNTAX)
in Processes formatting with path: "./website/src/playground/components/Resizable.tsx"
in Process check with path: "./website/src/playground/components/Resizable.tsx"
```

You can customize how the log will look like with a new option `--log-kind`. The supported kinds are: `pretty`, `compact` and `json`.

`pretty` is the default logging. Here's how a `compact` log will look like:

```
2023-10-05T08:29:04.864247Z INFO biome::worker_2 Process check:Processes linting:Pulling diagnostics: crates/biome_service/src/file_handlers/javascript.rs: Analyze file ./website/src/playground/components/Resizable.tsx path="./website/src/playground/components/Resizable.tsx" path="./website/src/playground/components/Resizable.tsx" categories=RuleCategories(LINT)
2023-10-05T08:29:04.864290Z INFO biome::worker_7 Process check:Processes formatting: crates/biome_service/src/file_handlers/javascript.rs: Format file ./website/src/playground/components/Tabs.tsx path="./website/src/playground/components/Tabs.tsx" path="./website/src/playground/components/Tabs.tsx"
2023-10-05T08:29:04.879332Z INFO biome::worker_2 Process check:Processes formatting:Pulling diagnostics: crates/biome_service/src/file_handlers/javascript.rs: Analyze file ./website/src/playground/components/Resizable.tsx path="./website/src/playground/components/Resizable.tsx" path="./website/src/playground/components/Resizable.tsx" categories=RuleCategories(SYNTAX)
2023-10-05T08:29:04.879383Z INFO biome::worker_2 Process check:Processes formatting: crates/biome_service/src/file_handlers/javascript.rs: Format file ./website/src/playground/components/Resizable.tsx path="./website/src/playground/components/Resizable.tsx" path="./website/src/playground/components/Resizable.tsx"
```

#### Enhancements

Expand Down
55 changes: 36 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/biome_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ serde_json = { workspace = true }
tokio = { workspace = true, features = ["io-std", "io-util", "net", "time", "rt", "sync", "rt-multi-thread", "macros"] }
tracing = { workspace = true }
tracing-appender = "0.2"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
tracing-subscriber = { version = "0.3.16", features = ["env-filter", "json"] }
tracing-tree = "0.2.2"

[target.'cfg(unix)'.dependencies]
Expand Down
22 changes: 22 additions & 0 deletions crates/biome_cli/src/cli_options.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::logging::LoggingKind;
use crate::LoggingLevel;
use bpaf::Bpaf;
use std::str::FromStr;

Expand Down Expand Up @@ -44,6 +46,26 @@ pub struct CliOptions {
/// Reports information using the JSON format
#[bpaf(long("json"), switch, hide_usage, hide)]
pub json: bool,

#[bpaf(
long("log-level"),
argument("none|debug|info|warn|error"),
fallback(LoggingLevel::default()),
display_fallback
)]
/// The level of logging. In order, from the most verbose to the least verbose: debug, info, warn, error.
///
/// The value `none` won't show any logging.
pub log_level: LoggingLevel,

/// How the log should look like.
#[bpaf(
long("log-kind"),
argument("pretty|compact|json"),
fallback(LoggingKind::default()),
display_fallback
)]
pub log_kind: LoggingKind,
}

#[derive(Debug, Clone)]
Expand Down
5 changes: 4 additions & 1 deletion crates/biome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::cli_options::CliOptions;
use crate::configuration::{load_configuration, LoadedConfiguration};
use crate::vcs::store_path_to_ignore_from_vcs;
use crate::{execute_mode, CliDiagnostic, CliSession, Execution, TraversalMode};
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
use biome_service::configuration::organize_imports::OrganizeImports;
use biome_service::configuration::{FormatterConfiguration, LinterConfiguration};
use biome_service::workspace::{FixFileMode, UpdateSettingsParams};
Expand Down Expand Up @@ -37,6 +39,7 @@ pub(crate) fn check(
organize_imports_enabled,
formatter_enabled,
} = payload;
setup_cli_subscriber(cli_options.log_level.clone(), cli_options.log_kind.clone());

let fix_file_mode = if apply && apply_unsafe {
return Err(CliDiagnostic::incompatible_arguments(
Expand Down
9 changes: 7 additions & 2 deletions crates/biome_cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::cli_options::CliOptions;
use crate::configuration::LoadedConfiguration;
use crate::vcs::store_path_to_ignore_from_vcs;
use crate::{
configuration::load_configuration, execute_mode, CliDiagnostic, CliSession, Execution,
TraversalMode,
configuration::load_configuration, execute_mode, setup_cli_subscriber, CliDiagnostic,
CliSession, Execution, TraversalMode,
};
use biome_service::configuration::organize_imports::OrganizeImports;
use biome_service::configuration::{FormatterConfiguration, LinterConfiguration};
Expand All @@ -22,6 +22,11 @@ pub(crate) struct CiCommandPayload {

/// Handler for the "ci" command of the Biome CLI
pub(crate) fn ci(mut session: CliSession, payload: CiCommandPayload) -> Result<(), CliDiagnostic> {
setup_cli_subscriber(
payload.cli_options.log_level.clone(),
payload.cli_options.log_kind.clone(),
);

let loaded_configuration =
load_configuration(&mut session, &payload.cli_options)?.with_file_path();

Expand Down
5 changes: 2 additions & 3 deletions crates/biome_cli/src/commands/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub(super) fn rome_log_dir() -> PathBuf {
/// - All spans and events at level debug in crates whose name starts with `biome`
struct LoggingFilter;

/// Tracing filter used for spans emitted by `rome*` crates
/// Tracing filter used for spans emitted by `biome*` crates
const SELF_FILTER: LevelFilter = if cfg!(debug_assertions) {
LevelFilter::TRACE
} else {
Expand All @@ -214,8 +214,7 @@ const SELF_FILTER: LevelFilter = if cfg!(debug_assertions) {

impl LoggingFilter {
fn is_enabled(&self, meta: &Metadata<'_>) -> bool {
// TODO: keep "rome" until all internal crates are moved to "biome_"
let filter = if meta.target().starts_with("rome") || meta.target().starts_with("biome") {
let filter = if meta.target().starts_with("biome") {
SELF_FILTER
} else {
LevelFilter::INFO
Expand Down
6 changes: 5 additions & 1 deletion crates/biome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::configuration::{load_configuration, LoadedConfiguration};
use crate::diagnostics::DeprecatedArgument;
use crate::execute::ReportMode;
use crate::vcs::store_path_to_ignore_from_vcs;
use crate::{execute_mode, CliDiagnostic, CliSession, Execution, TraversalMode};
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
use biome_console::{markup, ConsoleExt};
use biome_diagnostics::PrintDiagnostic;
use biome_service::configuration::json::JsonFormatter;
Expand Down Expand Up @@ -42,6 +44,8 @@ pub(crate) fn format(
write,
json_formatter,
} = payload;
setup_cli_subscriber(cli_options.log_level.clone(), cli_options.log_kind.clone());

let loaded_configuration = load_configuration(&mut session, &cli_options)?.with_file_path();

loaded_configuration.check_for_errors(session.app.console, cli_options.verbose)?;
Expand Down
5 changes: 4 additions & 1 deletion crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::cli_options::CliOptions;
use crate::configuration::{load_configuration, LoadedConfiguration};
use crate::vcs::store_path_to_ignore_from_vcs;
use crate::{execute_mode, CliDiagnostic, CliSession, Execution, TraversalMode};
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
use biome_service::configuration::vcs::VcsConfiguration;
use biome_service::configuration::{FilesConfiguration, LinterConfiguration};
use biome_service::workspace::{FixFileMode, UpdateSettingsParams};
Expand Down Expand Up @@ -35,6 +37,7 @@ pub(crate) fn lint(
vcs_configuration,
files_configuration,
} = payload;
setup_cli_subscriber(cli_options.log_level.clone(), cli_options.log_kind.clone());

let fix_file_mode = if apply && apply_unsafe {
return Err(CliDiagnostic::incompatible_arguments(
Expand Down
4 changes: 3 additions & 1 deletion crates/biome_cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::cli_options::CliOptions;
use crate::configuration::{load_configuration, LoadedConfiguration};
use crate::diagnostics::MigrationDiagnostic;
use crate::execute::{execute_mode, Execution, TraversalMode};
use crate::{CliDiagnostic, CliSession};
use crate::{setup_cli_subscriber, CliDiagnostic, CliSession};

/// Handler for the "check" command of the Biome CLI
pub(crate) fn migrate(
Expand All @@ -16,6 +16,8 @@ pub(crate) fn migrate(
directory_path,
file_path,
} = load_configuration(&mut session, &cli_options)?;
setup_cli_subscriber(cli_options.log_level.clone(), cli_options.log_kind.clone());

if let (Some(path), Some(directory_path)) = (file_path, directory_path) {
execute_mode(
Execution::new(TraversalMode::Migrate {
Expand Down
38 changes: 37 additions & 1 deletion crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::cli_options::{cli_options, CliOptions, ColorsArg};
use crate::VERSION;
use crate::logging::LoggingKind;
use crate::{LoggingLevel, VERSION};
use biome_service::configuration::json::JsonFormatter;
use biome_service::configuration::vcs::VcsConfiguration;
use biome_service::configuration::{
Expand Down Expand Up @@ -258,4 +259,39 @@ impl BiomeCommand {
BiomeCommand::PrintSocket => false,
}
}

pub fn log_level(&self) -> LoggingLevel {
match self {
BiomeCommand::Check { cli_options, .. }
| BiomeCommand::Lint { cli_options, .. }
| BiomeCommand::Format { cli_options, .. }
| BiomeCommand::Ci { cli_options, .. }
| BiomeCommand::LspProxy(cli_options)
| BiomeCommand::Migrate(cli_options, _) => cli_options.log_level.clone(),
BiomeCommand::Version(_)
| BiomeCommand::Rage(..)
| BiomeCommand::Start
| BiomeCommand::Stop
| BiomeCommand::Init
| BiomeCommand::RunServer { .. }
| BiomeCommand::PrintSocket => LoggingLevel::default(),
}
}
pub fn log_kind(&self) -> LoggingKind {
match self {
BiomeCommand::Check { cli_options, .. }
| BiomeCommand::Lint { cli_options, .. }
| BiomeCommand::Format { cli_options, .. }
| BiomeCommand::Ci { cli_options, .. }
| BiomeCommand::LspProxy(cli_options)
| BiomeCommand::Migrate(cli_options, _) => cli_options.log_kind.clone(),
BiomeCommand::Version(_)
| BiomeCommand::Rage(..)
| BiomeCommand::Start
| BiomeCommand::Stop
| BiomeCommand::Init
| BiomeCommand::RunServer { .. }
| BiomeCommand::PrintSocket => LoggingKind::default(),
}
}
}
Loading

0 comments on commit 075b9c4

Please sign in to comment.