From ee52b4166fa26a0b8ab7837886eeebc0e8510fac Mon Sep 17 00:00:00 2001 From: _leif Date: Sun, 24 Nov 2019 22:50:59 -0600 Subject: [PATCH] log: enum for logger level --- examples/log.v | 18 +++--- vlib/log/log.v | 171 +++++++++++++++++++++++++++++-------------------- 2 files changed, 112 insertions(+), 77 deletions(-) diff --git a/examples/log.v b/examples/log.v index c10ea39492..466439319c 100644 --- a/examples/log.v +++ b/examples/log.v @@ -1,12 +1,12 @@ import log -fn main(){ - mut l := log.Log{log.INFO, 'terminal'} - l.info('info') - l.warn('warn') - l.error('error') - l.debug('no debug') - l.set_level(log.DEBUG) - l.debug('debug') - l.fatal('fatal') +fn main() { + mut l := log.Log { log.LogLevel.info, 'info', true } + l.info('info') + l.warn('warn') + l.error('error') + l.debug('no debug') + l.set_level(log.DEBUG) + l.debug('debug') + l.fatal('fatal') } diff --git a/vlib/log/log.v b/vlib/log/log.v index 494610b28f..5ab445398d 100644 --- a/vlib/log/log.v +++ b/vlib/log/log.v @@ -4,101 +4,136 @@ import os import time import term +pub enum LogLevel { + fatal + error + warning + info + debug +} + +fn tag(l LogLevel) string { + return match l { + .fatal { term.red('F') } + .error { term.red('E') } + .warning { term.yellow('W') } + .info { term.white('I') } + .debug { term.blue('D') } + else { ' ' } + } +} + pub const ( - FATAL = 1 - ERROR = 2 - WARN = 3 - INFO = 4 - DEBUG = 5 + FATAL = 1 + ERROR = 2 + WARN = 3 + INFO = 4 + DEBUG = 5 ) interface Logger { - fatal(s string) - error(s string) - warn(s string) - info(s string) - debug(s string) + fatal(s string) + error(s string) + warn(s string) + info(s string) + debug(s string) } pub struct Log { -mut: - level int - output string + mut: + level LogLevel + output_label string + output_to_file bool } pub fn (l mut Log) set_level(level int){ - l.level = level + l.level = match level { + FATAL { LogLevel.fatal } + ERROR { LogLevel.error } + WARN { LogLevel.warning } + INFO { LogLevel.info } + DEBUG { LogLevel.debug } + else { .debug } + } } -pub fn (l mut Log) set_output(output string) { - l.output = output +pub fn (l mut Log) set_output_level(level LogLevel){ + l.level = level } -fn (l Log) log_file(s string, e string) { - filename := l.output - f := os.open_append(l.output) or { - panic('error reading file $filename') - } - timestamp := time.now().format_ss() - f.writeln('$timestamp [$e] $s') +pub fn (l mut Log) set_output_label(label string) { + l.output_label = label +} + +pub fn (l mut Log) set_output(output string){ + l.output_label = output +} + +fn (l Log) log_file(s string, level LogLevel) { + filename := '${l.output_label}.log'.replace(' ', '') + f := os.open_append(filename) or { + panic('error reading file $filename') + } + timestamp := time.now().format_ss() + e := tag(level) + f.writeln('$timestamp [$e] $s') +} + +fn (l Log) log_cli(s string, level LogLevel) { + f := tag(level) + t := time.now() + println('[$f ${t.format_ss()}] $s') } pub fn (l Log) fatal(s string){ - panic(s) + if l.level == .fatal { + if l.output_to_file { + l.log_file(s, .fatal) + } else { + l.log_cli(s, .fatal) + } + panic('$l.output_label: $s') + } } pub fn (l Log) error(s string){ - if l.level >= ERROR{ - match l.output { - 'terminal'{ - f := term.red('E') - t := time.now() - println('[$f ${t.format_ss()}] $s') - } else { - l.log_file(s, 'E') - } - } - } + if l.level in [.info, .debug, .warning, .error] { + if l.output_to_file { + l.log_file(s, .error) + } else { + l.log_cli(s, .error) + } + + } } pub fn (l Log) warn(s string){ - if l.level >= WARN{ - match l.output { - 'terminal'{ - f := term.yellow('W') - t := time.now() - println('[$f ${t.format_ss()}] $s') - } else { - l.log_file(s, 'W') - } - } - } + if l.level in [.info, .debug, .warning] { + if l.output_to_file { + l.log_file(s, .warning) + } else { + l.log_cli(s, .warning) + } + } } pub fn (l Log) info(s string){ - if l.level >= INFO{ - match l.output { - 'terminal'{ - f := term.white('I') - t := time.now() - println('[$f ${t.format_ss()}] $s') - } else { - l.log_file(s, 'I') - } - } - } + if l.level in [.info, .debug] { + if l.output_to_file { + l.log_file(s, .info) + } else { + l.log_cli(s, .info) + } + } } pub fn (l Log) debug(s string){ - if l.level >= DEBUG{ - match l.output { - 'terminal' { - f := term.blue('D') - t := time.now() - println('[$f ${t.format_ss()}] $s') - } else { - l.log_file(s, 'D') - } - } - } + if l.level != .debug { + return + } + if l.output_to_file { + l.log_file(s, .debug) + } else { + l.log_cli(s, .debug) + } }