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

feature: add command line logging option #971

Merged
merged 32 commits into from
Feb 13, 2023

Conversation

leejiwon1125
Copy link
Contributor

@leejiwon1125 leejiwon1125 commented Jan 11, 2023

"mindgitrwx" wanted to use various logging options directly on command line in #299.

But we can only use 'debug' option. (In fact, we can use 'info' option by simply removing "--debug" in ./backend.ai mgr start-server --debug command)

So, I accepted the demand for "mindgitrwx".

From now on, we can use options such as debug, info, warning, error and critical.

To run manager server, type in terminal
./backend.ai mgr start-server --log-level=~ (~ is one of the logging options: debug, info, warning, error, critical)
instead of ./backend.ai mgr start-server --debug

Plus, according to changed code, the default logging option is info so if you just type ./backend.ai mgr start-server in terminal, then logging option is info.

resolves #299

@github-actions github-actions bot added comp:manager Related to Manager component type:feature Add new features effort:easy Need to understand only a specific region of codes (good first issue, easy). labels Jan 11, 2023
@leejiwon1125 leejiwon1125 added this to the 21.03 milestone Jan 11, 2023
Copy link
Member

@kyujin-cho kyujin-cho left a comment

Choose a reason for hiding this comment

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

Looks great! Though there are some changes needs to be done before merging:

  • Removing argument flag at once can cause confusion to users (and of course, this is why modern APIs have somewhat "deprecated" mark). So let's retain debug flag along with newly introduced log-level flag, which acts like --log-level debug option, but warns user that this flag can be removed at any time in future releases.
  • Please add validating logic to log_level argument. We need to halt program when unexpected (that means, some values other than defined log levels) input is received and notify user that they have typed unknown log level.
  • There are other packages other than ai.backend.manager.server which uses same argument scheme (e.g. agent, web, ...). Please expand changes to these packages also.

@leejiwon1125 leejiwon1125 changed the title fix: add command line logging option feature: add command line logging option Jan 16, 2023
@fregataa
Copy link
Member

How about impl Enum type for log level like below?

class LogLevel(str, Enum):
    INFO = "info"
    DEBUG = "debug"
    ...

@github-actions github-actions bot added the comp:storage-proxy Related to Storage proxy component label Jan 19, 2023
help="This option will soon change to --log-level TEXT option.",
)
@click.option(
"--log-level",
Copy link
Member

Choose a reason for hiding this comment

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

FYI: You may narrow down the range of supported values using Choices.

https://click.palletsprojects.com/en/8.1.x/options/#choice-options

) -> int:

if debug:
print("Please use --log-level options instead")
Copy link
Member

@rapsealk rapsealk Jan 19, 2023

Choose a reason for hiding this comment

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

It is recommended to use click.echo() rather than print() for printing.

https://click.palletsprojects.com/en/8.1.x/utils/#printing-to-stdout

Comment on lines 850 to 853
if log_level not in ["debug", "info", "warning", "error", "critical"]:
click.echo("Undefined log-level")
click.echo("Try 'backend.ai ag start-server -h' for help")
exit(1)
Copy link
Member

Choose a reason for hiding this comment

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

Bit of a combination of @fregataa and @rapsealk 's review. How about muxing log levels as an enum and using click's Choice option? That way we can relieve the burden to manually validate log level string user provided.
Ref: pallets/click#605 (comment)

Comment on lines 359 to 361
if log_level not in ["debug", "info", "warning", "error", "critical"]:
click.echo("Undefined log-level")
exit(1)
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

Comment on lines 800 to 803
if log_level not in ["debug", "info", "warning", "error", "critical"]:
click.echo("Undefined log-level")
click.echo("Try 'backend.ai mgr start-server -h' for help")
exit(1)
Copy link
Member

Choose a reason for hiding this comment

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

Also here.

@github-actions github-actions bot added the webui label Jan 27, 2023
Copy link
Member

@fregataa fregataa left a comment

Choose a reason for hiding this comment

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

I left some comments.
Please ask me if you have any questions!

Comment on lines 804 to 809
class LogLevel(str, Enum):
debug = "debug"
info = "info"
warning = "warning"
error = "error"
critical = "critical"
Copy link
Member

Choose a reason for hiding this comment

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

we name members of Enum as capital letters, like below.

class LogLevel(str, Enum):
  DEBUG = "debug"
  INFO = "info"
  ...

Copy link
Contributor Author

@leejiwon1125 leejiwon1125 Jan 30, 2023

Choose a reason for hiding this comment

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

Thank you for your comment!
I wonder if there is any way to reduce duplicated code which means I want to put LogLevel class in such a file that contain classes so that I can import that file to agent/server.py, agent/watcher.py and so on.

Comment on lines 333 to 338
class LogLevel(str, Enum):
debug = "debug"
info = "info"
warning = "warning"
error = "error"
critical = "critical"
Copy link
Member

Choose a reason for hiding this comment

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

same here

Comment on lines 28 to 33
class LogLevel(str, Enum):
debug = "debug"
info = "info"
warning = "warning"
error = "error"
critical = "critical"
Copy link
Member

Choose a reason for hiding this comment

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

same here

