Skip to content

chore: Avoid logging a warning when LOG_LEVEL is unset#4497

Merged
polarathene merged 3 commits intomasterfrom
chore/avoid-warning-for-empty-loglevel
Jun 2, 2025
Merged

chore: Avoid logging a warning when LOG_LEVEL is unset#4497
polarathene merged 3 commits intomasterfrom
chore/avoid-warning-for-empty-loglevel

Conversation

@polarathene
Copy link
Copy Markdown
Member

@polarathene polarathene commented Jun 1, 2025

Description

When LOG_LEVEL is not explicitly set, DMS logs a warning early on:

WARN  start-mailserver.sh: Log level '' is invalid (falling back to default 'info')

That is redundant (should only log when it's assigned an invalid value), so let's configure the fallback earlier and only log this when it has actually been set with an unexpected value.

I've additionally collapsed the series of conditionals against LOG_LEVEL to use a regex instead, then inversed the logic so we don't need a redundant branch.


Reference:

If the assigned value to the LOG_LEVEL ENV is invalid:

  • We log a warning to inform the user like shown above
  • Then modify LOG_LEVEL to info as a valid fallback value:
    • Later assigned to the equivalent VARS key, which will store the variable into /etc/dms-settings (fixes the ENV when shelling into the container or when another service is started by supervisord like change detection or our setup CLI).
    • Updating the LOG_LEVEL variable for any other script that may check it during startup.

The default LOG_LEVEL is effectively already info when unset/invalid (this differs from the actual _log call that takes an arg to classify a log by level before checking LOG_LEVEL to filter):

case "$(_get_log_level_or_default)" in
( 'trace' ) LEVEL_AS_INT=5 ;;
( 'debug' ) LEVEL_AS_INT=4 ;;
( 'warn' ) LEVEL_AS_INT=2 ;;
( 'error' ) LEVEL_AS_INT=1 ;;
( * ) LEVEL_AS_INT=3 ;;
esac

# Get the value of the environment variable LOG_LEVEL if
# it is set. Otherwise, try to query the common environment
# variables file. If this does not yield a value either,
# use the default log level.
function _get_log_level_or_default() {
if [[ -n ${LOG_LEVEL:-} ]]; then
echo "${LOG_LEVEL}"
elif [[ -e /etc/dms-settings ]] && grep -q -E "^LOG_LEVEL='[a-z]+'" /etc/dms-settings; then
grep '^LOG_LEVEL=' /etc/dms-settings | cut -d "'" -f 2
else
echo 'info'
fi
}

Default/invalid LOG_LEVEL would match this:

( * ) LEVEL_AS_INT=3 ;;

Thus we only show error,warn,info logs:

( 'info' )
[[ ${LEVEL_AS_INT} -ge 3 ]] || return 0

For historical PR reference of this logic:

Type of change

  • Improvement (non-breaking change that does improve existing functionality)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes
  • I have added information about changes made in this PR to CHANGELOG.md

Minor internal change, seems unnecessary to document in the changelog.

@polarathene polarathene self-assigned this Jun 1, 2025
@polarathene polarathene added area/scripts kind/improvement Improve an existing feature, configuration file or the documentation labels Jun 1, 2025
@polarathene polarathene requested a review from casperklein June 1, 2025 02:31
georglauterbach
georglauterbach previously approved these changes Jun 1, 2025
casperklein
casperklein previously approved these changes Jun 1, 2025
Copy link
Copy Markdown
Member

@casperklein casperklein left a comment

Choose a reason for hiding this comment

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

One last thing: As the function serves only one purpose, we might rename it to something like:

__environment_variables_validate_log_level or __environment_variables_check_log_level_value

@polarathene polarathene merged commit 8fa6e6d into master Jun 2, 2025
7 checks passed
@polarathene polarathene deleted the chore/avoid-warning-for-empty-loglevel branch June 2, 2025 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/scripts kind/improvement Improve an existing feature, configuration file or the documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants