@@ -4,12 +4,31 @@ use crate::{
44} ;
55use log:: * ;
66use simplelog:: SharedLogger ;
7- use std:: io:: Write ;
7+ use std:: { env , io:: Write } ;
88
99/// A logger that prints logs in the format expected by GitHub Actions, with grouping support.
1010///
1111/// See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
12- pub struct GithubActionLogger ;
12+ pub struct GithubActionLogger {
13+ log_level : LevelFilter ,
14+ }
15+
16+ impl GithubActionLogger {
17+ pub fn new ( ) -> Self {
18+ // Only enable debug logging if it's enabled in GitHub Actions.
19+ // See: https://docs.github.com/en/actions/reference/workflows-and-actions/contexts#runner-context
20+ let log_level = if env:: var ( "RUNNER_DEBUG" ) . unwrap_or_default ( ) == "1" {
21+ LevelFilter :: Trace
22+ } else {
23+ env:: var ( "CODSPEED_LOG" )
24+ . ok ( )
25+ . and_then ( |log_level| log_level. parse :: < LevelFilter > ( ) . ok ( ) )
26+ . unwrap_or ( LevelFilter :: Info )
27+ } ;
28+
29+ Self { log_level }
30+ }
31+ }
1332
1433impl Log for GithubActionLogger {
1534 fn enabled ( & self , _metadata : & Metadata ) -> bool {
@@ -40,6 +59,10 @@ impl Log for GithubActionLogger {
4059 return ;
4160 }
4261
62+ if level > self . log_level {
63+ return ;
64+ }
65+
4366 let prefix = match level {
4467 Level :: Error => "::error::" ,
4568 Level :: Warn => "::warning::" ,
@@ -60,9 +83,7 @@ impl Log for GithubActionLogger {
6083
6184impl SharedLogger for GithubActionLogger {
6285 fn level ( & self ) -> LevelFilter {
63- // since TRACE and DEBUG use ::debug::, we always enable them and let GitHub handle the filtering
64- // thanks to https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging#enabling-step-debug-logging
65- LevelFilter :: Trace
86+ self . log_level
6687 }
6788
6889 fn config ( & self ) -> Option < & simplelog:: Config > {
0 commit comments