Skip to content

Commit

Permalink
Complete (but messy) example of switching all tracing output to JSON.
Browse files Browse the repository at this point in the history
Overrriding default tracing cfg [WIP]

Added missing Tracing component
  • Loading branch information
adizere committed Jan 14, 2021
1 parent 90b885f commit c65bcef
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
21 changes: 18 additions & 3 deletions relayer-cli/src/application.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Cli Abscissa Application
use crate::{commands::CliCmd, config::Config};
use abscissa_core::{
application::{self, AppCell},
config, trace, Application, EntryPoint, FrameworkError, StandardPaths,
component::Component,
Application, config, EntryPoint, FrameworkError, StandardPaths, trace,
};

use crate::{commands::CliCmd, config::Config, components::Tracing};

/// Application state
pub static APPLICATION: AppCell<CliApp> = AppCell::new();

Expand Down Expand Up @@ -76,13 +78,26 @@ impl Application for CliApp {
&mut self.state
}

fn framework_components(
&mut self,
command: &Self::Cmd,
) -> Result<Vec<Box<dyn Component<Self>>>, FrameworkError> {
Ok(vec![])
}

/// Register all components used by this application.
///
/// If you would like to add additional components to your application
/// beyond the default ones provided by the framework, this is the place
/// to do so.
fn register_components(&mut self, command: &Self::Cmd) -> Result<(), FrameworkError> {
let components = self.framework_components(command)?;
let mut components = self.framework_components(command)?;

tracing::info!("pre-push");
let c = Tracing::new()?;
components.push(Box::new(c));
tracing::info!("post-push");

self.state.components.register(components)
}

Expand Down
3 changes: 3 additions & 0 deletions relayer-cli/src/commands/query/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ impl QueryConnectionChannelsCmd {
impl Runnable for QueryConnectionChannelsCmd {
fn run(&self) {
let config = app_config();
tracing::debug!("post-push ^2");
tracing::info!("post-push ^2");
tracing::error!("ERROROROOROR");

let (chain_config, opts) = match self.validate_options(&config) {
Err(err) => {
Expand Down
59 changes: 59 additions & 0 deletions relayer-cli/src/components.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use abscissa_core::{Component, FrameworkError, Shutdown};
use tracing_subscriber::{EnvFilter, FmtSubscriber, reload::Handle};
use tracing_subscriber::fmt::{
format::{Format, Json, JsonFields},
Formatter,
time::SystemTime
};
use tracing_subscriber::util::SubscriberInitExt;

/// Abscissa component for initializing the `tracing` subsystem
#[derive(Component, Debug)]
pub struct Tracing {
filter_handle: Handle<EnvFilter, Formatter<JsonFields, Format<Json, SystemTime>>>,
}

impl Tracing {
/// Creates a new [`Tracing`] component with the given `filter`.
pub fn new() -> Result<Self, FrameworkError> {
// TODO(adi) put these into a section of the relayer configuration
let filter = "debug".to_string();
let use_color = false;

// Construct a tracing subscriber with the supplied filter and enable reloading.
let builder = FmtSubscriber::builder()
.with_env_filter(filter)
.with_ansi(use_color)
.json()
.with_filter_reloading();
let filter_handle = builder.reload_handle();

let subscriber = builder.finish();
subscriber.init();

Ok(Self {
filter_handle,
})
}
}
//
// impl std::fmt::Debug for Tracing {
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// f.debug_struct("Tracing").finish()
// }
// }

// impl<A: abscissa_core::Application> Component<A> for Tracing {
// fn id(&self) -> abscissa_core::component::Id {
// abscissa_core::component::Id::new("relayer-cli::components::Tracing")
// }
//
// fn version(&self) -> abscissa_core::Version {
// abscissa_core::Version::parse("0.1.0").unwrap()
// }
//
// fn before_shutdown(&self, _kind: Shutdown) -> Result<(), FrameworkError> {
// tracing::info!("shutting down");
// Ok(())
// }
// }
1 change: 1 addition & 0 deletions relayer-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ mod conclude;
pub mod config;
pub mod error;
pub mod prelude;
mod components;

0 comments on commit c65bcef

Please sign in to comment.