@@ -125,8 +125,11 @@ impl ConsoleWriter {
125
125
msg = Some ( kv. value . as_str ( ) ) ;
126
126
}
127
127
"logfire.level_num" => {
128
- if let Value :: I64 ( val) = kv. value {
129
- level = Some ( val) ;
128
+ if let Value :: I64 ( level_num) = kv. value {
129
+ if level_num < level_to_level_number ( self . options . min_log_level ) {
130
+ return Ok ( ( ) ) ;
131
+ }
132
+ level = Some ( level_num) ;
130
133
}
131
134
}
132
135
"code.namespace" => target = Some ( kv. value . as_str ( ) ) ,
@@ -192,6 +195,10 @@ impl ConsoleWriter {
192
195
w : & mut W ,
193
196
) -> io:: Result < ( ) > {
194
197
let level = level_to_level_number ( * event. metadata ( ) . level ( ) ) ;
198
+ // Filter out event below the minimum log level
199
+ if level < level_to_level_number ( self . options . min_log_level ) {
200
+ return Ok ( ( ) ) ;
201
+ }
195
202
let target = event. metadata ( ) . module_path ( ) ;
196
203
197
204
let mut visitor = FieldsVisitor {
@@ -251,8 +258,11 @@ impl ConsoleWriter {
251
258
msg = Some ( kv. value . as_str ( ) ) ;
252
259
}
253
260
"logfire.level_num" => {
254
- if let Value :: I64 ( val) = kv. value {
255
- level = Some ( val) ;
261
+ if let Value :: I64 ( level_num) = kv. value {
262
+ if level_num < level_to_level_number ( self . options . min_log_level ) {
263
+ return Ok ( ( ) ) ;
264
+ }
265
+ level = Some ( level_num) ;
256
266
}
257
267
}
258
268
"code.namespace" => target = Some ( kv. value . as_str ( ) ) ,
@@ -358,7 +368,9 @@ mod tests {
358
368
fn test_print_to_console ( ) {
359
369
let output = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
360
370
361
- let console_options = ConsoleOptions :: default ( ) . with_target ( Target :: Pipe ( output. clone ( ) ) ) ;
371
+ let console_options = ConsoleOptions :: default ( )
372
+ . with_target ( Target :: Pipe ( output. clone ( ) ) )
373
+ . with_min_log_level ( Level :: TRACE ) ;
362
374
363
375
let handler = crate :: configure ( )
364
376
. local ( )
@@ -395,7 +407,7 @@ mod tests {
395
407
[2m1970-01-01T00:00:00.000002Z[0m[34m DEBUG[0m [2;3mlogfire::internal::exporters::console::tests[0m [1mdebug span[0m
396
408
[2m1970-01-01T00:00:00.000003Z[0m[34m DEBUG[0m [2;3mlogfire::internal::exporters::console::tests[0m [1mdebug span with explicit parent[0m
397
409
[2m1970-01-01T00:00:00.000004Z[0m[32m INFO[0m [2;3mlogfire::internal::exporters::console::tests[0m [1mhello world log[0m
398
- [2m1970-01-01T00:00:00.000005Z[0m[31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:381 :17, [3mbacktrace[0m=disabled backtrace
410
+ [2m1970-01-01T00:00:00.000005Z[0m[31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:393 :17, [3mbacktrace[0m=disabled backtrace
399
411
" ) ;
400
412
}
401
413
@@ -405,7 +417,8 @@ mod tests {
405
417
406
418
let console_options = ConsoleOptions :: default ( )
407
419
. with_target ( Target :: Pipe ( output. clone ( ) ) )
408
- . with_include_timestamps ( false ) ;
420
+ . with_include_timestamps ( false )
421
+ . with_min_log_level ( Level :: TRACE ) ;
409
422
410
423
let handler = crate :: configure ( )
411
424
. local ( )
@@ -442,7 +455,52 @@ mod tests {
442
455
[34m DEBUG[0m [2;3mlogfire::internal::exporters::console::tests[0m [1mdebug span[0m
443
456
[34m DEBUG[0m [2;3mlogfire::internal::exporters::console::tests[0m [1mdebug span with explicit parent[0m
444
457
[32m INFO[0m [2;3mlogfire::internal::exporters::console::tests[0m [1mhello world log[0m
445
- [31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:428:17, [3mbacktrace[0m=disabled backtrace
458
+ [31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:441:17, [3mbacktrace[0m=disabled backtrace
459
+ " ) ;
460
+ }
461
+
462
+ #[ test]
463
+ fn test_print_to_console_with_min_log_level ( ) {
464
+ let output = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
465
+
466
+ let console_options = ConsoleOptions :: default ( )
467
+ . with_target ( Target :: Pipe ( output. clone ( ) ) )
468
+ . with_min_log_level ( Level :: INFO ) ;
469
+
470
+ let handler = crate :: configure ( )
471
+ . local ( )
472
+ . send_to_logfire ( false )
473
+ . with_console ( Some ( console_options) )
474
+ . install_panic_handler ( )
475
+ . with_default_level_filter ( LevelFilter :: TRACE )
476
+ . finish ( )
477
+ . unwrap ( ) ;
478
+
479
+ let guard = set_local_logfire ( handler) ;
480
+
481
+ std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( || {
482
+ tracing:: subscriber:: with_default ( guard. subscriber ( ) . clone ( ) , || {
483
+ let root = crate :: span!( "root span" ) . entered ( ) ;
484
+ let _ = crate :: span!( "hello world span" ) . entered ( ) ;
485
+ let _ = crate :: span!( level: Level :: DEBUG , "debug span" ) ;
486
+ let _ = crate :: span!( parent: & root, level: Level :: DEBUG , "debug span with explicit parent" ) ;
487
+ crate :: info!( "hello world log" ) ;
488
+ panic ! ( "oh no!" ) ;
489
+ } ) ;
490
+ } ) )
491
+ . unwrap_err ( ) ;
492
+
493
+ guard. shutdown_handler . shutdown ( ) . unwrap ( ) ;
494
+
495
+ let output = output. lock ( ) . unwrap ( ) ;
496
+ let output = std:: str:: from_utf8 ( & output) . unwrap ( ) ;
497
+ let output = remap_timestamps_in_console_output ( output) ;
498
+
499
+ assert_snapshot ! ( output, @r"
500
+ [2m1970-01-01T00:00:00.000000Z[0m[32m INFO[0m [2;3mlogfire::internal::exporters::console::tests[0m [1mroot span[0m
501
+ [2m1970-01-01T00:00:00.000001Z[0m[32m INFO[0m [2;3mlogfire::internal::exporters::console::tests[0m [1mhello world span[0m
502
+ [2m1970-01-01T00:00:00.000002Z[0m[32m INFO[0m [2;3mlogfire::internal::exporters::console::tests[0m [1mhello world log[0m
503
+ [2m1970-01-01T00:00:00.000003Z[0m[31m ERROR[0m [2;3mlogfire[0m [1mpanic: oh no![0m [3mlocation[0m=src/internal/exporters/console.rs:488:17, [3mbacktrace[0m=disabled backtrace
446
504
" ) ;
447
505
}
448
506
}
0 commit comments