Comment on lines 772 to 777
class LogLevel(str, Enum):
debug = "debug"
info = "info"
warning = "warning"
error = "error"
critical = "critical"
Copy link
Member

Choose a reason for hiding this comment

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

same here

Comment on lines 154 to 159
class LogLevel(str, Enum):
debug = "debug"
info = "info"
warning = "warning"
error = "error"
critical = "critical"
Copy link
Member

Choose a reason for hiding this comment

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

same here

@@ -920,7 +942,7 @@ def main(
log.info("runtime: {0}", utils.env_info())

log_config = logging.getLogger("ai.backend.agent.config")
if debug:
if log_level == "debug":
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
if log_level == "debug":
if log_level == LogLevel.DEBUG:

@click.option(
"--log-level",
type=click.Choice(LogLevel, case_sensitive=False),
default="info",
Copy link
Member

Choose a reason for hiding this comment

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

Please check the type here

@click.option(
"--log-level",
type=click.Choice(LogLevel, case_sensitive=False),
default="info",
Copy link
Member

Choose a reason for hiding this comment

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

same here

@click.option(
"--log-level",
type=click.Choice(LogLevel, case_sensitive=False),
default="info",
Copy link
Member

Choose a reason for hiding this comment

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

same here

@click.option(
"--log-level",
type=click.Choice(LogLevel, case_sensitive=False),
default="info",
Copy link
Member

Choose a reason for hiding this comment

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

same here

@leejiwon1125
Copy link
Contributor Author

I reflected all the comments!
According to my check, there is no problem in terms of function.
But I think the 'LogLevel' class repeated several times.
Is there any way to compress this scattered code into one?

@kyujin-cho
Copy link
Member

I reflected all the comments! According to my check, there is no problem in terms of function. But I think the 'LogLevel' class repeated several times. Is there any way to compress this scattered code into one?

Best way to prevent duplication is to define common code under ai.backend.common.types package and import it on anywhere needed.

@kyujin-cho
Copy link
Member

And... it seems there already is an enum which does exactly same thing: ai.backend.common.types.LogSeverity. Let's utilize this Enum instead of creating new one.

@github-actions github-actions bot added the webui label Jan 30, 2023
config.override_key(raw_cfg, ("logging", "level"), "DEBUG")
config.override_key(raw_cfg, ("logging", "pkg-ns", "ai.backend"), "DEBUG")

config.override_key(raw_cfg, ("debug", "enabled"), log_level.value == "debug")
Copy link
Member

Choose a reason for hiding this comment

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

Since we're now utilizing LogSeverity to represent log level, please replace all occurrences of raw "debug" string with LogSeverity.DEBUG.

Suggested change
config.override_key(raw_cfg, ("debug", "enabled"), log_level.value == "debug")
config.override_key(raw_cfg, ("debug", "enabled"), log_level== LogSeverity.DEBUG)

@@ -920,7 +935,7 @@ def main(
log.info("runtime: {0}", utils.env_info())

log_config = logging.getLogger("ai.backend.agent.config")
if debug:
if log_level.value == "debug":
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

Suggested change
if log_level.value == "debug":
if log_level == LogSeverity.DEBUG:

@@ -383,7 +397,7 @@ def main(cli_ctx, config_path, debug):
config.override_with_env(
raw_cfg, ("watcher", "service-addr", "port"), "BACKEND_WATCHER_SERVICE_PORT"
)
if debug:
if log_level.value == "debug":
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
if log_level.value == "debug":
if log_level == LogSeverity.DEBUG:

config.override_key(raw_cfg, ("logging", "pkg-ns", "ai.backend"), "DEBUG")
config.override_key(raw_cfg, ("logging", "pkg-ns", "aiohttp"), "DEBUG")

config.override_key(raw_cfg, ("debug", "enabled"), log_level == "debug")
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
config.override_key(raw_cfg, ("debug", "enabled"), log_level == "debug")
config.override_key(raw_cfg, ("debug", "enabled"), log_level == LogSeverity.DEBUG)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't change this part because log_level is raw string in load function in this file.

# Determine where to read configuration.
raw_cfg, cfg_src_path = config.read_from_file(config_path, "storage-proxy")

config.override_with_env(raw_cfg, ("etcd", "namespace"), "BACKEND_NAMESPACE")
config.override_with_env(raw_cfg, ("etcd", "addr"), "BACKEND_ETCD_ADDR")
config.override_with_env(raw_cfg, ("etcd", "user"), "BACKEND_ETCD_USER")
config.override_with_env(raw_cfg, ("etcd", "password"), "BACKEND_ETCD_PASSWORD")
if debug:
if log_level.value == "debug":
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
if log_level.value == "debug":
if log_level == LogSeverity.DEBUG:

@kyujin-cho kyujin-cho merged commit d011e8d into main Feb 13, 2023
@kyujin-cho kyujin-cho deleted the fix/add-command-line-logging-option branch February 13, 2023 05:03
@Yaminyam Yaminyam added the size:L 100~500 LoC label Apr 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:manager Related to Manager component comp:storage-proxy Related to Storage proxy component effort:easy Need to understand only a specific region of codes (good first issue, easy). size:L 100~500 LoC type:feature Add new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add command line logging option
5 participants