Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a stderr configuration of the logger module, and use it in tests #537

Merged
merged 2 commits into from
Oct 23, 2020
Merged
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
38 changes: 32 additions & 6 deletions common/src/logger/loggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/// Sets chan_size for stdout, gelf, and UDP loggers
const STDOUT_CHANNEL_SIZE: usize = 100_000;
const STDERR_CHANNEL_SIZE: usize = 100_000;
const GELF_CHANNEL_SIZE: usize = 100_000;
const UDP_CHANNEL_SIZE: usize = 100_000;

Expand Down Expand Up @@ -43,6 +44,22 @@ fn create_stdout_logger() -> slog::Fuse<slog_async::Async> {
.fuse()
}

/// Create a basic stderr logger.
fn create_stderr_logger() -> slog::Fuse<slog_async::Async> {
let decorator = slog_term::TermDecorator::new().stderr().build();
let drain = slog_envlogger::new(
slog_term::FullFormat::new(decorator)
.use_custom_timestamp(custom_timestamp)
.build()
.fuse(),
);
slog_async::Async::new(drain)
.thread_name("slog-stderr".into())
.chan_size(STDERR_CHANNEL_SIZE)
.build()
.fuse()
}

/// Create a GELF (https://docs.graylog.org/en/3.0/pages/gelf.html) logger.
fn create_gelf_logger() -> Option<slog::Fuse<slog_async::Async>> {
env::var("MC_LOG_GELF").ok().map(|remote_host_port| {
Expand Down Expand Up @@ -123,7 +140,13 @@ pub fn create_root_logger() -> Logger {
(None, Some(udp_json)) => Some(udp_json),
(Some(_), Some(_)) => panic!("MC_LOG_GELF and MC_LOG_UDP_JSON are mutually exclusive!"),
};
let stdout_logger = create_stdout_logger();

// Create stdout / stderr sink
let std_logger = if env::var("MC_LOG_STDERR") == Ok("1".to_string()) {
create_stderr_logger()
} else {
create_stdout_logger()
};

// Extra context that always gets added to each log message.
let extra_kv = o!(
Expand All @@ -133,12 +156,9 @@ pub fn create_root_logger() -> Logger {

// Create root logger.
let mut root_logger = if let Some(network_logger) = network_logger {
Logger::root(
slog::Duplicate(stdout_logger, network_logger).fuse(),
extra_kv,
)
Logger::root(slog::Duplicate(std_logger, network_logger).fuse(), extra_kv)
} else {
Logger::root(stdout_logger, extra_kv)
Logger::root(std_logger, extra_kv)
};

// Add extra context if it is available.
Expand All @@ -165,6 +185,12 @@ pub fn create_root_logger() -> Logger {

/// Create a logger that is suitable for use during test execution.
pub fn create_test_logger(test_name: String) -> Logger {
// Make it so that tests log to stderr by default.
// This can be overrided by setting MC_LOG_STDERR to 0,
// but that isn't expected to be necessary
if env::var("MC_LOG_STDERR").is_err() {
cbeck88 marked this conversation as resolved.
Show resolved Hide resolved
env::set_var("MC_LOG_STDERR", "1");
}
create_root_logger().new(o!(
"mc.test_name" => test_name,
))
Expand Down