2023-06-04 10:43:40 +02:00
|
|
|
use std *
|
|
|
|
use commons.nu *
|
|
|
|
|
|
|
|
def run-command [
|
|
|
|
system_level: string,
|
|
|
|
message: string,
|
|
|
|
format: string,
|
|
|
|
log_level: int,
|
|
|
|
--level-prefix: string,
|
|
|
|
--ansi: string
|
|
|
|
] {
|
|
|
|
do {
|
|
|
|
if ($level_prefix | is-empty) {
|
|
|
|
if ($ansi | is-empty) {
|
|
|
|
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log custom "($message)" "($format)" ($log_level)'
|
|
|
|
} else {
|
|
|
|
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log custom "($message)" "($format)" ($log_level) --ansi "($ansi)"'
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log custom "($message)" "($format)" ($log_level) --level-prefix "($level_prefix)" --ansi "($ansi)"'
|
|
|
|
}
|
|
|
|
} | complete | get --ignore-errors stderr
|
|
|
|
}
|
|
|
|
|
2023-07-02 10:41:33 +02:00
|
|
|
#[test]
|
|
|
|
def errors_during_deduction [] {
|
2023-06-04 10:43:40 +02:00
|
|
|
assert str contains (run-command "DEBUG" "msg" "%MSG%" 25) "Cannot deduce log level prefix for given log level"
|
|
|
|
assert str contains (run-command "DEBUG" "msg" "%MSG%" 25 --ansi (ansi red)) "Cannot deduce log level prefix for given log level"
|
|
|
|
assert str contains (run-command "DEBUG" "msg" "%MSG%" 25 --level-prefix "abc") "Cannot deduce ansi for given log level"
|
|
|
|
}
|
|
|
|
|
2023-07-02 10:41:33 +02:00
|
|
|
#[test]
|
|
|
|
def valid_calls [] {
|
2023-06-04 10:43:40 +02:00
|
|
|
assert equal (run-command "DEBUG" "msg" "%MSG%" 25 --level-prefix "abc" --ansi (ansi default) | str trim --right) "msg"
|
|
|
|
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" 20 | str trim --right) $"($env.LOG_PREFIX.INFO) msg"
|
|
|
|
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" --level-prefix "abc" 20 | str trim --right) "abc msg"
|
|
|
|
assert equal (run-command "INFO" "msg" "%ANSI_START%%LEVEL% %MSG%%ANSI_STOP%" $env.LOG_LEVEL.CRITICAL | str trim --right) $"($env.LOG_ANSI.CRITICAL)CRT msg(ansi reset)"
|
|
|
|
}
|
|
|
|
|
2023-07-02 10:41:33 +02:00
|
|
|
#[test]
|
|
|
|
def log_level_handling [] {
|
2023-06-04 10:43:40 +02:00
|
|
|
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" 20 | str trim --right) $"($env.LOG_PREFIX.INFO) msg"
|
|
|
|
assert equal (run-command "WARNING" "msg" "%LEVEL% %MSG%" 20 | str trim --right) ""
|
2023-06-10 20:16:17 +02:00
|
|
|
}
|