-
Notifications
You must be signed in to change notification settings - Fork 81
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
Use RUST_LOG env to configure logging if present #247
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,9 @@ use std::{ | |
borrow::BorrowMut, | ||
fmt::Write as fmtwrite, | ||
fs::{File, OpenOptions}, | ||
io, | ||
io::Write, | ||
io::{self, Write}, | ||
path::Path, | ||
str::FromStr, | ||
sync::Mutex, | ||
}; | ||
|
||
|
@@ -34,6 +34,8 @@ use crate::error::Error; | |
#[cfg(windows)] | ||
use crate::sys::windows::NamedPipeLogger; | ||
|
||
pub const LOG_ENV: &str = "RUST_LOG"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should name the env var to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did consider trying to integrate |
||
|
||
pub struct FifoLogger { | ||
file: Mutex<File>, | ||
} | ||
|
@@ -118,7 +120,12 @@ impl log::Log for FifoLogger { | |
} | ||
} | ||
|
||
pub fn init(debug: bool, _namespace: &str, _id: &str) -> Result<(), Error> { | ||
pub fn init( | ||
debug: bool, | ||
default_log_level: &str, | ||
_namespace: &str, | ||
_id: &str, | ||
) -> Result<(), Error> { | ||
#[cfg(unix)] | ||
let logger = FifoLogger::new().map_err(io_error!(e, "failed to init logger"))?; | ||
|
||
|
@@ -132,15 +139,20 @@ pub fn init(debug: bool, _namespace: &str, _id: &str) -> Result<(), Error> { | |
let logger = | ||
NamedPipeLogger::new(_namespace, _id).map_err(io_error!(e, "failed to init logger"))?; | ||
|
||
let level = if debug { | ||
configure_logging_level(debug, default_log_level); | ||
log::set_boxed_logger(Box::new(logger))?; | ||
Ok(()) | ||
} | ||
|
||
fn configure_logging_level(debug: bool, default_log_level: &str) { | ||
let debug_level = std::env::var(LOG_ENV).unwrap_or(default_log_level.to_string()); | ||
let debug_level = log::LevelFilter::from_str(&debug_level).unwrap_or(log::LevelFilter::Info); | ||
let level = if debug && log::LevelFilter::Debug > debug_level { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you still need this special handling of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept it there so that it was backwards compatible. If a user turns that value on they would expect logs and I didn't want to break that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some context about defaults is above, but we may simplify this a bit to:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure that we could push out a warning as the logger wasn't fully set up at this point but I can try it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the reason for |
||
log::LevelFilter::Debug | ||
} else { | ||
log::LevelFilter::Info | ||
debug_level | ||
}; | ||
|
||
log::set_boxed_logger(Box::new(logger))?; | ||
log::set_max_level(level); | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn rfc3339_formated() -> String { | ||
|
@@ -156,6 +168,56 @@ mod tests { | |
use log::{Log, Record}; | ||
|
||
use super::*; | ||
use crate::Config; | ||
|
||
#[test] | ||
fn test_init_log_level() -> Result<(), Error> { | ||
let config = Config::default(); | ||
|
||
configure_logging_level(false, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Info, log::max_level()); | ||
|
||
// Default for debug flag from containerd | ||
configure_logging_level(true, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Debug, log::max_level()); | ||
|
||
// ENV different than default | ||
std::env::set_var(LOG_ENV, "error"); | ||
configure_logging_level(false, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Error, log::max_level()); | ||
|
||
std::env::set_var(LOG_ENV, "warn"); | ||
configure_logging_level(false, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Warn, log::max_level()); | ||
|
||
std::env::set_var(LOG_ENV, "off"); | ||
configure_logging_level(false, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Off, log::max_level()); | ||
|
||
std::env::set_var(LOG_ENV, "trace"); | ||
configure_logging_level(false, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Trace, log::max_level()); | ||
|
||
std::env::set_var(LOG_ENV, "debug"); | ||
configure_logging_level(false, &config.default_log_level); | ||
|
||
// ENV Different than default from debug flag | ||
configure_logging_level(true, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Debug, log::max_level()); | ||
|
||
std::env::set_var(LOG_ENV, "trace"); | ||
configure_logging_level(true, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Trace, log::max_level()); | ||
|
||
std::env::set_var(LOG_ENV, "info"); | ||
configure_logging_level(true, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Debug, log::max_level()); | ||
|
||
std::env::set_var(LOG_ENV, "off"); | ||
configure_logging_level(true, &config.default_log_level); | ||
assert_eq!(log::LevelFilter::Debug, log::max_level()); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_fifo_log() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Is this case sensitive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from my testing it was not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default_log_level
is a bit confusing to me. We have a default one and env variable to customize it if default doesn't work. Customizing both default value and env log is a bit excessive?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that some shims owners would want to set it different from
info
with requiring customization through an Environment variableThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but they can use environment variable to customize it, if they don't like default value, right?