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 --log-level flag #3205

Merged
merged 3 commits into from
Oct 28, 2024
Merged

Conversation

Bravo555
Copy link
Contributor

@Bravo555 Bravo555 commented Oct 24, 2024

Proposed changes

Add a --log-level flag that sets the verbosity of reported logs.

In contrast to --debug, which enables debug logs in addition to the default error, warn, and info logs, --log-level sets the maximum verbosity, i.e. when set to error print only errors, if set to warn print warnings and errors, etc. and when set to trace, print all the logs.

If --log-level is selected, this value is used and takes precedence over --debug. RUST_LOG env variable still overrides the flag.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

There were 4 places where an identical change was necessary, perhaps we should consider how to extract all the common CLI flags into a shared component.

Contains a small refactor to logging initialization that eliminates the duplication. Dedicated PR #3208

@Bravo555 Bravo555 marked this pull request as ready for review October 24, 2024 14:53
@Bravo555 Bravo555 changed the title Add --level flag Add --level flag Oct 24, 2024
@Bravo555 Bravo555 changed the title Add --level flag Add --log-level flag Oct 24, 2024
Copy link

codecov bot commented Oct 24, 2024

Codecov Report

Attention: Patch coverage is 0% with 50 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...mon/tedge_config/src/system_services/log_config.rs 0.00% 30 Missing ⚠️
crates/core/tedge_agent/src/lib.rs 0.00% 5 Missing ⚠️
crates/core/tedge_mapper/src/lib.rs 0.00% 5 Missing ⚠️
crates/core/tedge_watchdog/src/lib.rs 0.00% 5 Missing ⚠️
plugins/c8y_firmware_plugin/src/lib.rs 0.00% 5 Missing ⚠️
Additional details and impacted files

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@albinsuresh albinsuresh left a comment

Choose a reason for hiding this comment

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

A few copy paste issues.


let log_level = match log_level {
Some(log_level) => log_level,
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
None => get_log_level("tedge-mapper", &tedge_config_location.tedge_config_root_path)?,


let log_level = match log_level {
Some(log_level) => log_level,
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
None => get_log_level("tedge-watchdog", &tedge_config_location.tedge_config_root_path)?,


let log_level = match log_level {
Some(log_level) => log_level,
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
None => get_log_level("tedge-agent", &tedge_config_location.tedge_config_root_path)?,
None => get_log_level("c8y-firmware-plugin", &tedge_config_location.tedge_config_root_path)?,

Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

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

There were 4 places where an identical change was necessary, perhaps we should consider how to extract all the common CLI flags into a shared component.

Could this be implemented in tedge::main.rs, the multi-call entry for the agent and the mapper?

@reubenmiller
Copy link
Contributor

reubenmiller commented Oct 25, 2024

If --log-level is selected, this value is used and takes precedence over --debug. RUST_LOG env variable still overrides the flag.

Flags should always take precedence over environment variable values...generally flags are often used by users to override the environment which they usually have little control over.

The first non-empty value should be taken (in order):

  1. Flag
  2. Environment variable
  3. Configuration file

@reubenmiller reubenmiller added theme:cli Theme: cli related topics theme:troubleshooting Theme: Troubleshooting and remote control labels Oct 25, 2024
@Bravo555 Bravo555 mentioned this pull request Oct 28, 2024
11 tasks
@Bravo555
Copy link
Contributor Author

Bravo555 commented Oct 28, 2024

Flags should always take precedence over environment variable values...generally flags are often used by users to override the environment which they usually have little control over.

The first non-empty value should be taken (in order):

  1. Flag
  2. Environment variable
  3. Configuration file

Made a dedicated PR that fixes that mistake: #3208

Could this be implemented in tedge::main.rs, the multi-call entry for the agent and the mapper?

That would probably require some more changes because for every component we use a separate CLI parser, but for now #3208 eliminates some duplication.

Copy link
Contributor

github-actions bot commented Oct 28, 2024

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
523 0 2 523 100 1h32m50.914102999s

Flags should always take precedence over environment variables because
are often used by users to override the environment which they usually
have little control over.

To implement this change it was necessary to refactor the code a little
bit as previously set_log_level function was not aware if desired log
level was specifically requested by the user (--debug flag) or just the
default (INFO level). As such, the new log_init function takes into
account all sources that decide what log level we should select: CLI
options, RUST_LOG env variable, and tedge configuration file.

Signed-off-by: Marcel Guzik <[email protected]>
Add a --log-level flag that sets the verbosity of reported logs.

In contrast to --debug, which enables debug logs in addition to the
default error, warn, and info logs, --log-level sets the maximum
verbosity, i.e. when set to error print only errors, if set to warn
print warnings and errors, etc. and when set to trace, print all the
logs.

If --log-level is selected, this value is used and takes precedence over
--debug. RUST_LOG env variable still overrides the flag.

Signed-off-by: Marcel Guzik <[email protected]>
@Bravo555
Copy link
Contributor Author

Although, would it be a better user experience to only allow using either --debug OR --log-level, but not both? It might eliminate some confusion of which overrides the other.

Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

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

Approved

@didier-wenzek
Copy link
Contributor

Although, would it be a better user experience to only allow using either --debug OR --log-level, but not both? It might eliminate some confusion of which overrides the other.

Indeed these two flags overlap. But, I don't see that as a big deal.

@Bravo555 Bravo555 added this pull request to the merge queue Oct 28, 2024
Merged via the queue into thin-edge:main with commit ad03ca3 Oct 28, 2024
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme:cli Theme: cli related topics theme:troubleshooting Theme: Troubleshooting and remote control
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants