mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-21 05:44:44 +00:00
Output network-test logs into files in CI (#6355)
* Add ci_logger * Update artifact name * Add env var * Add fork_name * Fix clippy error * Add comments
This commit is contained in:
@@ -43,13 +43,15 @@ use rayon::prelude::*;
|
||||
use sensitive_url::SensitiveUrl;
|
||||
use slog::{o, Drain, Logger};
|
||||
use slog_async::Async;
|
||||
use slog_term::{FullFormat, TermDecorator};
|
||||
use slog_term::{FullFormat, PlainSyncDecorator, TermDecorator};
|
||||
use slot_clock::{SlotClock, TestingSlotClock};
|
||||
use state_processing::per_block_processing::compute_timestamp_at_slot;
|
||||
use state_processing::state_advance::complete_state_advance;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fmt;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::BufWriter;
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, LazyLock};
|
||||
@@ -68,6 +70,8 @@ use types::{typenum::U4294967296, *};
|
||||
pub const HARNESS_GENESIS_TIME: u64 = 1_567_552_690;
|
||||
// Environment variable to read if `fork_from_env` feature is enabled.
|
||||
pub const FORK_NAME_ENV_VAR: &str = "FORK_NAME";
|
||||
// Environment variable to read if `ci_logger` feature is enabled.
|
||||
pub const CI_LOGGER_DIR_ENV_VAR: &str = "CI_LOGGER_DIR";
|
||||
|
||||
// Default target aggregators to set during testing, this ensures an aggregator at each slot.
|
||||
//
|
||||
@@ -2750,15 +2754,55 @@ pub struct MakeAttestationOptions {
|
||||
pub fork: Fork,
|
||||
}
|
||||
|
||||
pub fn build_log(level: slog::Level, enabled: bool) -> Logger {
|
||||
let decorator = TermDecorator::new().build();
|
||||
let drain = FullFormat::new(decorator).build().fuse();
|
||||
let drain = Async::new(drain).build().fuse();
|
||||
pub enum LoggerType {
|
||||
Test,
|
||||
// The logs are output to files for each test.
|
||||
CI,
|
||||
// No logs will be printed.
|
||||
Null,
|
||||
}
|
||||
|
||||
if enabled {
|
||||
Logger::root(drain.filter_level(level).fuse(), o!())
|
||||
} else {
|
||||
Logger::root(drain.filter(|_| false).fuse(), o!())
|
||||
fn ci_decorator() -> PlainSyncDecorator<BufWriter<File>> {
|
||||
let log_dir = std::env::var(CI_LOGGER_DIR_ENV_VAR).unwrap_or_else(|e| {
|
||||
panic!("{CI_LOGGER_DIR_ENV_VAR} env var must be defined when using ci_logger: {e:?}");
|
||||
});
|
||||
let fork_name = std::env::var(FORK_NAME_ENV_VAR)
|
||||
.map(|s| format!("{s}_"))
|
||||
.unwrap_or_default();
|
||||
// The current test name can be got via the thread name.
|
||||
let test_name = std::thread::current()
|
||||
.name()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
// Colons are not allowed in files that are uploaded to GitHub Artifacts.
|
||||
.replace("::", "_");
|
||||
let log_path = format!("/{log_dir}/{fork_name}{test_name}.log");
|
||||
let file = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(log_path)
|
||||
.unwrap();
|
||||
let file = BufWriter::new(file);
|
||||
PlainSyncDecorator::new(file)
|
||||
}
|
||||
|
||||
pub fn build_log(level: slog::Level, logger_type: LoggerType) -> Logger {
|
||||
match logger_type {
|
||||
LoggerType::Test => {
|
||||
let drain = FullFormat::new(TermDecorator::new().build()).build().fuse();
|
||||
let drain = Async::new(drain).build().fuse();
|
||||
Logger::root(drain.filter_level(level).fuse(), o!())
|
||||
}
|
||||
LoggerType::CI => {
|
||||
let drain = FullFormat::new(ci_decorator()).build().fuse();
|
||||
let drain = Async::new(drain).build().fuse();
|
||||
Logger::root(drain.filter_level(level).fuse(), o!())
|
||||
}
|
||||
LoggerType::Null => {
|
||||
let drain = FullFormat::new(TermDecorator::new().build()).build().fuse();
|
||||
let drain = Async::new(drain).build().fuse();
|
||||
Logger::root(drain.filter(|_| false).fuse(), o!())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user