Skip to content

Commit 682663e

Browse files
committed
fix: only enable debug logs GH action is debugged
1 parent 2c7507f commit 682663e

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/run/run_environment/github_actions/logger.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,31 @@ use crate::{
44
};
55
use log::*;
66
use 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

1433
impl 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

6184
impl 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> {

src/run/run_environment/github_actions/provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl RunEnvironmentProvider for GitHubActionsProvider {
126126
}
127127

128128
fn get_logger(&self) -> Box<dyn SharedLogger> {
129-
Box::new(GithubActionLogger)
129+
Box::new(GithubActionLogger::new())
130130
}
131131

132132
fn get_run_environment(&self) -> RunEnvironment {

0 commit comments

Comments
 (0)