-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathlogger.rs
More file actions
109 lines (95 loc) · 2.96 KB
/
Copy pathlogger.rs
File metadata and controls
109 lines (95 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::{
logger::{GroupEvent, get_announcement_event, get_group_event, get_json_event},
run_environment::logger::should_provider_logger_handle_record,
};
use log::*;
use simplelog::SharedLogger;
use std::{env, io::Write};
/// Title used for announcements when no explicit title is provided. Preserves the legacy
/// `[ANNOUNCEMENT]` prefix that this logger emitted before per-call titles were supported.
const DEFAULT_BUILDKITE_ANNOUNCEMENT_TITLE: &str = "ANNOUNCEMENT";
/// A logger that prints logs in the format expected by Buildkite
///
/// See https://buildkite.com/docs/pipelines/managing-log-output
pub struct BuildkiteLogger {
log_level: LevelFilter,
}
impl BuildkiteLogger {
pub fn new() -> Self {
let log_level = env::var("CODSPEED_LOG")
.ok()
.and_then(|log_level| log_level.parse::<log::LevelFilter>().ok())
.unwrap_or(log::LevelFilter::Info);
Self { log_level }
}
}
impl Log for BuildkiteLogger {
fn enabled(&self, _metadata: &Metadata) -> bool {
true
}
fn log(&self, record: &Record) {
if !should_provider_logger_handle_record(record) {
return;
}
let level = record.level();
let message = record.args();
if let Some(group_event) = get_group_event(record) {
match group_event {
GroupEvent::Start(name) => {
println!("--- {name}");
}
GroupEvent::StartOpened(name) => {
println!("+++ {name}");
}
GroupEvent::End => {}
}
return;
}
if get_json_event(record).is_some() {
return;
}
if let Some(announcement) = get_announcement_event(record) {
let title = announcement
.title
.as_deref()
.unwrap_or(DEFAULT_BUILDKITE_ANNOUNCEMENT_TITLE);
println!("[{title}] {}", announcement.message);
return;
}
if level > self.log_level {
return;
}
// there is no support for log levels in Buildkite, so we print the level in the message
match level {
Level::Error => {
println!("[ERROR] {message}");
}
Level::Warn => {
println!("[WARN] {message}");
}
Level::Info => {
println!("{message}");
}
Level::Debug => {
println!("[DEBUG] {message}");
}
Level::Trace => {
println!("[TRACE] {message}");
}
}
}
fn flush(&self) {
std::io::stdout().flush().unwrap();
}
}
impl SharedLogger for BuildkiteLogger {
fn level(&self) -> LevelFilter {
self.log_level
}
fn config(&self) -> Option<&simplelog::Config> {
None
}
fn as_log(self: Box<Self>) -> Box<dyn Log> {
Box::new(*self)
}
}