From 80995f3a2d62247f89d661429860ec05580ed0c2 Mon Sep 17 00:00:00 2001 From: Sandro Martini Date: Sat, 18 Dec 2021 11:38:43 +0100 Subject: [PATCH] log: improve logging interface (#12886) --- examples/.gitignore | 3 ++- examples/log.v | 12 +++++++++--- vlib/log/log.v | 36 +++++++++++++++++++++++++++--------- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/examples/.gitignore b/examples/.gitignore index 8299fd79f..1f275d822 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -1,2 +1,3 @@ +*.js +*.log *.ppm -*.js \ No newline at end of file diff --git a/examples/log.v b/examples/log.v index a4c28e1c5..e29ec8b0f 100644 --- a/examples/log.v +++ b/examples/log.v @@ -11,8 +11,14 @@ fn main() { l.info('info') l.warn('warn') l.error('error') - l.debug('no debug') + l.debug('no output for debug') l.set_level(.debug) - l.debug('debug') - l.fatal('fatal') + l.debug('debug now') + l.set_level(log.level_from_tag('INFO') or { log.Level.disabled }) // set level from string, sample + l.info('info again') + l.set_level(log.level_from_tag('') or { log.Level.disabled }) // set level from string, sample + l.error('no output anymore') + l.fatal('fatal') // panic, next statements won't be executed + l.set_level(.info) + l.warn('warn') } diff --git a/vlib/log/log.v b/vlib/log/log.v index 6440bbb4a..e5d510291 100644 --- a/vlib/log/log.v +++ b/vlib/log/log.v @@ -9,22 +9,25 @@ import term // Level defines possible log levels used by `Log` pub enum Level { - fatal = 1 + disabled = 0 + fatal error warn info debug } +// LogTarget defines possible log targets pub enum LogTarget { console file both } -// tag returns the tag for log level `l` as a string. +// 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 ') } @@ -33,9 +36,10 @@ fn tag_to_cli(l Level) string { } } -// tag 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 { return match l { + .disabled { ' ' } .fatal { 'FATAL' } .error { 'ERROR' } .warn { 'WARN ' } @@ -44,7 +48,21 @@ fn tag_to_file(l Level) string { } } -interface Logger { +// level_from_tag returns the log level from the given string if matches. +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 } + else { none } + } +} + +// Logger is an interface that describes a generic Logger +pub interface Logger { fatal(s string) error(s string) warn(s string) @@ -58,7 +76,7 @@ mut: level Level output_label string ofile os.File - output_target LogTarget // if true output to file else use stdout/stderr. + output_target LogTarget // output to console (stdout/stderr) or file or both. pub mut: output_file_name string // log output to this file } @@ -148,12 +166,12 @@ pub fn (mut l Log) send_output(s &string, level Level) { } // fatal logs line `s` via `send_output` if `Log.level` is greater than or equal to the `Level.fatal` category. +// Note that this method performs a panic at the end, even if log level is not enabled. pub fn (mut l Log) fatal(s string) { - if int(l.level) < int(Level.fatal) { - return + if int(l.level) >= int(Level.fatal) { + l.send_output(s, .fatal) + l.ofile.close() } - l.send_output(s, .fatal) - l.ofile.close() panic('$l.output_label: $s') } -- 2.30.2