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

Add debug flag #465

Merged
merged 3 commits into from
Nov 17, 2021
Merged

Add debug flag #465

merged 3 commits into from
Nov 17, 2021

Conversation

unknowndevQwQ
Copy link
Contributor

fix #454

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" };
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

image
like this? Or should I make some other changes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Copy link
Contributor Author

@unknowndevQwQ unknowndevQwQ Nov 12, 2021

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 of DEFAULT_LOG_LEVEL at L41. How about this?

I want the flag to have a higher priority than the environment variable

yihuaf
yihuaf previously requested changes Nov 11, 2021
Copy link
Collaborator

@yihuaf yihuaf left a 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

crates/youki/src/main.rs Show resolved Hide resolved
@unknowndevQwQ
Copy link
Contributor Author

@utam0k Seems to be ready to go

@Furisto Furisto marked this pull request as ready for review November 15, 2021 08:11
@utam0k
Copy link
Member

utam0k commented Nov 15, 2021

@unknowndevQwQ Sorry, this pr seems to be conflicting.

@unknowndevQwQ
Copy link
Contributor Author

@unknowndevQwQ Sorry, this pr seems to be conflicting.

fixed

Comment on lines 41 to 43
// 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.
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm

Copy link
Contributor Author

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

The default value in some options is dynamic, for example, --root

Copy link
Member

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.

Copy link
Contributor Author

@unknowndevQwQ unknowndevQwQ Nov 16, 2021

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?

or like this?
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 49 to 72
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();
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What areas became tricky?

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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
        }
    };

I didn't find a proper error handling in L39
In L51, `bail!()` does not exit after the output, but continues to execute, and I can't do anything about it
@codecov-commenter
Copy link

codecov-commenter commented Nov 16, 2021

Codecov Report

Merging #465 (5725f52) into main (ba08bb3) will not change coverage.
The diff coverage is 0.00%.

@@           Coverage Diff           @@
##             main     #465   +/-   ##
=======================================
  Coverage   60.44%   60.44%           
=======================================
  Files          82       82           
  Lines       12150    12150           
=======================================
  Hits         7344     7344           
  Misses       4806     4806           

Comment on lines 37 to 54
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)
},
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this?

Suggested change
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();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx

Copy link
Contributor Author

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?

@Furisto
Copy link
Collaborator

Furisto commented Nov 17, 2021

Yes, a faulty log level should not prevent execution of the command.

@Furisto Furisto dismissed yihuaf’s stale review November 17, 2021 19:34

Already addressed

@Furisto
Copy link
Collaborator

Furisto commented Nov 17, 2021

lgtm

@Furisto Furisto merged commit acf6e31 into youki-dev:main Nov 17, 2021
@unknowndevQwQ
Copy link
Contributor Author

lgtm

Can we compress the redundant commits?

@utam0k
Copy link
Member

utam0k commented Nov 18, 2021

lgtm

Can we compress the redundant commits?

I wish We could have, but I don't have to be so obsessed.

@unknowndevQwQ
Copy link
Contributor Author

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.

@unknowndevQwQ unknowndevQwQ deleted the add_debug_flag branch November 18, 2021 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: add --debug flag to enable debug logging
5 participants