2024-03-14 11:28:13 +00:00
|
|
|
export def log-ansi [] {
|
|
|
|
{
|
2023-06-04 08:43:40 +00:00
|
|
|
"CRITICAL": (ansi red_bold),
|
|
|
|
"ERROR": (ansi red),
|
|
|
|
"WARNING": (ansi yellow),
|
|
|
|
"INFO": (ansi default),
|
|
|
|
"DEBUG": (ansi default_dimmed)
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
2024-03-14 11:28:13 +00:00
|
|
|
}
|
2023-05-08 08:13:21 +00:00
|
|
|
|
2024-03-14 11:28:13 +00:00
|
|
|
export def log-level [] {
|
|
|
|
{
|
2023-06-04 08:43:40 +00:00
|
|
|
"CRITICAL": 50,
|
|
|
|
"ERROR": 40,
|
|
|
|
"WARNING": 30,
|
|
|
|
"INFO": 20,
|
|
|
|
"DEBUG": 10
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
2024-03-14 11:28:13 +00:00
|
|
|
}
|
|
|
|
export def log-prefix [] {
|
|
|
|
{
|
2023-06-04 08:43:40 +00:00
|
|
|
"CRITICAL": "CRT",
|
|
|
|
"ERROR": "ERR",
|
|
|
|
"WARNING": "WRN",
|
|
|
|
"INFO": "INF",
|
|
|
|
"DEBUG": "DBG"
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
2024-03-14 11:28:13 +00:00
|
|
|
}
|
|
|
|
export def log-short-prefix [] {
|
|
|
|
{
|
2023-06-04 08:43:40 +00:00
|
|
|
"CRITICAL": "C",
|
|
|
|
"ERROR": "E",
|
|
|
|
"WARNING": "W",
|
|
|
|
"INFO": "I",
|
|
|
|
"DEBUG": "D"
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
2024-03-14 11:28:13 +00:00
|
|
|
}
|
|
|
|
export-env {
|
2023-10-04 18:30:49 +00:00
|
|
|
$env.NU_LOG_FORMAT = $"%ANSI_START%%DATE%|%LEVEL%|%MSG%%ANSI_STOP%"
|
2023-10-04 16:53:46 +00:00
|
|
|
$env.NU_LOG_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S%.3f"
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 08:43:40 +00:00
|
|
|
def log-types [] {
|
|
|
|
(
|
|
|
|
{
|
|
|
|
"CRITICAL": {
|
2024-03-14 11:28:13 +00:00
|
|
|
"ansi": ((log-ansi).CRITICAL),
|
|
|
|
"level": ((log-level).CRITICAL),
|
|
|
|
"prefix": ((log-prefix).CRITICAL),
|
|
|
|
"short_prefix": ((log-short-prefix).CRITICAL)
|
2023-06-04 08:43:40 +00:00
|
|
|
},
|
|
|
|
"ERROR": {
|
2024-03-14 11:28:13 +00:00
|
|
|
"ansi": ((log-ansi).ERROR),
|
|
|
|
"level": ((log-level).ERROR),
|
|
|
|
"prefix": ((log-prefix).ERROR),
|
|
|
|
"short_prefix": ((log-short-prefix).ERROR)
|
2023-06-04 08:43:40 +00:00
|
|
|
},
|
|
|
|
"WARNING": {
|
2024-03-14 11:28:13 +00:00
|
|
|
"ansi": ((log-ansi).WARNING),
|
|
|
|
"level": ((log-level).WARNING),
|
|
|
|
"prefix": ((log-prefix).WARNING),
|
|
|
|
"short_prefix": ((log-short-prefix).WARNING)
|
2023-06-04 08:43:40 +00:00
|
|
|
},
|
|
|
|
"INFO": {
|
2024-03-14 11:28:13 +00:00
|
|
|
"ansi": ((log-ansi).INFO),
|
|
|
|
"level": ((log-level).INFO),
|
|
|
|
"prefix": ((log-prefix).INFO),
|
|
|
|
"short_prefix": ((log-short-prefix).INFO)
|
2023-06-04 08:43:40 +00:00
|
|
|
},
|
|
|
|
"DEBUG": {
|
2024-03-14 11:28:13 +00:00
|
|
|
"ansi": ((log-ansi).DEBUG),
|
|
|
|
"level": ((log-level).DEBUG),
|
|
|
|
"prefix": ((log-prefix).DEBUG),
|
|
|
|
"short_prefix": ((log-short-prefix).DEBUG)
|
2023-06-04 08:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 08:43:40 +00:00
|
|
|
|
|
|
|
def parse-string-level [
|
|
|
|
level: string
|
2023-05-08 08:13:21 +00:00
|
|
|
] {
|
2023-06-04 08:43:40 +00:00
|
|
|
let level = ($level | str upcase)
|
|
|
|
|
2024-03-14 11:28:13 +00:00
|
|
|
if $level in [((log-prefix).CRITICAL) ((log-short-prefix).CRITICAL) "CRIT" "CRITICAL"] {
|
|
|
|
((log-level).CRITICAL)
|
|
|
|
} else if $level in [((log-prefix).ERROR) ((log-short-prefix).ERROR) "ERROR"] {
|
|
|
|
((log-level).ERROR)
|
|
|
|
} else if $level in [((log-prefix).WARNING) ((log-short-prefix).WARNING) "WARN" "WARNING"] {
|
|
|
|
((log-level).WARNING)
|
|
|
|
} else if $level in [((log-prefix).DEBUG) ((log-short-prefix).DEBUG) "DEBUG"] {
|
|
|
|
((log-level).DEBUG)
|
2023-05-08 08:13:21 +00:00
|
|
|
} else {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-level).INFO)
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 08:43:40 +00:00
|
|
|
|
2023-05-08 08:13:21 +00:00
|
|
|
def parse-int-level [
|
|
|
|
level: int,
|
|
|
|
--short (-s)
|
|
|
|
] {
|
2024-03-14 11:28:13 +00:00
|
|
|
if $level >= ((log-level).CRITICAL) {
|
2023-05-08 08:13:21 +00:00
|
|
|
if $short {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-short-prefix).CRITICAL)
|
2023-05-08 08:13:21 +00:00
|
|
|
} else {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-prefix).CRITICAL)
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
2024-03-14 11:28:13 +00:00
|
|
|
} else if $level >= ((log-level).ERROR) {
|
2023-05-08 08:13:21 +00:00
|
|
|
if $short {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-short-prefix).ERROR)
|
2023-05-08 08:13:21 +00:00
|
|
|
} else {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-prefix).ERROR)
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
2024-03-14 11:28:13 +00:00
|
|
|
} else if $level >= ((log-level).WARNING) {
|
2023-05-08 08:13:21 +00:00
|
|
|
if $short {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-short-prefix).WARNING)
|
2023-05-08 08:13:21 +00:00
|
|
|
} else {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-prefix).WARNING)
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
2024-03-14 11:28:13 +00:00
|
|
|
} else if $level >= ((log-level).INFO) {
|
2023-05-08 08:13:21 +00:00
|
|
|
if $short {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-short-prefix).INFO)
|
2023-05-08 08:13:21 +00:00
|
|
|
} else {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-prefix).INFO)
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if $short {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-short-prefix).DEBUG)
|
2023-05-04 16:30:15 +00:00
|
|
|
} else {
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-prefix).DEBUG)
|
2023-05-04 16:30:15 +00:00
|
|
|
}
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
2023-04-09 17:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def current-log-level [] {
|
2024-03-14 11:28:13 +00:00
|
|
|
let env_level = ($env.NU_log-level? | default (((log-level).INFO)))
|
2023-04-09 17:00:20 +00:00
|
|
|
|
|
|
|
try {
|
2023-05-08 08:13:21 +00:00
|
|
|
$env_level | into int
|
2023-04-09 17:00:20 +00:00
|
|
|
} catch {
|
|
|
|
parse-string-level $env_level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def now [] {
|
2023-10-04 16:53:46 +00:00
|
|
|
date now | format date $env.NU_LOG_DATE_FORMAT
|
2023-04-09 17:00:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 08:43:40 +00:00
|
|
|
def handle-log [
|
|
|
|
message: string,
|
|
|
|
formatting: record,
|
|
|
|
format_string: string,
|
|
|
|
short: bool
|
2023-05-08 08:13:21 +00:00
|
|
|
] {
|
2023-06-04 08:43:40 +00:00
|
|
|
let log_format = if ($format_string | is-empty) {
|
2023-09-06 17:17:14 +00:00
|
|
|
$env.NU_LOG_FORMAT
|
2023-06-04 08:43:40 +00:00
|
|
|
} 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
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 08:43:40 +00:00
|
|
|
# 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 [] {}
|
|
|
|
|
2023-05-08 08:13:21 +00:00
|
|
|
# Log a critical message
|
2023-05-19 20:27:45 +00:00
|
|
|
export def critical [
|
2023-05-08 08:13:21 +00:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
2023-06-04 08:43:40 +00:00
|
|
|
--format (-f): string # A format (for further reference: help std log)
|
2023-05-08 08:13:21 +00:00
|
|
|
] {
|
2024-01-12 15:48:53 +00:00
|
|
|
let format = $format | default ""
|
2023-06-04 08:43:40 +00:00
|
|
|
handle-log $message (log-types | get CRITICAL) $format $short
|
2023-04-09 17:00:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 08:13:21 +00:00
|
|
|
# Log an error message
|
2023-05-19 20:27:45 +00:00
|
|
|
export def error [
|
2023-05-08 08:13:21 +00:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
2023-06-04 08:43:40 +00:00
|
|
|
--format (-f): string # A format (for further reference: help std log)
|
2023-05-08 08:13:21 +00:00
|
|
|
] {
|
2024-01-12 15:48:53 +00:00
|
|
|
let format = $format | default ""
|
2023-06-04 08:43:40 +00:00
|
|
|
handle-log $message (log-types | get ERROR) $format $short
|
2023-04-09 17:00:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 08:13:21 +00:00
|
|
|
# Log a warning message
|
2023-05-19 20:27:45 +00:00
|
|
|
export def warning [
|
2023-05-08 08:13:21 +00:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
2023-06-04 08:43:40 +00:00
|
|
|
--format (-f): string # A format (for further reference: help std log)
|
2023-05-08 08:13:21 +00:00
|
|
|
] {
|
2024-01-12 15:48:53 +00:00
|
|
|
let format = $format | default ""
|
2023-06-04 08:43:40 +00:00
|
|
|
handle-log $message (log-types | get WARNING) $format $short
|
2023-04-09 17:00:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 08:13:21 +00:00
|
|
|
# Log an info message
|
2023-05-19 20:27:45 +00:00
|
|
|
export def info [
|
2023-05-08 08:13:21 +00:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
2023-06-04 08:43:40 +00:00
|
|
|
--format (-f): string # A format (for further reference: help std log)
|
2023-05-08 08:13:21 +00:00
|
|
|
] {
|
2024-01-12 15:48:53 +00:00
|
|
|
let format = $format | default ""
|
2023-06-04 08:43:40 +00:00
|
|
|
handle-log $message (log-types | get INFO) $format $short
|
2023-04-09 17:00:20 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 08:13:21 +00:00
|
|
|
# Log a debug message
|
2023-05-19 20:27:45 +00:00
|
|
|
export def debug [
|
2023-05-08 08:13:21 +00:00
|
|
|
message: string, # A message
|
|
|
|
--short (-s) # Whether to use a short prefix
|
2023-06-04 08:43:40 +00:00
|
|
|
--format (-f): string # A format (for further reference: help std log)
|
2023-05-08 08:13:21 +00:00
|
|
|
] {
|
2024-01-12 15:48:53 +00:00
|
|
|
let format = $format | default ""
|
2023-06-04 08:43:40 +00:00
|
|
|
handle-log $message (log-types | get DEBUG) $format $short
|
|
|
|
}
|
2023-05-08 08:13:21 +00:00
|
|
|
|
2023-06-04 08:43:40 +00:00
|
|
|
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."
|
2024-03-14 11:28:13 +00:00
|
|
|
$" Available log levels in log-level:"
|
|
|
|
(log-level | to text | lines | each {|it| $" ($it)" } | to text)
|
2023-06-04 08:43:40 +00:00
|
|
|
] | str join "\n")
|
2023-11-03 15:09:33 +00:00
|
|
|
span: $span
|
2023-06-04 08:43:40 +00:00
|
|
|
}
|
2023-05-08 08:13:21 +00:00
|
|
|
}
|
2023-04-09 17:00:20 +00:00
|
|
|
}
|
2023-05-04 16:30:15 +00:00
|
|
|
|
2023-06-04 08:43:40 +00:00
|
|
|
# Log a message with a specific format and verbosity level, with either configurable or auto-deduced %LEVEL% and %ANSI_START% placeholder extensions
|
2023-05-19 20:27:45 +00:00
|
|
|
export def custom [
|
2023-05-08 08:13:21 +00:00
|
|
|
message: string, # A message
|
2023-06-04 08:43:40 +00:00
|
|
|
format: string, # A format (for further reference: help std log)
|
2024-03-14 11:28:13 +00:00
|
|
|
log_level: int # A log level (has to be one of the log-level values for correct ansi/prefix deduction)
|
2023-06-04 08:43:40 +00:00
|
|
|
--level-prefix (-p): string # %LEVEL% placeholder extension
|
|
|
|
--ansi (-a): string # %ANSI_START% placeholder extension
|
2023-05-08 08:13:21 +00:00
|
|
|
] {
|
|
|
|
if (current-log-level) > ($log_level) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-04 08:43:40 +00:00
|
|
|
let valid_levels_for_defaulting = [
|
2024-03-14 11:28:13 +00:00
|
|
|
((log-level).CRITICAL)
|
|
|
|
((log-level).ERROR)
|
|
|
|
((log-level).WARNING)
|
|
|
|
((log-level).INFO)
|
|
|
|
((log-level).DEBUG)
|
2023-06-04 08:43:40 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-08 08:13:21 +00:00
|
|
|
parse-int-level $log_level
|
2023-06-04 08:43:40 +00:00
|
|
|
|
|
|
|
} 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
|
|
|
|
}
|
2023-05-08 08:13:21 +00:00
|
|
|
|
|
|
|
print --stderr ([
|
|
|
|
["%MSG%" $message]
|
|
|
|
["%DATE%" (now)]
|
2023-06-04 08:43:40 +00:00
|
|
|
["%LEVEL%" $prefix]
|
|
|
|
["%ANSI_START%" $ansi]
|
|
|
|
["%ANSI_STOP%" (ansi reset)]
|
2023-05-08 08:13:21 +00:00
|
|
|
] | reduce --fold $format {
|
|
|
|
|it, acc| $acc | str replace --all $it.0 $it.1
|
|
|
|
})
|
|
|
|
}
|