Skip to content

Commit

Permalink
add tracing_journald support for linux (#407)
Browse files Browse the repository at this point in the history
all comtrya log

```
$ journalctl -t comtrya
```

filter by manifest
```
$ journalctl F_MANIFEST=github
```

filter by action
```
$ journalctl F_ACTION=github.binary
```
  • Loading branch information
shawn111 committed Mar 27, 2024
1 parent df829d0 commit f56cfc5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rhai = { version = "1.17", features = ["serde"] }
strip-ansi-escapes = "0.2"
structopt = { version = "0.3", features = ["paw"] }
tracing = "0.1"
tracing-journald = "0.3.0"
tracing-subscriber = "0.3"
update-informer = "1.1"

Expand Down
34 changes: 22 additions & 12 deletions app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::io;

use commands::ComtryaCommand;

use comtrya_lib::contexts::build_contexts;
use comtrya_lib::contexts::Contexts;
use comtrya_lib::manifests;
use structopt::StructOpt;
use tracing::{error, Level, Subscriber};
use tracing_subscriber::FmtSubscriber;
use tracing::{error, Level};
use tracing_subscriber::{fmt::writer::MakeWriterExt, layer::SubscriberExt, FmtSubscriber};

mod commands;
mod config;
Expand Down Expand Up @@ -58,26 +60,34 @@ pub(crate) fn execute(runtime: Runtime) -> anyhow::Result<()> {
}
}

fn configure_subscriber(args: &GlobalArgs) -> impl Subscriber {
fn configure_tracing(args: &GlobalArgs) {
let stdout_writer = match args.verbose {
0 => io::stdout.with_max_level(tracing::Level::INFO),
1 => io::stdout.with_max_level(tracing::Level::DEBUG),
_ => io::stdout.with_max_level(tracing::Level::TRACE),
};

let builder = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.with_max_level(Level::TRACE)
.with_ansi(!args.no_color)
.with_target(false)
.with_writer(stdout_writer)
.without_time();

match args.verbose {
0 => builder,
1 => builder.with_max_level(Level::DEBUG),
_ => builder.with_max_level(Level::TRACE),
#[cfg(target_os = "linux")]
if let Ok(layer) = tracing_journald::layer() {
tracing::subscriber::set_global_default(builder.finish().with(layer))
.expect("Unable to set a global subscriber");
return;
}
.finish()

tracing::subscriber::set_global_default(builder.finish())
.expect("Unable to set a global subscriber");
}

#[paw::main]
fn main(args: GlobalArgs) -> anyhow::Result<()> {
let subscriber = configure_subscriber(&args);

tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
configure_tracing(&args);

let config = match load_config(args.clone()) {
Ok(config) => config,
Expand Down

0 comments on commit f56cfc5

Please sign in to comment.