log: improve logging interface (#12886)

pull/12891/head
Sandro Martini 2021-12-18 11:38:43 +01:00 committed by GitHub
parent 927eecf7c0
commit 80995f3a2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 13 deletions

3
examples/.gitignore vendored
View File

@ -1,2 +1,3 @@
*.js
*.log
*.ppm *.ppm
*.js

View File

@ -11,8 +11,14 @@ fn main() {
l.info('info') l.info('info')
l.warn('warn') l.warn('warn')
l.error('error') l.error('error')
l.debug('no debug') l.debug('no output for debug')
l.set_level(.debug) l.set_level(.debug)
l.debug('debug') l.debug('debug now')
l.fatal('fatal') 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')
} }

View File

@ -9,22 +9,25 @@ import term
// Level defines possible log levels used by `Log` // Level defines possible log levels used by `Log`
pub enum Level { pub enum Level {
fatal = 1 disabled = 0
fatal
error error
warn warn
info info
debug debug
} }
// LogTarget defines possible log targets
pub enum LogTarget { pub enum LogTarget {
console console
file file
both 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 { fn tag_to_cli(l Level) string {
return match l { return match l {
.disabled { '' }
.fatal { term.red('FATAL') } .fatal { term.red('FATAL') }
.error { term.red('ERROR') } .error { term.red('ERROR') }
.warn { term.yellow('WARN ') } .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 { fn tag_to_file(l Level) string {
return match l { return match l {
.disabled { ' ' }
.fatal { 'FATAL' } .fatal { 'FATAL' }
.error { 'ERROR' } .error { 'ERROR' }
.warn { 'WARN ' } .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) fatal(s string)
error(s string) error(s string)
warn(s string) warn(s string)
@ -58,7 +76,7 @@ mut:
level Level level Level
output_label string output_label string
ofile os.File 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: pub mut:
output_file_name string // log output to this file 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. // 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) { pub fn (mut l Log) fatal(s string) {
if int(l.level) < int(Level.fatal) { if int(l.level) >= int(Level.fatal) {
return l.send_output(s, .fatal)
l.ofile.close()
} }
l.send_output(s, .fatal)
l.ofile.close()
panic('$l.output_label: $s') panic('$l.output_label: $s')
} }