Skip to content

Commit 66a856b

Browse files
committed
fix: only enable debug logs GH action is debugged
1 parent 3323c5a commit 66a856b

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/run/run_environment/github_actions/logger.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,30 @@ 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+
let log_level = if env::var("RUNNER_DEBUG").unwrap_or_default() == "1" {
20+
LevelFilter::Debug
21+
} else {
22+
env::var("CODSPEED_LOG")
23+
.ok()
24+
.and_then(|log_level| log_level.parse::<LevelFilter>().ok())
25+
.unwrap_or(LevelFilter::Info)
26+
};
27+
28+
Self { log_level }
29+
}
30+
}
1331

1432
impl Log for GithubActionLogger {
1533
fn enabled(&self, _metadata: &Metadata) -> bool {
@@ -40,6 +58,10 @@ impl Log for GithubActionLogger {
4058
return;
4159
}
4260

61+
if level > self.log_level {
62+
return;
63+
}
64+
4365
let prefix = match level {
4466
Level::Error => "::error::",
4567
Level::Warn => "::warning::",
@@ -60,9 +82,7 @@ impl Log for GithubActionLogger {
6082

6183
impl SharedLogger for GithubActionLogger {
6284
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
85+
self.log_level
6686
}
6787

6888
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)