log: add support for l.set_short_tag/1 (#20652)

This commit is contained in:
kbkpbot 2024-01-25 16:47:00 +08:00 committed by GitHub
parent a09bd7cff3
commit 4ceb550e88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 75 additions and 24 deletions

View File

@ -20,26 +20,48 @@ pub enum LogTarget {
}
// tag_to_cli returns the tag for log level `l` as a colored string.
fn tag_to_cli(l Level) string {
return match l {
.disabled { '' }
.fatal { term.red('FATAL') }
.error { term.red('ERROR') }
.warn { term.yellow('WARN ') }
.info { term.white('INFO ') }
.debug { term.magenta('DEBUG') }
fn tag_to_cli(l Level, short_tag bool) string {
if short_tag {
return match l {
.disabled { ' ' }
.fatal { term.red('F') }
.error { term.red('E') }
.warn { term.yellow('W') }
.info { term.white('I') }
.debug { term.magenta('D') }
}
} else {
return match l {
.disabled { '' }
.fatal { term.red('FATAL') }
.error { term.red('ERROR') }
.warn { term.yellow('WARN ') }
.info { term.white('INFO ') }
.debug { term.magenta('DEBUG') }
}
}
}
// tag_to_file returns the tag for log level `l` as a string.
fn tag_to_file(l Level) string {
return match l {
.disabled { ' ' }
.fatal { 'FATAL' }
.error { 'ERROR' }
.warn { 'WARN ' }
.info { 'INFO ' }
.debug { 'DEBUG' }
fn tag_to_file(l Level, short_tag bool) string {
if short_tag {
return match l {
.disabled { ' ' }
.fatal { 'F' }
.error { 'E' }
.warn { 'W' }
.info { 'I' }
.debug { 'D' }
}
} else {
return match l {
.disabled { ' ' }
.fatal { 'FATAL' }
.error { 'ERROR' }
.warn { 'WARN ' }
.info { 'INFO ' }
.debug { 'DEBUG' }
}
}
}
@ -47,12 +69,12 @@ fn tag_to_file(l Level) string {
// It returns `none` when it does not find a match.
pub fn level_from_tag(tag string) ?Level {
return match tag {
'DISABLED' { Level.disabled }
'FATAL' { Level.fatal }
'ERROR' { Level.error }
'WARN' { Level.warn }
'INFO' { Level.info }
'DEBUG' { Level.debug }
'DISABLED', ' ' { Level.disabled }
'FATAL', 'F' { Level.fatal }
'ERROR', 'E' { Level.error }
'WARN', 'W' { Level.warn }
'INFO', 'I' { Level.info }
'DEBUG', 'D' { Level.debug }
else { none }
}
}

View File

@ -13,6 +13,7 @@ fn test_default_log_instance() {
println('^^^ there should be `debug now` shown above')
log.set_level(log.level_from_tag('INFO') or { log.Level.disabled })
log.info('info again')
log.debug('no output for debug')
log.set_level(log.level_from_tag('') or { log.Level.disabled })
log.error('no output anymore')
println('^^^ there should be no `no output anymore` shown above')

View File

@ -33,6 +33,7 @@ mut:
output_target LogTarget // output to console (stdout/stderr) or file or both.
time_format TimeFormat
custom_time_format string = 'MMMM Do YY N kk:mm:ss A' // timestamp with custom format
short_tag bool
pub mut:
output_file_name string // log output to this file
}
@ -115,14 +116,14 @@ pub fn (mut l Log) reopen() ! {
// log_file writes log line `s` with `level` to the log file.
fn (mut l Log) log_file(s string, level Level) {
timestamp := l.time_format(time.now())
e := tag_to_file(level)
e := tag_to_file(level, l.short_tag)
l.ofile.writeln('${timestamp} [${e}] ${s}') or { panic(err) }
}
// log_cli writes log line `s` with `level` to stdout.
fn (l &Log) log_cli(s string, level Level) {
timestamp := l.time_format(time.now())
e := tag_to_cli(level)
e := tag_to_cli(level, l.short_tag)
println('${timestamp} [${e}] ${s}')
}
@ -260,3 +261,14 @@ pub fn (mut l Log) set_custom_time_format(f string) {
pub fn (l Log) get_custom_time_format() string {
return l.custom_time_format
}
// set_short_tag will set the log tag to it's short version
// eg. '[FATAL]'=>'[F]', '[ERROR]'=>'[E]','[WARN ]'=>'[W]','[INFO ]'=>'[I]','[DEBUG]'=>'[D]'
pub fn (mut l Log) set_short_tag(enabled bool) {
l.short_tag = enabled
}
// get_short_tag will get the log short tag enable state
pub fn (l Log) get_short_tag() bool {
return l.short_tag
}

View File

@ -75,12 +75,27 @@ fn test_logger_mutable_reference() {
println(@FN + ' end')
}
fn test_log_mutable_short_tag() {
println(@FN + ' start')
mut l := Log{}
l.set_level(.info)
l.set_short_tag(true)
log_mutable_statements(mut l)
assert true
println(@FN + ' end')
}
fn test_level_from_tag() ? {
assert level_from_tag('INFO')? == .info
assert level_from_tag('FATAL')? == .fatal
assert level_from_tag('WARN')? == .warn
assert level_from_tag('ERROR')? == .error
assert level_from_tag('DEBUG')? == .debug
assert level_from_tag('I')? == .info
assert level_from_tag('F')? == .fatal
assert level_from_tag('W')? == .warn
assert level_from_tag('E')? == .error
assert level_from_tag('D')? == .debug
invalid := ['', 'FOO', 'nope']

View File

@ -12,6 +12,7 @@
output_target: console
time_format: tf_ss_micro
custom_time_format: 'MMMM Do YY N kk:mm:ss A'
short_tag: false
output_file_name: ''
})
}