66//!
77//! To get started, see [`Tracing`].
88
9- use std:: path:: PathBuf ;
9+ use std:: { ops :: Not , path:: PathBuf } ;
1010
1111#[ cfg_attr( feature = "clap" , cfg( doc) ) ]
1212use clap;
@@ -105,6 +105,7 @@ pub enum Error {
105105/// async fn main() -> Result<(), Error> {
106106/// let options = TelemetryOptions {
107107/// console_log_disabled: false,
108+ /// console_log_format: Default::default(),
108109/// file_log_directory: None,
109110/// file_log_rotation_period: None,
110111/// file_log_max_files: Some(6),
@@ -347,6 +348,7 @@ impl Tracing {
347348 pub fn pre_configured ( service_name : & ' static str , options : TelemetryOptions ) -> Self {
348349 let TelemetryOptions {
349350 console_log_disabled,
351+ console_log_format,
350352 file_log_directory,
351353 file_log_rotation_period,
352354 file_log_max_files,
@@ -358,11 +360,14 @@ impl Tracing {
358360
359361 Self :: builder ( )
360362 . service_name ( service_name)
361- . with_console_output ( (
362- Self :: CONSOLE_LOG_LEVEL_ENV ,
363- LevelFilter :: INFO ,
364- !console_log_disabled,
365- ) )
363+ . with_console_output ( console_log_disabled. not ( ) . then ( || {
364+ Settings :: builder ( )
365+ . with_environment_variable ( Self :: CONSOLE_LOG_LEVEL_ENV )
366+ . with_default_level ( LevelFilter :: INFO )
367+ . console_log_settings_builder ( )
368+ . with_log_format ( console_log_format)
369+ . build ( )
370+ } ) )
366371 . with_file_output ( file_log_directory. map ( |log_directory| {
367372 Settings :: builder ( )
368373 . with_environment_variable ( Self :: FILE_LOG_LEVEL_ENV )
@@ -397,16 +402,29 @@ impl Tracing {
397402
398403 if let ConsoleLogSettings :: Enabled {
399404 common_settings,
400- log_format : _ ,
405+ log_format,
401406 } = & self . console_log_settings
402407 {
403408 let env_filter_layer = env_filter_builder (
404409 common_settings. environment_variable ,
405410 common_settings. default_level ,
406411 ) ;
407- let console_output_layer =
408- tracing_subscriber:: fmt:: layer ( ) . with_filter ( env_filter_layer) ;
409- layers. push ( console_output_layer. boxed ( ) ) ;
412+
413+ // NOTE (@NickLarsenNZ): There is no elegant way to build the layer depending on formats because the types
414+ // returned from each subscriber "modifier" function is different (sometimes with different generics).
415+ match log_format {
416+ Format :: Plain => {
417+ let console_output_layer =
418+ tracing_subscriber:: fmt:: layer ( ) . with_filter ( env_filter_layer) ;
419+ layers. push ( console_output_layer. boxed ( ) ) ;
420+ }
421+ Format :: Json => {
422+ let console_output_layer = tracing_subscriber:: fmt:: layer ( )
423+ . json ( )
424+ . with_filter ( env_filter_layer) ;
425+ layers. push ( console_output_layer. boxed ( ) ) ;
426+ }
427+ } ;
410428 }
411429
412430 if let FileLogSettings :: Enabled {
@@ -761,9 +779,16 @@ struct Cli {
761779#[ derive( Debug , Default ) ]
762780pub struct TelemetryOptions {
763781 /// Disable console logs.
764- #[ cfg_attr( feature = "clap" , arg( long, env) ) ]
782+ #[ cfg_attr( feature = "clap" , arg( long, env, group = "console_log" ) ) ]
765783 pub console_log_disabled : bool ,
766784
785+ /// Console log format
786+ #[ cfg_attr(
787+ feature = "clap" ,
788+ arg( long, env, group = "console_log" , default_value_t)
789+ ) ]
790+ pub console_log_format : Format ,
791+
767792 /// Enable logging to files located in the specified DIRECTORY.
768793 #[ cfg_attr(
769794 feature = "clap" ,
@@ -1023,6 +1048,7 @@ mod test {
10231048 fn pre_configured ( ) {
10241049 let tracing = Tracing :: pre_configured ( "test" , TelemetryOptions {
10251050 console_log_disabled : false ,
1051+ console_log_format : Default :: default ( ) ,
10261052 file_log_directory : None ,
10271053 file_log_rotation_period : None ,
10281054 file_log_max_files : None ,
0 commit comments