-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
Why? Configuring the logs in the vdk_initialize hook causes commands like vdk server and vdk info to have the same verbose logging format as data job logs. What? - Put the log configuration for vdk_initialize behind a config option and disable it by default for local runs and enable it by default for cloud runs. Configuring the logging format in vdk_initialize might still be useful for cloud runs and we don't care about other vdk commands output in cloud environments - Move config classes and functions into separate file How was this tested Manually CI/CD What kind of change is this? Feature/non-breaking Signed-off-by: Dilyan Marinov <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# Copyright 2021-2024 VMware, Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from vdk.internal.builtin_plugins.config import vdk_config | ||
from vdk.internal.core.config import Configuration | ||
from vdk.internal.core.config import ConfigurationBuilder | ||
from vdk.plugin.structlog.constants import * | ||
|
||
|
||
class StructlogConfig: | ||
def __init__(self, configuration: Configuration): | ||
presets = { | ||
"LOCAL": { | ||
STRUCTLOG_USE_STRUCTLOG: configuration.get_value( | ||
STRUCTLOG_USE_STRUCTLOG | ||
), | ||
STRUCTLOG_LOGGING_METADATA_KEY: ",".join( | ||
Check notice on line 16 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
list(JSON_STRUCTLOG_LOGGING_METADATA_DEFAULT.keys()) | ||
), | ||
STRUCTLOG_LOGGING_FORMAT_KEY: STRUCTLOG_LOGGING_FORMAT_DEFAULT, | ||
Check notice on line 19 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
STRUCTLOG_CONSOLE_LOG_PATTERN: "", | ||
STRUCTLOG_CONFIG_PRESET: configuration.get_value( | ||
Check notice on line 21 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
STRUCTLOG_CONFIG_PRESET | ||
Check notice on line 22 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
), | ||
SYSLOG_HOST_KEY: DEFAULT_SYSLOG_HOST, | ||
SYSLOG_PORT_KEY: DEFAULT_SYSLOG_PORT, | ||
Check notice on line 25 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
SYSLOG_PROTOCOL_KEY: DEFAULT_SYSLOG_PROTOCOL, | ||
SYSLOG_ENABLED_KEY: DEFAULT_SYSLOG_ENABLED, | ||
Check notice on line 27 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
vdk_config.LOG_LEVEL_VDK.lower(): configuration.get_value( | ||
vdk_config.LOG_LEVEL_VDK.lower() | ||
), | ||
vdk_config.LOG_LEVEL_MODULE.lower(): configuration.get_value( | ||
vdk_config.LOG_LEVEL_MODULE.lower() | ||
), | ||
STRUCTLOG_FORMAT_INIT_LOGS: False, | ||
Check notice on line 34 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
}, | ||
"CLOUD": { | ||
STRUCTLOG_USE_STRUCTLOG: configuration.get_value( | ||
STRUCTLOG_USE_STRUCTLOG | ||
), | ||
STRUCTLOG_LOGGING_METADATA_KEY: "", | ||
Check notice on line 40 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
STRUCTLOG_LOGGING_FORMAT_KEY: STRUCTLOG_LOGGING_FORMAT_DEFAULT, | ||
STRUCTLOG_CONSOLE_LOG_PATTERN: DETAILED_LOGGING_FORMAT, | ||
STRUCTLOG_CONFIG_PRESET: configuration.get_value( | ||
STRUCTLOG_CONFIG_PRESET | ||
), | ||
SYSLOG_HOST_KEY: DEFAULT_SYSLOG_HOST, | ||
SYSLOG_PORT_KEY: DEFAULT_SYSLOG_PORT, | ||
SYSLOG_PROTOCOL_KEY: DEFAULT_SYSLOG_PROTOCOL, | ||
SYSLOG_ENABLED_KEY: DEFAULT_SYSLOG_ENABLED, | ||
vdk_config.LOG_LEVEL_VDK.lower(): configuration.get_value( | ||
vdk_config.LOG_LEVEL_VDK.lower() | ||
), | ||
vdk_config.LOG_LEVEL_MODULE.lower(): configuration.get_value( | ||
vdk_config.LOG_LEVEL_MODULE.lower() | ||
), | ||
STRUCTLOG_FORMAT_INIT_LOGS: True, | ||
Check notice on line 56 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
}, | ||
} | ||
|
||
self._config = presets[configuration.get_value(STRUCTLOG_CONFIG_PRESET)] | ||
Check notice on line 60 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
|
||
for key in configuration.list_config_keys(): | ||
if not configuration.is_default(key): | ||
self._config[key] = configuration.get_value(key) | ||
|
||
def get_use_structlog(self) -> bool: | ||
return self._config[STRUCTLOG_USE_STRUCTLOG] | ||
|
||
def get_structlog_logging_metadata(self) -> str: | ||
return self._config[STRUCTLOG_LOGGING_METADATA_KEY] | ||
Check notice on line 70 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
|
||
def get_structlog_logging_format(self) -> str: | ||
return self._config[STRUCTLOG_LOGGING_FORMAT_KEY] | ||
|
||
def get_structlog_console_log_pattern(self) -> str: | ||
return self._config[STRUCTLOG_CONSOLE_LOG_PATTERN] | ||
|
||
def get_structlog_config_preset(self) -> str: | ||
return self._config[STRUCTLOG_CONFIG_PRESET] | ||
|
||
def get_syslog_host(self) -> str: | ||
return self._config[SYSLOG_HOST_KEY] | ||
|
||
def get_syslog_port(self) -> int: | ||
return self._config[SYSLOG_PORT_KEY] | ||
Check notice on line 85 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
|
||
def get_syslog_protocol(self) -> str: | ||
return self._config[SYSLOG_PROTOCOL_KEY] | ||
|
||
def get_syslog_enabled(self) -> bool: | ||
return self._config[SYSLOG_ENABLED_KEY] | ||
|
||
def get_log_level_vdk(self) -> str: | ||
return self._config[vdk_config.LOG_LEVEL_VDK.lower()] | ||
|
||
def get_log_level_module(self) -> str: | ||
return self._config[vdk_config.LOG_LEVEL_MODULE.lower()] | ||
|
||
def get_format_init_logs(self) -> str: | ||
return self._config[STRUCTLOG_FORMAT_INIT_LOGS] | ||
Check notice on line 100 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
|
||
|
||
def add_definitions(config_builder: ConfigurationBuilder): | ||
config_builder.add( | ||
key=STRUCTLOG_LOGGING_METADATA_KEY, | ||
Check notice on line 105 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
default_value=",".join(list(JSON_STRUCTLOG_LOGGING_METADATA_DEFAULT.keys())), | ||
description=( | ||
f"Possible values: {STRUCTLOG_LOGGING_METADATA_ALL_KEYS}" | ||
Check notice on line 108 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
"User-defined key-value pairs added to the logger's context will be displayed after the metadata, " | ||
"but before the message" | ||
"Keys for user-defined key-value pairs have to be added in this config option for the values to be " | ||
"displayed in the metadata" | ||
), | ||
) | ||
|
||
config_builder.add( | ||
key=STRUCTLOG_CONSOLE_LOG_PATTERN, | ||
Check notice on line 117 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
default_value="", | ||
description="Custom format string for console logging. Leave empty for default format.", | ||
) | ||
|
||
config_builder.add( | ||
key=STRUCTLOG_LOGGING_FORMAT_KEY, | ||
Check notice on line 123 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
default_value=STRUCTLOG_LOGGING_FORMAT_DEFAULT, | ||
Check notice on line 124 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
description=( | ||
f"Controls the logging output format. Possible values: {STRUCTLOG_LOGGING_FORMAT_POSSIBLE_VALUES}" | ||
Check notice on line 126 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
), | ||
) | ||
|
||
config_builder.add( | ||
key=SYSLOG_HOST_KEY, | ||
Check notice on line 131 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
default_value=DEFAULT_SYSLOG_HOST, | ||
Check notice on line 132 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
description="Hostname of the Syslog server.", | ||
) | ||
|
||
config_builder.add( | ||
key=SYSLOG_PORT_KEY, | ||
Check notice on line 137 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
default_value=DEFAULT_SYSLOG_PORT, | ||
description="Port of the Syslog server.", | ||
) | ||
|
||
config_builder.add( | ||
key=SYSLOG_PROTOCOL_KEY, | ||
default_value=DEFAULT_SYSLOG_PROTOCOL, | ||
Check notice on line 144 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
description="Syslog protocol (UDP or TCP).", | ||
) | ||
|
||
config_builder.add( | ||
key=SYSLOG_ENABLED_KEY, | ||
default_value=DEFAULT_SYSLOG_ENABLED, | ||
Check notice on line 150 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
description="Enable Syslog logging (True or False).", | ||
) | ||
|
||
config_builder.add( | ||
key=STRUCTLOG_CONFIG_PRESET, | ||
Check notice on line 155 in projects/vdk-plugins/vdk-structlog/src/vdk/plugin/structlog/structlog_config.py
|
||
default_value="LOCAL", | ||
description="Choose configuration preset. Any config options set together with the preset will override " | ||
"the preset options. Available presets: LOCAL, CLOUD", | ||
) | ||
|
||
config_builder.add( | ||
key=STRUCTLOG_USE_STRUCTLOG, | ||
default_value=True, | ||
description="Use the structlog logging config instead of using the one in vdk-core", | ||
) | ||
|
||
config_builder.add( | ||
key=STRUCTLOG_FORMAT_INIT_LOGS, | ||
default_value=False, | ||
description="Set to True to apply structlog formatting options to the vdk initialization logs", | ||
) |