mirror of
https://github.com/nushell/nushell.git
synced 2025-08-23 19:47:21 +02:00
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>
This commit is contained in:
@@ -38,9 +38,14 @@ const LOG_SHORT_PREFIX = {
|
|||||||
|
|
||||||
export def log-short-prefix [] {$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 {
|
export-env {
|
||||||
$env.NU_LOG_FORMAT = $env.NU_LOG_FORMAT? | default "%ANSI_START%%DATE%|%LEVEL%|%MSG%%ANSI_STOP%"
|
$env.NU_LOG_FORMAT = $env.NU_LOG_FORMAT? | default $LOG_FORMATS.log
|
||||||
$env.NU_LOG_DATE_FORMAT = $env.NU_LOG_DATE_FORMAT? | default "%Y-%m-%dT%H:%M:%S%.3f"
|
$env.NU_LOG_DATE_FORMAT = $env.NU_LOG_DATE_FORMAT? | default $LOG_FORMATS.date
|
||||||
}
|
}
|
||||||
|
|
||||||
const LOG_TYPES = {
|
const LOG_TYPES = {
|
||||||
@@ -142,7 +147,7 @@ def current-log-level [] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def now [] {
|
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 [
|
def handle-log [
|
||||||
@@ -151,11 +156,7 @@ def handle-log [
|
|||||||
format_string: string,
|
format_string: string,
|
||||||
short: bool
|
short: bool
|
||||||
] {
|
] {
|
||||||
let log_format = if ($format_string | is-empty) {
|
let log_format = $format_string | default -e $env.NU_LOG_FORMAT? | default $LOG_FORMATS.log
|
||||||
$env.NU_LOG_FORMAT
|
|
||||||
} else {
|
|
||||||
$format_string
|
|
||||||
}
|
|
||||||
|
|
||||||
let prefix = if $short {
|
let prefix = if $short {
|
||||||
$formatting.short_prefix
|
$formatting.short_prefix
|
||||||
|
Reference in New Issue
Block a user