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
9 changed files with 445 additions and 330 deletions

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"
}