-
Notifications
You must be signed in to change notification settings - Fork 346
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
Add debug flag #465
Add debug flag #465
Conversation
crates/youki/src/logger.rs
Outdated
let formatter = match log_format.as_deref() { | ||
None | Some(LOG_FORMAT_TEXT) => text_write, | ||
Some(LOG_FORMAT_JSON) => json_write, | ||
Some(unknown) => bail!("unknown log format: {}", unknown), | ||
}; | ||
let loglevel = if log_debug_flag { "debug" } else { "warn" }; |
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.
Do you use loglevel
anywhere? I think you need to use this flag, perhaps around line 41?
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.
Do you use loglevel anywhere? I think you need to use this flag, perhaps around line 41?
I don't know what I should do after this, can you give me some tips?
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.
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.
You need to pass the log level to the logger builder. It has a filter_level method that should do the trick. You should set the log level to debug if the debug flag is specified, otherwise you need to check the youki log environment variable. If both are not set, you should fall back to the default log level.
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.
You need to pass the log level to the logger builder. It has a filter_level method that should do the trick. You should set the log level to debug if the debug flag is specified, otherwise you need to check the youki log environment variable. If both are not set, you should fall back to the default log level.
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.
let loglevel = if log_debug_flag { "debug" } else { "warn" }; | |
let loglevel = if log_debug_flag { | |
"debug" | |
} else { | |
DEFAULT_LOG_LEVEL | |
}; |
and set a variable loglevel
instead of DEFAULT_LOG_LEVEL
at L41.
How about this?
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.
and set a variable
loglevel
instead ofDEFAULT_LOG_LEVEL
at L41. How about this?
I want the flag to have a higher priority than the environment variable
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.
First of all, there is a DEFAULT_LOG_LEVEL
variable you should be using for default. Second, in addition to --debug
flag, you will have to handle YOUKI_LOG_LEVEL
from environment variable. We use env_logger
, and you should read the documentation here: https://docs.rs/env_logger/0.9.0/env_logger
@utam0k Seems to be ready to go |
41f4739
to
059a8e9
Compare
@unknowndevQwQ Sorry, this pr seems to be conflicting. |
059a8e9
to
e3b9062
Compare
fixed |
e3b9062
to
f908d88
Compare
crates/youki/src/main.rs
Outdated
// I don't know how to get the log level when the --debug flag is not set (I want to show some default values on the help page when the options are not set) | ||
// Example: '--debug change log level to debug. (default: "warn")' | ||
/// change log level to debug. |
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.
These comments in this section are ./youki -h
will be output.
When I thought about it, I felt that the first line was unnecessary.
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.
These codes may be helpful for you.
https://github.com/containers/youki/blob/ba08bb323165f4d0b98a0321fb990725bb32de69/crates/youki/src/commands/create.rs#L10-L27
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.
hmm
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.
These codes may be helpful for you.
The default value in some options is dynamic, for example, --root
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.
@unknowndevQwQ
My point is that the comments in this section are not for developers, but for users. I would like you to take that into account when making your comments.
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.
@unknowndevQwQ My point is that the comments in this section are not for developers, but for users. I would like you to take that into account when making your comments.
Should I delete L41-L42?
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.
f908d88
to
c50d8c7
Compare
crates/youki/src/logger.rs
Outdated
env_logger::Builder::new() | ||
.parse_env(env_logger::Env::new().filter("YOUKI_LOG_LEVEL")) | ||
.filter_level(FromStr::from_str(log_level).unwrap()) | ||
.format(formatter) | ||
.target(target) | ||
.init(); |
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.
This doesn't give a clear idea of which level has the highest priority.
So it is better to decide which log level you want in lines 33-37 and just set the value in the builder and do not overwrite it.
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.
This thing is starting to get tricky for me
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.
What areas became tricky?
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.
Get the value from the environment variable and convert it to the appropriate type
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.
Get the value from the environment variable and convert it to the appropriate type
It didn't seem like it would be a very complex code, but what are your concerns?
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.
Doesn't this code overwrite the default level even if YOUKI_LOG_LEVEL
is set?
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.
Get the value from the environment variable and convert it to the appropriate type
It didn't seem like it would be a very complex code, but what are your concerns?
Get the value from the environment variable and convert it to &str, output a friendly error if it fails
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.
let log_level = if log_debug_flag {
"debug"
} else {
match env::var("YOUKI_LOG_LEVEL") {
Ok(val) =>
Err(_e) => DEFAULT_LOG_LEVEL
}
};
c50d8c7
to
93bbaba
Compare
Codecov Report
@@ Coverage Diff @@
## main #465 +/- ##
=======================================
Coverage 60.44% 60.44%
=======================================
Files 82 82
Lines 12150 12150
=======================================
Hits 7344 7344
Misses 4806 4806 |
crates/youki/src/logger.rs
Outdated
let log_level_from_env = match env::var("YOUKI_LOG_LEVEL") { | ||
Ok(val) => val, | ||
Err(_) => String::from("i don't know"), | ||
}; | ||
let log_level = if log_debug_flag { | ||
"debug" | ||
} else if log_level_from_env != "i don't know" { | ||
&log_level_from_env | ||
} else { | ||
DEFAULT_LOG_LEVEL | ||
}; | ||
let log_level = match LevelFilter::from_str(log_level) { | ||
Ok(val) => val, | ||
Err(err) => { | ||
bail!("error: '{}', val from env: '{}'", err, log_level); | ||
exit(1) | ||
}, | ||
}; |
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.
How about this?
let log_level_from_env = match env::var("YOUKI_LOG_LEVEL") { | |
Ok(val) => val, | |
Err(_) => String::from("i don't know"), | |
}; | |
let log_level = if log_debug_flag { | |
"debug" | |
} else if log_level_from_env != "i don't know" { | |
&log_level_from_env | |
} else { | |
DEFAULT_LOG_LEVEL | |
}; | |
let log_level = match LevelFilter::from_str(log_level) { | |
Ok(val) => val, | |
Err(err) => { | |
bail!("error: '{}', val from env: '{}'", err, log_level); | |
exit(1) | |
}, | |
}; | |
let filter: Cow<str> = if debug_flag { | |
"debug".into() | |
} else if let Ok(level) = std::env::var("YOUKI_LOG_LEVEL") { | |
level.into() | |
} else { | |
DEFAULT_LOG_LEVEL.into() | |
}; | |
env_logger::Builder::new() | |
.filter_level(LevelFilter::from_str(filter.as_ref()).context("failed to parse log level")?) | |
.format(formatter) | |
.target(target) | |
.init(); |
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.
thx
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.
YOUKI_LOG_LEVEL=o sudo -E ./youki/target/x86_64-unknown-linux-gnu/release/youki state test
log init failed: failed to parse log level
Caused by:
attempted to convert a string that doesn't match an existing log level
{
"ociVersion": "v1.0.2",
"id": "test",
"status": "stopped",
"pid": 114514,
"bundle": "test",
"annotations": {},
"created": "2021-11-11T11:11:11.111111111Z",
"creator": 0,
"useSystemd": false
}
Is it normal to continue doing other things after a loglevel failure?
Yes, a faulty log level should not prevent execution of the command. |
lgtm |
Can we compress the redundant commits? |
I wish We could have, but I don't have to be so obsessed. |
Then let nature take its course. |
fix #454