Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/run/run_environment/github_actions/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,31 @@ use crate::{
};
use log::*;
use simplelog::SharedLogger;
use std::io::Write;
use std::{env, io::Write};

/// A logger that prints logs in the format expected by GitHub Actions, with grouping support.
///
/// See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
pub struct GithubActionLogger;
pub struct GithubActionLogger {
log_level: LevelFilter,
}

impl GithubActionLogger {
pub fn new() -> Self {
// Only enable debug logging if it's enabled in GitHub Actions.
// See: https://docs.github.com/en/actions/reference/workflows-and-actions/variables
let log_level = if env::var("RUNNER_DEBUG").unwrap_or_default() == "1" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have docs on this RUNNER_DEBUG flag?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mentioned in the docs here: https://docs.github.com/en/actions/reference/workflows-and-actions/contexts#runner-context, i'll add a link in the code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks. Actually this page is about the env variables: https://docs.github.com/en/actions/reference/workflows-and-actions/variables

LevelFilter::Trace
} else {
env::var("CODSPEED_LOG")
.ok()
.and_then(|log_level| log_level.parse::<LevelFilter>().ok())
.unwrap_or(LevelFilter::Info)
};

Self { log_level }
}
}

impl Log for GithubActionLogger {
fn enabled(&self, _metadata: &Metadata) -> bool {
Expand Down Expand Up @@ -40,6 +59,10 @@ impl Log for GithubActionLogger {
return;
}

if level > self.log_level {
return;
}

let prefix = match level {
Level::Error => "::error::",
Level::Warn => "::warning::",
Expand All @@ -60,9 +83,7 @@ impl Log for GithubActionLogger {

impl SharedLogger for GithubActionLogger {
fn level(&self) -> LevelFilter {
// since TRACE and DEBUG use ::debug::, we always enable them and let GitHub handle the filtering
// thanks to https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging#enabling-step-debug-logging
LevelFilter::Trace
self.log_level
}

fn config(&self) -> Option<&simplelog::Config> {
Expand Down
2 changes: 1 addition & 1 deletion src/run/run_environment/github_actions/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl RunEnvironmentProvider for GitHubActionsProvider {
}

fn get_logger(&self) -> Box<dyn SharedLogger> {
Box::new(GithubActionLogger)
Box::new(GithubActionLogger::new())
}

fn get_run_environment(&self) -> RunEnvironment {
Expand Down