-
Notifications
You must be signed in to change notification settings - Fork 157
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
Conversation
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.
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 introducedlog-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.
How about impl Enum type for log level like below?
|
…com/lablup/backend.ai into fix/add-command-line-logging-option
help="This option will soon change to --log-level TEXT option.", | ||
) | ||
@click.option( | ||
"--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.
FYI: You may narrow down the range of supported values using Choices
.
https://click.palletsprojects.com/en/8.1.x/options/#choice-options
src/ai/backend/agent/server.py
Outdated
) -> int: | ||
|
||
if debug: | ||
print("Please use --log-level options instead") |
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.
It is recommended to use click.echo()
rather than print()
for printing.
https://click.palletsprojects.com/en/8.1.x/utils/#printing-to-stdout
src/ai/backend/agent/server.py
Outdated
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) |
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.
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)
src/ai/backend/agent/watcher.py
Outdated
if log_level not in ["debug", "info", "warning", "error", "critical"]: | ||
click.echo("Undefined 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.
Same here.
src/ai/backend/manager/server.py
Outdated
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) |
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.
Also here.
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 left some comments.
Please ask me if you have any questions!
src/ai/backend/agent/server.py
Outdated
class LogLevel(str, Enum): | ||
debug = "debug" | ||
info = "info" | ||
warning = "warning" | ||
error = "error" | ||
critical = "critical" |
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.
we name members of Enum
as capital letters, like below.
class LogLevel(str, Enum):
DEBUG = "debug"
INFO = "info"
...
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.
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.
src/ai/backend/agent/watcher.py
Outdated
class LogLevel(str, Enum): | ||
debug = "debug" | ||
info = "info" | ||
warning = "warning" | ||
error = "error" | ||
critical = "critical" |
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.
same here
class LogLevel(str, Enum): | ||
debug = "debug" | ||
info = "info" | ||
warning = "warning" | ||
error = "error" | ||
critical = "critical" |
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.
same here
src/ai/backend/manager/server.py
Outdated
class LogLevel(str, Enum): | ||
debug = "debug" | ||
info = "info" | ||
warning = "warning" | ||
error = "error" | ||
critical = "critical" |
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.
same here
src/ai/backend/storage/server.py
Outdated
class LogLevel(str, Enum): | ||
debug = "debug" | ||
info = "info" | ||
warning = "warning" | ||
error = "error" | ||
critical = "critical" |
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.
same here
src/ai/backend/agent/server.py
Outdated
@@ -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": |
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.
if log_level == "debug": | |
if log_level == LogLevel.DEBUG: |
src/ai/backend/agent/watcher.py
Outdated
@click.option( | ||
"--log-level", | ||
type=click.Choice(LogLevel, case_sensitive=False), | ||
default="info", |
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.
Please check the type here
@click.option( | ||
"--log-level", | ||
type=click.Choice(LogLevel, case_sensitive=False), | ||
default="info", |
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.
same here
src/ai/backend/manager/server.py
Outdated
@click.option( | ||
"--log-level", | ||
type=click.Choice(LogLevel, case_sensitive=False), | ||
default="info", |
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.
same here
src/ai/backend/storage/server.py
Outdated
@click.option( | ||
"--log-level", | ||
type=click.Choice(LogLevel, case_sensitive=False), | ||
default="info", |
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.
same here
I reflected all the comments! |
Best way to prevent duplication is to define common code under |
And... it seems there already is an enum which does exactly same thing: |
src/ai/backend/agent/server.py
Outdated
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") |
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.
Since we're now utilizing LogSeverity
to represent log level, please replace all occurrences of raw "debug" string with LogSeverity.DEBUG
.
config.override_key(raw_cfg, ("debug", "enabled"), log_level.value == "debug") | |
config.override_key(raw_cfg, ("debug", "enabled"), log_level== LogSeverity.DEBUG) |
src/ai/backend/agent/server.py
Outdated
@@ -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": |
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.
Same here.
if log_level.value == "debug": | |
if log_level == LogSeverity.DEBUG: |
src/ai/backend/agent/watcher.py
Outdated
@@ -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": |
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.
if log_level.value == "debug": | |
if log_level == LogSeverity.DEBUG: |
src/ai/backend/manager/config.py
Outdated
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") |
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.
config.override_key(raw_cfg, ("debug", "enabled"), log_level == "debug") | |
config.override_key(raw_cfg, ("debug", "enabled"), log_level == LogSeverity.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.
I didn't change this part because log_level is raw string in load function in this file.
src/ai/backend/storage/server.py
Outdated
# 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": |
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.
if log_level.value == "debug": | |
if log_level == LogSeverity.DEBUG: |
…com/lablup/backend.ai into fix/add-command-line-logging-option
"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