Logger constants refactored, format argument added, better formatting of failed (non) equality assertions (#9315)

# Description
I have (hopefully) simplified the `log.nu` internal structure and added
customizable log format for all `log` commands

# User-Facing Changes
- [x] Replaced constants with env records for: 
    - ansi (newly added)
    - log level
    - prefix
    - short prefix
- [x] Added `format` argument to all log commands
- [x] Assertions for (not) equality (equal, not equal, greater,
lesser...) now put left and right values inside `'` quotes, so the
assertions for strings are more meaningful
- [x] Documented the %-formatting of log messages

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

---------

Co-authored-by: amtoine <stevan.antoine@gmail.com>
This commit is contained in:
Kamil 2023-06-04 10:43:40 +02:00 committed by GitHub
parent 7ca62b7b35
commit df15fc24fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 445 additions and 330 deletions

View file

@ -143,7 +143,7 @@ export def equal [left: any, right: any, message?: string] {
main ($left == $right) $message --error-label {
start: (metadata $left).span.start
end: (metadata $right).span.end
text: $"They are not equal. Left = ($left). Right = ($right)."
text: $"They are not equal. Left = '($left)'. Right = '($right)'."
}
}
@ -160,7 +160,7 @@ export def "not equal" [left: any, right: any, message?: string] {
main ($left != $right) $message --error-label {
start: (metadata $left).span.start
end: (metadata $right).span.end
text: $"They both are ($left)."
text: $"They both are '($left)'."
}
}
@ -177,7 +177,7 @@ export def "less or equal" [left: any, right: any, message?: string] {
main ($left <= $right) $message --error-label {
start: (metadata $left).span.start
end: (metadata $right).span.end
text: $"Left: ($left), Right: ($right)"
text: $"Left: '($left)', Right: '($right)'"
}
}
@ -193,7 +193,7 @@ export def less [left: any, right: any, message?: string] {
main ($left < $right) $message --error-label {
start: (metadata $left).span.start
end: (metadata $right).span.end
text: $"Left: ($left), Right: ($right)"
text: $"Left: '($left)', Right: '($right)'"
}
}
@ -209,7 +209,7 @@ export def greater [left: any, right: any, message?: string] {
main ($left > $right) $message --error-label {
start: (metadata $left).span.start
end: (metadata $right).span.end
text: $"Left: ($left), Right: ($right)"
text: $"Left: '($left)', Right: '($right)'"
}
}
@ -226,7 +226,7 @@ export def "greater or equal" [left: any, right: any, message?: string] {
main ($left >= $right) $message --error-label {
start: (metadata $left).span.start
end: (metadata $right).span.end
text: $"Left: ($left), Right: ($right)"
text: $"Left: '($left)', Right: '($right)'"
}
}

View file

@ -1,128 +1,135 @@
export def CRITICAL_LEVEL [] {
50
export-env {
let-env LOG_ANSI = {
"CRITICAL": (ansi red_bold),
"ERROR": (ansi red),
"WARNING": (ansi yellow),
"INFO": (ansi default),
"DEBUG": (ansi default_dimmed)
}
let-env LOG_LEVEL = {
"CRITICAL": 50,
"ERROR": 40,
"WARNING": 30,
"INFO": 20,
"DEBUG": 10
}
let-env LOG_PREFIX = {
"CRITICAL": "CRT",
"ERROR": "ERR",
"WARNING": "WRN",
"INFO": "INF",
"DEBUG": "DBG"
}
let-env LOG_SHORT_PREFIX = {
"CRITICAL": "C",
"ERROR": "E",
"WARNING": "W",
"INFO": "I",
"DEBUG": "D"
}
let-env LOG_FORMAT = $"%ANSI_START%%DATE%|%LEVEL%|(ansi u)%MSG%%ANSI_STOP%"
}
export def ERROR_LEVEL [] {
40
def log-types [] {
(
{
"CRITICAL": {
"ansi": $env.LOG_ANSI.CRITICAL,
"level": $env.LOG_LEVEL.CRITICAL,
"prefix": $env.LOG_PREFIX.CRITICAL,
"short_prefix": $env.LOG_SHORT_PREFIX.CRITICAL
},
"ERROR": {
"ansi": $env.LOG_ANSI.ERROR,
"level": $env.LOG_LEVEL.ERROR,
"prefix": $env.LOG_PREFIX.ERROR,
"short_prefix": $env.LOG_SHORT_PREFIX.ERROR
},
"WARNING": {
"ansi": $env.LOG_ANSI.WARNING,
"level": $env.LOG_LEVEL.WARNING,
"prefix": $env.LOG_PREFIX.WARNING,
"short_prefix": $env.LOG_SHORT_PREFIX.WARNING
},
"INFO": {
"ansi": $env.LOG_ANSI.INFO,
"level": $env.LOG_LEVEL.INFO,
"prefix": $env.LOG_PREFIX.INFO,
"short_prefix": $env.LOG_SHORT_PREFIX.INFO
},
"DEBUG": {
"ansi": $env.LOG_ANSI.DEBUG,
"level": $env.LOG_LEVEL.DEBUG,
"prefix": $env.LOG_PREFIX.DEBUG,
"short_prefix": $env.LOG_SHORT_PREFIX.DEBUG
}
}
)
}
export def WARNING_LEVEL [] {
30
}
export def INFO_LEVEL [] {
20
}
export def DEBUG_LEVEL [] {
10
}
def parse-string-level [
level: string
] {
if $level in [(CRITICAL_LEVEL_PREFIX) (CRITICAL_LEVEL_PREFIX --short) "CRIT" "CRITICAL"] {
CRITICAL_LEVEL
} else if $level in [(ERROR_LEVEL_PREFIX) (ERROR_LEVEL_PREFIX --short) "ERROR" ] {
ERROR_LEVEL
} else if $level in [(WARNING_LEVEL_PREFIX) (WARNING_LEVEL_PREFIX --short) "WARN" "WARNING"] {
WARNING_LEVEL
} else if $level in [(DEBUG_LEVEL_PREFIX) (DEBUG_LEVEL_PREFIX --short) "DEBUG"] {
DEBUG_LEVEL
let level = ($level | str upcase)
if $level in [$env.LOG_PREFIX.CRITICAL $env.LOG_SHORT_PREFIX.CRITICAL "CRIT" "CRITICAL"] {
$env.LOG_LEVEL.CRITICAL
} else if $level in [$env.LOG_PREFIX.ERROR $env.LOG_SHORT_PREFIX.ERROR "ERROR"] {
$env.LOG_LEVEL.ERROR
} else if $level in [$env.LOG_PREFIX.WARNING $env.LOG_SHORT_PREFIX.WARNING "WARN" "WARNING"] {
$env.LOG_LEVEL.WARNING
} else if $level in [$env.LOG_PREFIX.DEBUG $env.LOG_SHORT_PREFIX.DEBUG "DEBUG"] {
$env.LOG_LEVEL.DEBUG
} else {
INFO_LEVEL
$env.LOG_LEVEL.INFO
}
}
export def CRITICAL_LEVEL_PREFIX [
--short (-s)
] {
if $short {
"C"
} else {
"CRT"
}
}
export def ERROR_LEVEL_PREFIX [
--short (-s)
] {
if $short {
"E"
} else {
"ERR"
}
}
export def WARNING_LEVEL_PREFIX [
--short (-s)
] {
if $short {
"W"
} else {
"WRN"
}
}
export def INFO_LEVEL_PREFIX [
--short (-s)
] {
if $short {
"I"
} else {
"INF"
}
}
export def DEBUG_LEVEL_PREFIX [
--short (-s)
] {
if $short {
"D"
} else {
"DBG"
}
}
def parse-int-level [
level: int,
--short (-s)
] {
if $level >= (CRITICAL_LEVEL) {
if $level >= $env.LOG_LEVEL.CRITICAL {
if $short {
CRITICAL_LEVEL_PREFIX --short
$env.LOG_SHORT_PREFIX.CRITICAL
} else {
CRITICAL_LEVEL_PREFIX
$env.LOG_PREFIX.CRITICAL
}
} else if $level >= (ERROR_LEVEL) {
} else if $level >= $env.LOG_LEVEL.ERROR {
if $short {
ERROR_LEVEL_PREFIX --short
$env.LOG_SHORT_PREFIX.ERROR
} else {
ERROR_LEVEL_PREFIX
$env.LOG_PREFIX.ERROR
}
} else if $level >= (WARNING_LEVEL) {
} else if $level >= $env.LOG_LEVEL.WARNING {
if $short {
WARNING_LEVEL_PREFIX --short
$env.LOG_SHORT_PREFIX.WARNING
} else {
WARNING_LEVEL_PREFIX
$env.LOG_PREFIX.WARNING
}
} else if $level >= (INFO_LEVEL) {
} else if $level >= $env.LOG_LEVEL.INFO {
if $short {
INFO_LEVEL_PREFIX --short
$env.LOG_SHORT_PREFIX.INFO
} else {
INFO_LEVEL_PREFIX
$env.LOG_PREFIX.INFO
}
} else {
if $short {
DEBUG_LEVEL_PREFIX --short
$env.LOG_SHORT_PREFIX.DEBUG
} else {
DEBUG_LEVEL_PREFIX
$env.LOG_PREFIX.DEBUG
}
}
}
def current-log-level [] {
let env_level = ($env.NU_LOG_LEVEL? | default (INFO_LEVEL))
let env_level = ($env.NU_LOG_LEVEL? | default ($env.LOG_LEVEL.INFO))
try {
$env_level | into int
@ -135,128 +142,160 @@ def now [] {
date now | date format "%Y-%m-%dT%H:%M:%S%.3f"
}
def log-formatted [
color: string,
prefix: string,
message: string
def handle-log [
message: string,
formatting: record,
format_string: string,
short: bool
] {
print --stderr $"($color)(now)|($prefix)|(ansi u)($message)(ansi reset)"
let log_format = if ($format_string | is-empty) {
$env.LOG_FORMAT
} else {
$format_string
}
let prefix = if $short {
$formatting.short_prefix
} else {
$formatting.prefix
}
custom $message $log_format $formatting.level --level-prefix $prefix --ansi $formatting.ansi
}
# Logging module
#
# Log formatting placeholders:
# - %MSG%: message to be logged
# - %DATE%: date of log
# - %LEVEL%: string prefix for the log level
# - %ANSI_START%: ansi formatting
# - %ANSI_STOP%: literally (ansi reset)
#
# Note: All placeholders are optional, so "" is still a valid format
#
# Example: $"%ANSI_START%%DATE%|%LEVEL%|(ansi u)%MSG%%ANSI_STOP%"
export def main [] {}
# Log a critical message
export def critical [
message: string, # A message
--short (-s) # Whether to use a short prefix
--format (-f): string # A format (for further reference: help std log)
] {
if (current-log-level) > (CRITICAL_LEVEL) {
return
}
let prefix = if $short {
CRITICAL_LEVEL_PREFIX --short
} else {
CRITICAL_LEVEL_PREFIX
}
log-formatted (ansi red_bold) $prefix $message
handle-log $message (log-types | get CRITICAL) $format $short
}
# Log an error message
export def error [
message: string, # A message
--short (-s) # Whether to use a short prefix
--format (-f): string # A format (for further reference: help std log)
] {
if (current-log-level) > (ERROR_LEVEL) {
return
}
let prefix = if $short {
ERROR_LEVEL_PREFIX --short
} else {
ERROR_LEVEL_PREFIX
}
log-formatted (ansi red) $prefix $message
handle-log $message (log-types | get ERROR) $format $short
}
# Log a warning message
export def warning [
message: string, # A message
--short (-s) # Whether to use a short prefix
--format (-f): string # A format (for further reference: help std log)
] {
if (current-log-level) > (WARNING_LEVEL) {
return
}
let prefix = if $short {
WARNING_LEVEL_PREFIX --short
} else {
WARNING_LEVEL_PREFIX
}
log-formatted (ansi yellow) $prefix $message
handle-log $message (log-types | get WARNING) $format $short
}
# Log an info message
export def info [
message: string, # A message
--short (-s) # Whether to use a short prefix
--format (-f): string # A format (for further reference: help std log)
] {
if (current-log-level) > (INFO_LEVEL) {
return
}
let prefix = if $short {
INFO_LEVEL_PREFIX --short
} else {
INFO_LEVEL_PREFIX
}
log-formatted (ansi default) $prefix $message
handle-log $message (log-types | get INFO) $format $short
}
# Log a debug message
export def debug [
message: string, # A message
--short (-s) # Whether to use a short prefix
--format (-f): string # A format (for further reference: help std log)
] {
if (current-log-level) > (DEBUG_LEVEL) {
return
}
let prefix = if $short {
DEBUG_LEVEL_PREFIX --short
} else {
DEBUG_LEVEL_PREFIX
}
log-formatted (ansi default_dimmed) $prefix $message
handle-log $message (log-types | get DEBUG) $format $short
}
# Log a message with a specific format and verbosity level
#
# Format reference:
# - %MSG% will be replaced by $message
# - %DATE% will be replaced by the timestamp of log in standard Nushell's log format: "%Y-%m-%dT%H:%M:%S%.3f"
# - %LEVEL% will be replaced by the standard Nushell's log verbosity prefixes, e.g. "CRT"
#
# Examples:
# - std log custom "my message" $"(ansi yellow)[%LEVEL%]MY MESSAGE: %MSG% [%DATE%](ansi reset)" (std log WARNING_LEVEL)
def log-level-deduction-error [
type: string
span: record<start: int, end: int>
log_level: int
] {
error make {
msg: $"(ansi red_bold)Cannot deduce ($type) for given log level: ($log_level).(ansi reset)"
label: {
text: ([
"Invalid log level."
$" Available log levels in $env.LOG_LEVEL:"
($env.LOG_LEVEL | to text | lines | each {|it| $" ($it)" } | to text)
] | str join "\n")
start: $span.start
end: $span.end
}
}
}
# Log a message with a specific format and verbosity level, with either configurable or auto-deduced %LEVEL% and %ANSI_START% placeholder extensions
export def custom [
message: string, # A message
format: string, # A format
log_level: int # A log level
--short (-s) # Whether to use a short prefix
format: string, # A format (for further reference: help std log)
log_level: int # A log level (has to be one of the $env.LOG_LEVEL values for correct ansi/prefix deduction)
--level-prefix (-p): string # %LEVEL% placeholder extension
--ansi (-a): string # %ANSI_START% placeholder extension
] {
if (current-log-level) > ($log_level) {
return
}
let level = ((if $short {
parse-int-level $log_level --short
} else {
let valid_levels_for_defaulting = [
$env.LOG_LEVEL.CRITICAL
$env.LOG_LEVEL.ERROR
$env.LOG_LEVEL.WARNING
$env.LOG_LEVEL.INFO
$env.LOG_LEVEL.DEBUG
]
let prefix = if ($level_prefix | is-empty) {
if ($log_level not-in $valid_levels_for_defaulting) {
log-level-deduction-error "log level prefix" (metadata $log_level).span $log_level
}
parse-int-level $log_level
}) | into string)
} else {
$level_prefix
}
let ansi = if ($ansi | is-empty) {
if ($log_level not-in $valid_levels_for_defaulting) {
log-level-deduction-error "ansi" (metadata $log_level).span $log_level
}
(
log-types
| values
| each {|record|
if ($record.level == $log_level) {
$record.ansi
}
} | first
)
} else {
$ansi
}
print --stderr ([
["%MSG%" $message]
["%DATE%" (now)]
["%LEVEL%" $level]
["%LEVEL%" $prefix]
["%ANSI_START%" $ansi]
["%ANSI_STOP%" (ansi reset)]
] | reduce --fold $format {
|it, acc| $acc | str replace --all $it.0 $it.1
})

View file

@ -2,6 +2,7 @@
export-env {
use dirs.nu []
use log.nu []
}
use dt.nu [datetime-diff, pretty-print-duration]

View file

@ -0,0 +1,20 @@
export def now [] {
date now | date format "%Y-%m-%dT%H:%M:%S%.3f"
}
export def format-message [
message: string,
format: string
prefix: string,
ansi
] {
[
["%MSG%" $message]
["%DATE%" (now)]
["%LEVEL%" $prefix]
["%ANSI_START%" $ansi]
["%ANSI_STOP%" (ansi reset)]
] | reduce --fold $format {
|it, acc| $acc | str replace --all $it.0 $it.1
}
}

View file

@ -0,0 +1,88 @@
use std *
def run [
system_level,
message_level
--short
] {
do {
if $short {
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) --short "test message"'
} else {
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) "test message"'
}
} | complete | get --ignore-errors stderr
}
def "assert no message" [
system_level,
message_level
] {
let output = (run $system_level $message_level)
assert equal "" $output
}
def "assert message" [
system_level,
message_level,
message_level_str
] {
let output = (run $system_level $message_level)
assert str contains $output $message_level_str
assert str contains $output "test message"
}
def "assert message short" [
system_level,
message_level,
message_level_str
] {
let output = (run --short $system_level $message_level)
assert str contains $output $message_level_str
assert str contains $output "test message"
}
export def test_critical [] {
assert no message 99 critical
assert message CRITICAL critical CRT
}
export def test_critical_short [] {
assert message short CRITICAL critical C
}
export def test_error [] {
assert no message CRITICAL error
assert message ERROR error ERR
}
export def test_error_short [] {
assert message short ERROR error E
}
export def test_warning [] {
assert no message ERROR warning
assert message WARNING warning WRN
}
export def test_warning_short [] {
assert message short WARNING warning W
}
export def test_info [] {
assert no message WARNING info
assert message INFO info "INF" # INF has to be quoted, otherwise it is the `inf` float
}
export def test_info_short [] {
assert message short INFO info I
}
export def test_debug [] {
assert no message INFO debug
assert message DEBUG debug DBG
}
export def test_debug_short [] {
assert message short DEBUG debug D
}

View file

@ -0,0 +1,41 @@
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
}
export def test_errors_during_deduction [] {
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"
}
export def test_valid_calls [] {
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)"
}
export def test_log_level_handling [] {
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) ""
}

View file

@ -0,0 +1,53 @@
use std *
use commons.nu *
def run-command [
system_level,
message_level,
message,
--format: string,
--short
] {
do {
if $short {
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) --format "($format)" --short "($message)"'
} else {
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) --format "($format)" "($message)"'
}
} | complete | get --ignore-errors stderr
}
def "assert formatted" [
message: string,
format: string,
command_level: string
--short
] {
let output = (run-command "debug" $command_level $message --format $format)
let prefix = if $short {
($env.LOG_SHORT_PREFIX | get ($command_level | str upcase))
} else {
($env.LOG_PREFIX | get ($command_level | str upcase))
}
let ansi = if $short {
($env.LOG_ANSI | get ($command_level | str upcase))
} else {
($env.LOG_ANSI | get ($command_level | str upcase))
}
assert equal ($output | str trim --right) (format-message $message $format $prefix $ansi)
}
export def "test_format_flag" [] {
assert formatted "test" "25 %MSG% %ANSI_START% %LEVEL%%ANSI_STOP%" critical
assert formatted "test" "25 %MSG% %ANSI_START% %LEVEL%%ANSI_STOP%" error
assert formatted "test" "25 %MSG% %ANSI_START% %LEVEL%%ANSI_STOP%" warning
assert formatted "test" "25 %MSG% %ANSI_START% %LEVEL%%ANSI_STOP%" info
assert formatted "test" "25 %MSG% %ANSI_START% %LEVEL%%ANSI_STOP%" debug
assert formatted --short "test" "TEST %ANSI_START% %MSG%%ANSI_STOP%" critical
assert formatted --short "test" "TEST %ANSI_START% %MSG%%ANSI_STOP%" error
assert formatted --short "test" "TEST %ANSI_START% %MSG%%ANSI_STOP%" warning
assert formatted --short "test" "TEST %ANSI_START% %MSG%%ANSI_STOP%" info
assert formatted --short "test" "TEST %ANSI_START% %MSG%%ANSI_STOP%" debug
}

View file

@ -0,0 +1,37 @@
use std *
export def "test_env_log_ansi" [] {
assert equal $env.LOG_ANSI.CRITICAL (ansi red_bold)
assert equal $env.LOG_ANSI.ERROR (ansi red)
assert equal $env.LOG_ANSI.WARNING (ansi yellow)
assert equal $env.LOG_ANSI.INFO (ansi default)
assert equal $env.LOG_ANSI.DEBUG (ansi default_dimmed)
}
export def "test_env_log_level" [] {
assert equal $env.LOG_LEVEL.CRITICAL 50
assert equal $env.LOG_LEVEL.ERROR 40
assert equal $env.LOG_LEVEL.WARNING 30
assert equal $env.LOG_LEVEL.INFO 20
assert equal $env.LOG_LEVEL.DEBUG 10
}
export def "test_env_log_prefix" [] {
assert equal $env.LOG_PREFIX.CRITICAL "CRT"
assert equal $env.LOG_PREFIX.ERROR "ERR"
assert equal $env.LOG_PREFIX.WARNING "WRN"
assert equal $env.LOG_PREFIX.INFO "INF"
assert equal $env.LOG_PREFIX.DEBUG "DBG"
}
export def "test_env_log_short_prefix" [] {
assert equal $env.LOG_SHORT_PREFIX.CRITICAL "C"
assert equal $env.LOG_SHORT_PREFIX.ERROR "E"
assert equal $env.LOG_SHORT_PREFIX.WARNING "W"
assert equal $env.LOG_SHORT_PREFIX.INFO "I"
assert equal $env.LOG_SHORT_PREFIX.DEBUG "D"
}
export def "test_env_log_format" [] {
assert equal $env.LOG_FORMAT $"%ANSI_START%%DATE%|%LEVEL%|(ansi u)%MSG%%ANSI_STOP%"
}

View file

@ -1,164 +0,0 @@
use std *
def run [
system_level,
message_level
--short (-s)
] {
do {
if $short {
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) --short "test message"'
} else {
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) "test message"'
}
} | complete | get --ignore-errors stderr
}
def "assert no message" [
system_level,
message_level
] {
let output = (run $system_level $message_level)
assert equal "" $output
}
def "assert message" [
system_level,
message_level,
message_level_str
] {
let output = (run $system_level $message_level)
assert str contains $output $message_level_str
assert str contains $output "test message"
}
def "assert message short" [
system_level,
message_level,
message_level_str
] {
let output = (run --short $system_level $message_level)
assert str contains $output $message_level_str
assert str contains $output "test message"
}
export def test_critical [] {
assert no message 99 critical
assert message CRITICAL critical CRT
}
export def test_critical_short [] {
assert message short CRITICAL critical C
}
export def test_error [] {
assert no message CRITICAL error
assert message ERROR error ERR
}
export def test_error_short [] {
assert message short ERROR error E
}
export def test_warning [] {
assert no message ERROR warning
assert message WARNING warning WRN
}
export def test_warning_short [] {
assert message short WARNING warning W
}
export def test_info [] {
assert no message WARNING info
assert message INFO info "INF" # INF has to be quoted, otherwise it is the `inf` float
}
export def test_info_short [] {
assert message short INFO info I
}
export def test_debug [] {
assert no message INFO debug
assert message DEBUG debug DBG
}
export def test_debug_short [] {
assert message short DEBUG debug D
}
def "run custom" [
system_level,
format,
message_level
] {
do {
^$nu.current-exe --commands $'use std; NU_LOG_LEVEL=($system_level) std log custom "test message" "($format)" ($message_level)'
} | complete | get --ignore-errors stderr
}
def "assert custom message" [
system_level,
format,
message_level
] {
let output = (run custom $system_level $format $message_level)
assert equal ($output | str trim --right) ($format | str replace "%MSG%" "test message")
}
def "assert custom message contains" [
system_level,
format,
message_level,
tested_str
] {
let output = (run custom $system_level $format $message_level)
assert str contains $output $tested_str
assert str contains $output "test message"
}
def "assert custom message not contains" [
system_level,
format,
message_level,
tested_str
] {
let output = (run custom $system_level $format $message_level)
assert not ($output | str contains $tested_str)
assert str contains $output "test message"
}
def "assert no custom message" [
system_level,
format,
message_level
] {
let output = (run custom $system_level $format $message_level)
assert equal ($output | str trim --right) ""
}
export def test_custom [] {
assert no custom message (log ERROR_LEVEL) "%MSG%" (log DEBUG_LEVEL)
assert custom message (log DEBUG_LEVEL) "%MSG%" (log INFO_LEVEL)
assert custom message (log WARNING_LEVEL) $"my_msg: %MSG%" (log CRITICAL_LEVEL)
assert custom message contains (log DEBUG_LEVEL) $"(ansi yellow)[%LEVEL%]MY MESSAGE: %MSG% [%DATE%](ansi reset)" (log WARNING_LEVEL) (log WARNING_LEVEL_PREFIX)
assert custom message not contains (log DEBUG_LEVEL) $"(ansi yellow)MY MESSAGE: %MSG% [%DATE%](ansi reset)" (log WARNING_LEVEL) (log WARNING_LEVEL_PREFIX)
}
export def "test_long_prefixes" [] {
assert equal (log CRITICAL_LEVEL_PREFIX) "CRT"
assert equal (log ERROR_LEVEL_PREFIX) "ERR"
assert equal (log WARNING_LEVEL_PREFIX) "WRN"
assert equal (log INFO_LEVEL_PREFIX) "INF"
assert equal (log DEBUG_LEVEL_PREFIX) "DBG"
}
export def "test_short_prefixes" [] {
assert equal (log CRITICAL_LEVEL_PREFIX --short) "C"
assert equal (log ERROR_LEVEL_PREFIX --short) "E"
assert equal (log WARNING_LEVEL_PREFIX --short) "W"
assert equal (log INFO_LEVEL_PREFIX --short) "I"
assert equal (log DEBUG_LEVEL_PREFIX --short) "D"
}