Skip to content

Commit

Permalink
Merge pull request loco-rs#470 from loco-rs/logger-span
Browse files Browse the repository at this point in the history
improve app logger by adding span to root app and task with details
  • Loading branch information
jondot authored Mar 4, 2024
2 parents 685f84f + 00e60dc commit 822af84
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
18 changes: 9 additions & 9 deletions examples/demo/tests/cmd/cli.trycmd
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ $ LOCO_ENV=test blo-cli db reset

```console
$ LOCO_ENV=teste2e blo-cli db migrate
[[..]  INFO loco_rs::config: loading environment from selected_path="config/[..].yaml"
[[..]  WARN loco_rs::boot: migrate:
[[..]  INFO sea_orm_migration::migrator: Applying all pending migrations
[[..]  INFO sea_orm_migration::migrator: No pending migrations
[..][0m  INFO app: loco_rs::config: loading environment from selected_path="config/teste2e.yaml" environment=teste2e
[..][0m  WARN app: loco_rs::boot: migrate: environment=teste2e
[..][0m  INFO app: sea_orm_migration::migrator: Applying all pending migrations environment=teste2e
[..][0m  INFO app: sea_orm_migration::migrator: No pending migrations environment=teste2e

```

```console
$ LOCO_ENV=teste2e blo-cli db status
[[..]  INFO loco_rs::config: loading environment from selected_path="config/[..].yaml"
[[..]  WARN loco_rs::boot: status:
[[..]  INFO sea_orm_migration::migrator: Checking migration status
[[..]  INFO sea_orm_migration::migrator: Migration 'm20220101_000001_users'... Applied
[[..]  INFO sea_orm_migration::migrator: Migration 'm20231103_114510_notes'... Applied
[..][0m  INFO app: loco_rs::config: loading environment from selected_path="config/teste2e.yaml" environment=teste2e
[..][0m  WARN app: loco_rs::boot: status: environment=teste2e
[..][0m  INFO app: sea_orm_migration::migrator: Checking migration status environment=teste2e
[..][0m  INFO app: sea_orm_migration::migrator: Migration 'm20220101_000001_users'... Applied environment=teste2e
[..][0m  INFO app: sea_orm_migration::migrator: Migration 'm20231103_114510_notes'... Applied environment=teste2e

```
2 changes: 2 additions & 0 deletions src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub async fn run_task<H: Hooks>(
H::register_tasks(&mut tasks);

if let Some(task) = task {
let task_span = tracing::span!(tracing::Level::DEBUG, "task", task,);
let _guard = task_span.enter();
tasks.run(app_context, task, vars).await?;
} else {
let list = tasks.list();
Expand Down
12 changes: 11 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ pub async fn playground<H: Hooks>() -> Result<AppContext> {
/// ```
#[cfg(feature = "with-db")]
pub async fn main<H: Hooks, M: MigratorTrait>() -> eyre::Result<()> {
let cli = Cli::parse();
let cli: Cli = Cli::parse();
let environment: Environment = cli.environment.unwrap_or_else(resolve_from_env).into();

let config = environment.load()?;
Expand All @@ -292,6 +292,9 @@ pub async fn main<H: Hooks, M: MigratorTrait>() -> eyre::Result<()> {
logger::init::<H>(&config.logger);
}

let task_span = create_root_span(&environment);
let _guard = task_span.enter();

match cli.command {
Commands::Start {
worker,
Expand Down Expand Up @@ -371,6 +374,9 @@ pub async fn main<H: Hooks>() -> eyre::Result<()> {
logger::init::<H>(&config.logger);
}

let task_span = create_root_span(&environment);
let _guard = task_span.enter();

match cli.command {
Commands::Start {
worker,
Expand Down Expand Up @@ -425,3 +431,7 @@ fn show_list_endpoints<H: Hooks>(ctx: &AppContext) {
println!("{}", router.to_string());
}
}

fn create_root_span(environment: &Environment) -> tracing::Span {
tracing::span!(tracing::Level::DEBUG, "app", environment = %environment)
}

0 comments on commit 822af84

Please sign in to comment.