mirror of
https://github.com/vlang/v.git
synced 2025-08-04 02:07:28 -04:00
log: add support for l.set_short_tag/1 (#20652)
This commit is contained in:
parent
a09bd7cff3
commit
4ceb550e88
@ -20,26 +20,48 @@ pub enum LogTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// tag_to_cli returns the tag for log level `l` as a colored string.
|
// tag_to_cli returns the tag for log level `l` as a colored string.
|
||||||
fn tag_to_cli(l Level) string {
|
fn tag_to_cli(l Level, short_tag bool) string {
|
||||||
return match l {
|
if short_tag {
|
||||||
.disabled { '' }
|
return match l {
|
||||||
.fatal { term.red('FATAL') }
|
.disabled { ' ' }
|
||||||
.error { term.red('ERROR') }
|
.fatal { term.red('F') }
|
||||||
.warn { term.yellow('WARN ') }
|
.error { term.red('E') }
|
||||||
.info { term.white('INFO ') }
|
.warn { term.yellow('W') }
|
||||||
.debug { term.magenta('DEBUG') }
|
.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.
|
// tag_to_file returns the tag for log level `l` as a string.
|
||||||
fn tag_to_file(l Level) string {
|
fn tag_to_file(l Level, short_tag bool) string {
|
||||||
return match l {
|
if short_tag {
|
||||||
.disabled { ' ' }
|
return match l {
|
||||||
.fatal { 'FATAL' }
|
.disabled { ' ' }
|
||||||
.error { 'ERROR' }
|
.fatal { 'F' }
|
||||||
.warn { 'WARN ' }
|
.error { 'E' }
|
||||||
.info { 'INFO ' }
|
.warn { 'W' }
|
||||||
.debug { 'DEBUG' }
|
.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.
|
// It returns `none` when it does not find a match.
|
||||||
pub fn level_from_tag(tag string) ?Level {
|
pub fn level_from_tag(tag string) ?Level {
|
||||||
return match tag {
|
return match tag {
|
||||||
'DISABLED' { Level.disabled }
|
'DISABLED', ' ' { Level.disabled }
|
||||||
'FATAL' { Level.fatal }
|
'FATAL', 'F' { Level.fatal }
|
||||||
'ERROR' { Level.error }
|
'ERROR', 'E' { Level.error }
|
||||||
'WARN' { Level.warn }
|
'WARN', 'W' { Level.warn }
|
||||||
'INFO' { Level.info }
|
'INFO', 'I' { Level.info }
|
||||||
'DEBUG' { Level.debug }
|
'DEBUG', 'D' { Level.debug }
|
||||||
else { none }
|
else { none }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ fn test_default_log_instance() {
|
|||||||
println('^^^ there should be `debug now` shown above')
|
println('^^^ there should be `debug now` shown above')
|
||||||
log.set_level(log.level_from_tag('INFO') or { log.Level.disabled })
|
log.set_level(log.level_from_tag('INFO') or { log.Level.disabled })
|
||||||
log.info('info again')
|
log.info('info again')
|
||||||
|
log.debug('no output for debug')
|
||||||
log.set_level(log.level_from_tag('') or { log.Level.disabled })
|
log.set_level(log.level_from_tag('') or { log.Level.disabled })
|
||||||
log.error('no output anymore')
|
log.error('no output anymore')
|
||||||
println('^^^ there should be no `no output anymore` shown above')
|
println('^^^ there should be no `no output anymore` shown above')
|
||||||
|
@ -33,6 +33,7 @@ mut:
|
|||||||
output_target LogTarget // output to console (stdout/stderr) or file or both.
|
output_target LogTarget // output to console (stdout/stderr) or file or both.
|
||||||
time_format TimeFormat
|
time_format TimeFormat
|
||||||
custom_time_format string = 'MMMM Do YY N kk:mm:ss A' // timestamp with custom format
|
custom_time_format string = 'MMMM Do YY N kk:mm:ss A' // timestamp with custom format
|
||||||
|
short_tag bool
|
||||||
pub mut:
|
pub mut:
|
||||||
output_file_name string // log output to this file
|
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.
|
// log_file writes log line `s` with `level` to the log file.
|
||||||
fn (mut l Log) log_file(s string, level Level) {
|
fn (mut l Log) log_file(s string, level Level) {
|
||||||
timestamp := l.time_format(time.now())
|
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) }
|
l.ofile.writeln('${timestamp} [${e}] ${s}') or { panic(err) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// log_cli writes log line `s` with `level` to stdout.
|
// log_cli writes log line `s` with `level` to stdout.
|
||||||
fn (l &Log) log_cli(s string, level Level) {
|
fn (l &Log) log_cli(s string, level Level) {
|
||||||
timestamp := l.time_format(time.now())
|
timestamp := l.time_format(time.now())
|
||||||
e := tag_to_cli(level)
|
e := tag_to_cli(level, l.short_tag)
|
||||||
println('${timestamp} [${e}] ${s}')
|
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 {
|
pub fn (l Log) get_custom_time_format() string {
|
||||||
return l.custom_time_format
|
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
|
||||||
|
}
|
||||||
|
@ -75,12 +75,27 @@ fn test_logger_mutable_reference() {
|
|||||||
println(@FN + ' end')
|
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() ? {
|
fn test_level_from_tag() ? {
|
||||||
assert level_from_tag('INFO')? == .info
|
assert level_from_tag('INFO')? == .info
|
||||||
assert level_from_tag('FATAL')? == .fatal
|
assert level_from_tag('FATAL')? == .fatal
|
||||||
assert level_from_tag('WARN')? == .warn
|
assert level_from_tag('WARN')? == .warn
|
||||||
assert level_from_tag('ERROR')? == .error
|
assert level_from_tag('ERROR')? == .error
|
||||||
assert level_from_tag('DEBUG')? == .debug
|
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']
|
invalid := ['', 'FOO', 'nope']
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
output_target: console
|
output_target: console
|
||||||
time_format: tf_ss_micro
|
time_format: tf_ss_micro
|
||||||
custom_time_format: 'MMMM Do YY N kk:mm:ss A'
|
custom_time_format: 'MMMM Do YY N kk:mm:ss A'
|
||||||
|
short_tag: false
|
||||||
output_file_name: ''
|
output_file_name: ''
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user