pub fn init_logger(
	pattern: &str,
	tracing_receiver: sc_tracing::TracingReceiver,
	profiling_targets: Option<String>,
	disable_log_reloading: bool,
) -> std::result::Result<(), String> {
	use sc_tracing::parse_default_directive;

	// Accept all valid directives and print invalid ones
	fn parse_user_directives(mut env_filter: EnvFilter, dirs: &str) -> std::result::Result<EnvFilter, String> {
		for dir in dirs.split(',') {
			env_filter = env_filter.add_directive(parse_default_directive(&dir)?);
		}
		Ok(env_filter)
	}

	if let Err(e) = tracing_log::LogTracer::init() {
		return Err(format!(
			"Registering Substrate logger failed: {:}!", e
		))
	}

	// Initialize filter - ensure to use `parse_default_directive` for any defaults to persist
	// after log filter reloading by RPC
	let mut env_filter = EnvFilter::default()
		// Enable info
		.add_directive(parse_default_directive("info")
			.expect("provided directive is valid"))
		// Disable info logging by default for some modules.
		.add_directive(parse_default_directive("ws=off")
			.expect("provided directive is valid"))
		.add_directive(parse_default_directive("yamux=off")
			.expect("provided directive is valid"))
		.add_directive(parse_default_directive("cranelift_codegen=off")
			.expect("provided directive is valid"))
		// Set warn logging by default for some modules.
		.add_directive(parse_default_directive("cranelift_wasm=warn")
			.expect("provided directive is valid"))
		.add_directive(parse_default_directive("hyper=warn")
			.expect("provided directive is valid"));

	if let Ok(lvl) = std::env::var("RUST_LOG") {
		if lvl != "" {
			env_filter = parse_user_directives(env_filter, &lvl)?;
		}
	}

	if pattern != "" {
		// We're not sure if log or tracing is available at this moment, so silently ignore the
		// parse error.
		env_filter = parse_user_directives(env_filter, pattern)?;
	}

	// If we're only logging `INFO` entries then we'll use a simplified logging format.
	let simple = match Layer::<FmtSubscriber>::max_level_hint(&env_filter) {
		Some(level) if level <= tracing_subscriber::filter::LevelFilter::INFO => true,
		_ => false,
	};

	// Always log the special target `sc_tracing`, overrides global level.
	// Required because profiling traces are emitted via `sc_tracing`
	// NOTE: this must be done after we check the `max_level_hint` otherwise
	// it is always raised to `TRACE`.
	env_filter = env_filter.add_directive(
		parse_default_directive("sc_tracing=trace").expect("provided directive is valid")
	);

	// Make sure to include profiling targets in the filter
	if let Some(profiling_targets) = profiling_targets.clone() {
		env_filter = parse_user_directives(env_filter, &profiling_targets)?;
	}

	let enable_color = atty::is(atty::Stream::Stderr);
	let timer = ChronoLocal::with_format(if simple {
		"%Y-%m-%d %H:%M:%S".to_string()
	} else {
		"%Y-%m-%d %H:%M:%S%.3f".to_string()
	});

	let subscriber_builder = FmtSubscriber::builder()
		.with_env_filter(env_filter)
		.with_writer(std::io::stderr as _)
		.event_format(logging::EventFormat {
			timer,
			enable_color,
			display_target: !simple,
			display_level: !simple,
			display_thread_name: !simple,
		});
	if disable_log_reloading {
		let subscriber = subscriber_builder
			.finish()
			.with(logging::NodeNameLayer);
		initialize_tracing(subscriber, tracing_receiver, profiling_targets)
	} else {
		let subscriber_builder = subscriber_builder.with_filter_reloading();
		let handle = subscriber_builder.reload_handle();
		sc_tracing::set_reload_handle(handle);
		let subscriber = subscriber_builder
			.finish()
			.with(logging::NodeNameLayer);
		initialize_tracing(subscriber, tracing_receiver, profiling_targets)
	}
}
/// Initialize the global logger
///
/// This sets various global logging and tracing instances and thus may only be called once.
Initialize the global logger
This sets various global logging and tracing instances and thus may only be called once.
Initialize the global logger This sets various global logging and tracing instances and thus may only be called once .
pub fn init_logger(
	pattern: &str,
	tracing_receiver: sc_tracing::TracingReceiver,
	profiling_targets: Option<String>,
	disable_log_reloading: bool,
) -> std::result::Result<(), String> {
	use sc_tracing::parse_default_directive;
	// Accept all valid directives and print invalid ones
	fn parse_user_directives(mut env_filter: EnvFilter, dirs: &str) -> std::result::Result<EnvFilter, String> {
		for dir in dirs.split(',') {
			env_filter = env_filter.add_directive(parse_default_directive(&dir)?);
		}
		Ok(env_filter)
	}
	if let Err(e) = tracing_log::LogTracer::init() {
		return Err(format!(
			"Registering Substrate logger failed: {:}!", e
		))
	}
	// Initialize filter - ensure to use `parse_default_directive` for any defaults to persist
	// after log filter reloading by RPC
	let mut env_filter = EnvFilter::default()
		// Enable info
		.add_directive(parse_default_directive("info")
			.expect("provided directive is valid"))
		// Disable info logging by default for some modules.
		.add_directive(parse_default_directive("ws=off")
			.expect("provided directive is valid"))
		.add_directive(parse_default_directive("yamux=off")
			.expect("provided directive is valid"))
		.add_directive(parse_default_directive("cranelift_codegen=off")
			.expect("provided directive is valid"))
		// Set warn logging by default for some modules.
		.add_directive(parse_default_directive("cranelift_wasm=warn")
			.expect("provided directive is valid"))
		.add_directive(parse_default_directive("hyper=warn")
			.expect("provided directive is valid"));
	if let Ok(lvl) = std::env::var("RUST_LOG") {
		if lvl != "" {
			env_filter = parse_user_directives(env_filter, &lvl)?;
		}
	}
	if pattern != "" {
		// We're not sure if log or tracing is available at this moment, so silently ignore the
		// parse error.
		env_filter = parse_user_directives(env_filter, pattern)?;
	}
	// If we're only logging `INFO` entries then we'll use a simplified logging format.
	let simple = match Layer::<FmtSubscriber>::max_level_hint(&env_filter) {
		Some(level) if level <= tracing_subscriber::filter::LevelFilter::INFO => true,
		_ => false,
	};
	// Always log the special target `sc_tracing`, overrides global level.
	// Required because profiling traces are emitted via `sc_tracing`
	// NOTE: this must be done after we check the `max_level_hint` otherwise
	// it is always raised to `TRACE`.
	env_filter = env_filter.add_directive(
		parse_default_directive("sc_tracing=trace").expect("provided directive is valid")
	);
	// Make sure to include profiling targets in the filter
	if let Some(profiling_targets) = profiling_targets.clone() {
		env_filter = parse_user_directives(env_filter, &profiling_targets)?;
	}
	let enable_color = atty::is(atty::Stream::Stderr);
	let timer = ChronoLocal::with_format(if simple {
		"%Y-%m-%d %H:%M:%S".to_string()
	} else {
		"%Y-%m-%d %H:%M:%S%.3f".to_string()
	});
	let subscriber_builder = FmtSubscriber::builder()
		.with_env_filter(env_filter)
		.with_writer(std::io::stderr as _)
		.event_format(logging::EventFormat {
			timer,
			enable_color,
			display_target: !simple,
			display_level: !simple,
			display_thread_name: !simple,
		});
	if disable_log_reloading {
		let subscriber = subscriber_builder
			.finish()
			.with(logging::NodeNameLayer);
		initialize_tracing(subscriber, tracing_receiver, profiling_targets)
	} else {
		let subscriber_builder = subscriber_builder.with_filter_reloading();
		let handle = subscriber_builder.reload_handle();
		sc_tracing::set_reload_handle(handle);
		let subscriber = subscriber_builder
			.finish()
			.with(logging::NodeNameLayer);
		initialize_tracing(subscriber, tracing_receiver, profiling_targets)
	}
}
pub fn init_logger ( pattern : & str , tracing_receiver : sc_tracing :: TracingReceiver , profiling_targets : Option < String > , disable_log_reloading : bool , ) -> std :: result :: Result < ( ) , String > { use sc_tracing :: parse_default_directive ; // Accept all valid directives and print invalid ones fn parse_user_directives ( mut env_filter : EnvFilter , dirs : & str ) -> std :: result :: Result < EnvFilter , String > { for dir in dirs . split ( ',' ) { env_filter = env_filter . add_directive ( parse_default_directive ( & dir ) ? ) ; } Ok ( env_filter ) } if let Err ( e ) = tracing_log :: LogTracer :: init ( ) { return Err ( format ! ( " " e ) ) } // Initialize filter - ensure to use `parse_default_directive` for any defaults to persist // after log filter reloading by RPC let mut env_filter = EnvFilter :: default ( ) // Enable info . add_directive ( parse_default_directive ( " " ) . expect ( " " ) ) // Disable info logging by default for some modules. . add_directive ( parse_default_directive ( " " ) . expect ( " " ) ) . add_directive ( parse_default_directive ( " " ) . expect ( " " ) ) . add_directive ( parse_default_directive ( " " ) . expect ( " " ) ) // Set warn logging by default for some modules. . add_directive ( parse_default_directive ( " " ) . expect ( " " ) ) . add_directive ( parse_default_directive ( " " ) . expect ( " " ) ) ; if let Ok ( lvl ) = std :: env :: var ( " " ) { if lvl != " " { env_filter = parse_user_directives ( env_filter , & lvl ) ? ; } } if pattern != " " { // We're not sure if log or tracing is available at this moment, so silently ignore the // parse error. env_filter = parse_user_directives ( env_filter , pattern ) ? ; } // If we're only logging `INFO` entries then we'll use a simplified logging format. let simple = match Layer :: < FmtSubscriber > :: max_level_hint ( & env_filter ) { Some ( level ) if level <= tracing_subscriber :: filter :: LevelFilter :: INFO => true , _ => false , } ; // Always log the special target `sc_tracing`, overrides global level. // Required because profiling traces are emitted via `sc_tracing` // NOTE: this must be done after we check the `max_level_hint` otherwise // it is always raised to `TRACE`. env_filter = env_filter . add_directive ( parse_default_directive ( " " ) . expect ( " " ) ) ; // Make sure to include profiling targets in the filter if let Some ( profiling_targets ) = profiling_targets . clone ( ) { env_filter = parse_user_directives ( env_filter , & profiling_targets ) ? ; } let enable_color = atty :: is ( atty :: Stream :: Stderr ) ; let timer = ChronoLocal :: with_format ( if simple { " " . to_string ( ) } else { " " . to_string ( ) } ) ; let subscriber_builder = FmtSubscriber :: builder ( ) . with_env_filter ( env_filter ) . with_writer ( std :: io :: stderr as _ ) . event_format ( logging :: EventFormat { timer , enable_color , display_target : ! simple , display_level : ! simple , display_thread_name : ! simple , } ) ; if disable_log_reloading { let subscriber = subscriber_builder . finish ( ) . with ( logging :: NodeNameLayer ) ; initialize_tracing ( subscriber , tracing_receiver , profiling_targets ) } else { let subscriber_builder = subscriber_builder . with_filter_reloading ( ) ; let handle = subscriber_builder . reload_handle ( ) ; sc_tracing :: set_reload_handle ( handle ) ; let subscriber = subscriber_builder . finish ( ) . with ( logging :: NodeNameLayer ) ; initialize_tracing ( subscriber , tracing_receiver , profiling_targets ) } }
Initialize the global logger
This sets various global logging and tracing instances and thus may only be called once.
Initialize the global logger This sets various global logging and tracing instances and thus may only be called once .
pattern
&str
tracing_receiver
sc_tracing::TracingReceiver
profiling_targets
Option<String>
disable_log_reloading
bool
pattern
&str
tracing_receiver
sc_tracing::TracingReceiver
profiling_targets
Option<String>
disable_log_reloading
bool