2023-05-19 22:27:45 +02:00
|
|
|
export def CRITICAL_LEVEL [] {
|
2023-05-08 10:13:21 +02:00
|
|
|
50
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:27:45 +02:00
|
|
|
export def ERROR_LEVEL [] {
|
2023-05-08 10:13:21 +02:00
|
|
|
40
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:27:45 +02:00
|
|
|
export def WARNING_LEVEL [] {
|
2023-05-08 10:13:21 +02:00
|
|
|
30
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:27:45 +02:00
|
|
|
export def INFO_LEVEL [] {
|
2023-05-08 10:13:21 +02:00
|
|
|
20
|
2023-05-04 18:30:15 +02:00
|
|
|
}
|
|
|
|
|
2023-05-19 22:27:45 +02:00
|
|
|
export def DEBUG_LEVEL [] {
|
2023-05-08 10:13:21 +02:00
|
|
|
10
|
|
|
|
}
|
|
|
|
|
|
|
|
def parse-string-level [
|
|
|
|
level: string
|
|
|
|
] {
|
2023-05-19 22:27:45 +02:00
|
|
|
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
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
INFO_LEVEL
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:27:45 +02:00
|
|
|
export def CRITICAL_LEVEL_PREFIX [
|
2023-05-08 10:13:21 +02:00
|
|
|
--short (-s)
|
|
|
|
] {
|
|
|
|
if $short {
|
|
|
|
"C"
|
|
|
|
} else {
|
|
|
|
"CRT"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:27:45 +02:00
|
|
|
export def ERROR_LEVEL_PREFIX [
|
2023-05-08 10:13:21 +02:00
|
|
|
--short (-s)
|
|
|
|
] {
|
|
|
|
if $short {
|
|
|
|
"E"
|
|
|
|
} else {
|
|
|
|
"ERR"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:27:45 +02:00
|
|
|
export def WARNING_LEVEL_PREFIX [
|
2023-05-08 10:13:21 +02:00
|
|
|
--short (-s)
|
|
|
|
] {
|
|
|
|
if $short {
|
|
|
|
"W"
|
|
|
|
} else {
|
|
|
|
"WRN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:27:45 +02:00
|
|
|
export def INFO_LEVEL_PREFIX [
|
2023-05-08 10:13:21 +02:00
|
|
|
--short (-s)
|
|
|
|
] {
|
|
|
|
if $short {
|
|
|
|
"I"
|
|
|
|
} else {
|
|
|
|
"INF"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 22:27:45 +02:00
|
|
|
export def DEBUG_LEVEL_PREFIX [
|
2023-05-08 10:13:21 +02:00
|
|
|
--short (-s)
|
|
|
|
] {
|
|
|
|
if $short {
|
|
|
|
"D"
|
|
|
|
} else {
|
|
|
|
"DBG"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def parse-int-level [
|
|
|
|
level: int,
|
|
|
|
--short (-s)
|
|
|
|
] {
|
2023-05-19 22:27:45 +02:00
|
|
|
if $level >= (CRITICAL_LEVEL) {
|
2023-05-08 10:13:21 +02:00
|
|
|
if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
CRITICAL_LEVEL_PREFIX --short
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
CRITICAL_LEVEL_PREFIX
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
2023-05-19 22:27:45 +02:00
|
|
|
} else if $level >= (ERROR_LEVEL) {
|
2023-05-08 10:13:21 +02:00
|
|
|
if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
ERROR_LEVEL_PREFIX --short
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
ERROR_LEVEL_PREFIX
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
2023-05-19 22:27:45 +02:00
|
|
|
} else if $level >= (WARNING_LEVEL) {
|
2023-05-08 10:13:21 +02:00
|
|
|
if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
WARNING_LEVEL_PREFIX --short
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
WARNING_LEVEL_PREFIX
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
2023-05-19 22:27:45 +02:00
|
|
|
} else if $level >= (INFO_LEVEL) {
|
2023-05-08 10:13:21 +02:00
|
|
|
if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
INFO_LEVEL_PREFIX --short
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
INFO_LEVEL_PREFIX
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
DEBUG_LEVEL_PREFIX --short
|
2023-05-04 18:30:15 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
DEBUG_LEVEL_PREFIX
|
2023-05-04 18:30:15 +02:00
|
|
|
}
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
2023-04-09 19:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def current-log-level [] {
|
2023-05-19 22:27:45 +02:00
|
|
|
let env_level = ($env.NU_LOG_LEVEL? | default (INFO_LEVEL))
|
2023-04-09 19:00:20 +02:00
|
|
|
|
|
|
|
try {
|
2023-05-08 10:13:21 +02:00
|
|
|
$env_level | into int
|
2023-04-09 19:00:20 +02:00
|
|
|
} catch {
|
|
|
|
parse-string-level $env_level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def now [] {
|
|
|
|
date now | date format "%Y-%m-%dT%H:%M:%S%.3f"
|
|
|
|
}
|
|
|
|
|
2023-05-08 10:13:21 +02:00
|
|
|
def log-formatted [
|
|
|
|
color: string,
|
|
|
|
prefix: string,
|
|
|
|
message: string
|
|
|
|
] {
|
2023-05-08 19:29:18 +02:00
|
|
|
print --stderr $"($color)(now)|($prefix)|(ansi u)($message)(ansi reset)"
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Log a critical message
|
2023-05-19 22:27:45 +02:00
|
|
|
export def critical [
|
2023-05-08 10:13:21 +02:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
|
|
|
] {
|
2023-05-19 22:27:45 +02:00
|
|
|
if (current-log-level) > (CRITICAL_LEVEL) {
|
2023-05-08 10:13:21 +02:00
|
|
|
return
|
|
|
|
}
|
2023-04-09 19:00:20 +02:00
|
|
|
|
2023-05-08 10:13:21 +02:00
|
|
|
let prefix = if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
CRITICAL_LEVEL_PREFIX --short
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
CRITICAL_LEVEL_PREFIX
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
|
|
|
log-formatted (ansi red_bold) $prefix $message
|
2023-04-09 19:00:20 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 10:13:21 +02:00
|
|
|
# Log an error message
|
2023-05-19 22:27:45 +02:00
|
|
|
export def error [
|
2023-05-08 10:13:21 +02:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
|
|
|
] {
|
2023-05-19 22:27:45 +02:00
|
|
|
if (current-log-level) > (ERROR_LEVEL) {
|
2023-05-08 10:13:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let prefix = if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
ERROR_LEVEL_PREFIX --short
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
ERROR_LEVEL_PREFIX
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
|
|
|
log-formatted (ansi red) $prefix $message
|
2023-04-09 19:00:20 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 10:13:21 +02:00
|
|
|
# Log a warning message
|
2023-05-19 22:27:45 +02:00
|
|
|
export def warning [
|
2023-05-08 10:13:21 +02:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
|
|
|
] {
|
2023-05-19 22:27:45 +02:00
|
|
|
if (current-log-level) > (WARNING_LEVEL) {
|
2023-05-08 10:13:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let prefix = if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
WARNING_LEVEL_PREFIX --short
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
WARNING_LEVEL_PREFIX
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
|
|
|
log-formatted (ansi yellow) $prefix $message
|
2023-04-09 19:00:20 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 10:13:21 +02:00
|
|
|
# Log an info message
|
2023-05-19 22:27:45 +02:00
|
|
|
export def info [
|
2023-05-08 10:13:21 +02:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
|
|
|
] {
|
2023-05-19 22:27:45 +02:00
|
|
|
if (current-log-level) > (INFO_LEVEL) {
|
2023-05-08 10:13:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let prefix = if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
INFO_LEVEL_PREFIX --short
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
INFO_LEVEL_PREFIX
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
|
|
|
log-formatted (ansi default) $prefix $message
|
2023-04-09 19:00:20 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 10:13:21 +02:00
|
|
|
# Log a debug message
|
2023-05-19 22:27:45 +02:00
|
|
|
export def debug [
|
2023-05-08 10:13:21 +02:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
|
|
|
] {
|
2023-05-19 22:27:45 +02:00
|
|
|
if (current-log-level) > (DEBUG_LEVEL) {
|
2023-05-08 10:13:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let prefix = if $short {
|
2023-05-19 22:27:45 +02:00
|
|
|
DEBUG_LEVEL_PREFIX --short
|
2023-05-08 10:13:21 +02:00
|
|
|
} else {
|
2023-05-19 22:27:45 +02:00
|
|
|
DEBUG_LEVEL_PREFIX
|
2023-05-08 10:13:21 +02:00
|
|
|
}
|
|
|
|
log-formatted (ansi default_dimmed) $prefix $message
|
2023-04-09 19:00:20 +02:00
|
|
|
}
|
2023-05-04 18:30:15 +02:00
|
|
|
|
2023-05-08 10:13:21 +02:00
|
|
|
# Log a message with a specific format and verbosity level
|
2023-05-04 18:30:15 +02:00
|
|
|
#
|
|
|
|
# Format reference:
|
2023-05-08 10:13:21 +02:00
|
|
|
# - %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"
|
2023-05-04 18:30:15 +02:00
|
|
|
#
|
|
|
|
# Examples:
|
2023-05-08 10:13:21 +02:00
|
|
|
# - std log custom "my message" $"(ansi yellow)[%LEVEL%]MY MESSAGE: %MSG% [%DATE%](ansi reset)" (std log WARNING_LEVEL)
|
2023-05-19 22:27:45 +02:00
|
|
|
export def custom [
|
2023-05-08 10:13:21 +02:00
|
|
|
message: string, # A message
|
|
|
|
format: string, # A format
|
|
|
|
log_level: int # A log level
|
|
|
|
--short (-s) # Whether to use a short prefix
|
|
|
|
] {
|
|
|
|
if (current-log-level) > ($log_level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let level = ((if $short {
|
|
|
|
parse-int-level $log_level --short
|
|
|
|
} else {
|
|
|
|
parse-int-level $log_level
|
|
|
|
}) | into string)
|
|
|
|
|
|
|
|
print --stderr ([
|
|
|
|
["%MSG%" $message]
|
|
|
|
["%DATE%" (now)]
|
|
|
|
["%LEVEL%" $level]
|
|
|
|
] | reduce --fold $format {
|
|
|
|
|it, acc| $acc | str replace --all $it.0 $it.1
|
|
|
|
})
|
|
|
|
}
|