From 52604f8b00875207ad2119165319bcef20d2eeab Mon Sep 17 00:00:00 2001 From: Bahex Date: Mon, 16 Jun 2025 19:31:17 +0300 Subject: [PATCH] fix(std/log): Don't assume env variables are set (#15980) # Description Commands in `std/log` assume the `export-env` has been run and the relevant environment variables are set. However, when modules/libraries import `std/log` without defining their own `export-env` block to run `std/log`'s, logging commands will fail at runtime. While it's on the author of the modules to include `export-env { use std/log [] }` in their modules, this is a very simple issue to solve and would make the user experience smoother. # User-Facing Changes `std/log` work without problem when their env vars are not set. --------- Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com> Co-authored-by: 132ikl <132@ikl.sh> --- crates/nu-std/std/log/mod.nu | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/nu-std/std/log/mod.nu b/crates/nu-std/std/log/mod.nu index 19354a615e..b55f2e89bf 100644 --- a/crates/nu-std/std/log/mod.nu +++ b/crates/nu-std/std/log/mod.nu @@ -38,9 +38,14 @@ const LOG_SHORT_PREFIX = { export def log-short-prefix [] {$LOG_SHORT_PREFIX} +const LOG_FORMATS = { + log: "%ANSI_START%%DATE%|%LEVEL%|%MSG%%ANSI_STOP%" + date: "%Y-%m-%dT%H:%M:%S%.3f" +} + export-env { - $env.NU_LOG_FORMAT = $env.NU_LOG_FORMAT? | default "%ANSI_START%%DATE%|%LEVEL%|%MSG%%ANSI_STOP%" - $env.NU_LOG_DATE_FORMAT = $env.NU_LOG_DATE_FORMAT? | default "%Y-%m-%dT%H:%M:%S%.3f" + $env.NU_LOG_FORMAT = $env.NU_LOG_FORMAT? | default $LOG_FORMATS.log + $env.NU_LOG_DATE_FORMAT = $env.NU_LOG_DATE_FORMAT? | default $LOG_FORMATS.date } const LOG_TYPES = { @@ -142,7 +147,7 @@ def current-log-level [] { } def now [] { - date now | format date $env.NU_LOG_DATE_FORMAT + date now | format date ($env.NU_LOG_DATE_FORMAT? | default $LOG_FORMATS.date) } def handle-log [ @@ -151,11 +156,7 @@ def handle-log [ format_string: string, short: bool ] { - let log_format = if ($format_string | is-empty) { - $env.NU_LOG_FORMAT - } else { - $format_string - } + let log_format = $format_string | default -e $env.NU_LOG_FORMAT? | default $LOG_FORMATS.log let prefix = if $short { $formatting.short_prefix