log: enum for logger level

pull/2882/head
_leif 2019-11-24 22:50:59 -06:00 committed by Alexander Medvednikov
parent 9bfea5e60b
commit ee52b4166f
2 changed files with 112 additions and 77 deletions

View File

@ -1,12 +1,12 @@
import log import log
fn main(){ fn main() {
mut l := log.Log{log.INFO, 'terminal'} mut l := log.Log { log.LogLevel.info, 'info', true }
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 debug')
l.set_level(log.DEBUG) l.set_level(log.DEBUG)
l.debug('debug') l.debug('debug')
l.fatal('fatal') l.fatal('fatal')
} }

View File

@ -4,101 +4,136 @@ import os
import time import time
import term 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 ( pub const (
FATAL = 1 FATAL = 1
ERROR = 2 ERROR = 2
WARN = 3 WARN = 3
INFO = 4 INFO = 4
DEBUG = 5 DEBUG = 5
) )
interface Logger { interface Logger {
fatal(s string) fatal(s string)
error(s string) error(s string)
warn(s string) warn(s string)
info(s string) info(s string)
debug(s string) debug(s string)
} }
pub struct Log { pub struct Log {
mut: mut:
level int level LogLevel
output string output_label string
output_to_file bool
} }
pub fn (l mut Log) set_level(level int){ 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) { pub fn (l mut Log) set_output_level(level LogLevel){
l.output = output l.level = level
} }
fn (l Log) log_file(s string, e string) { pub fn (l mut Log) set_output_label(label string) {
filename := l.output l.output_label = label
f := os.open_append(l.output) or { }
panic('error reading file $filename')
} pub fn (l mut Log) set_output(output string){
timestamp := time.now().format_ss() l.output_label = output
f.writeln('$timestamp [$e] $s') }
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){ 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){ pub fn (l Log) error(s string){
if l.level >= ERROR{ if l.level in [.info, .debug, .warning, .error] {
match l.output { if l.output_to_file {
'terminal'{ l.log_file(s, .error)
f := term.red('E') } else {
t := time.now() l.log_cli(s, .error)
println('[$f ${t.format_ss()}] $s') }
} else {
l.log_file(s, 'E') }
}
}
}
} }
pub fn (l Log) warn(s string){ pub fn (l Log) warn(s string){
if l.level >= WARN{ if l.level in [.info, .debug, .warning] {
match l.output { if l.output_to_file {
'terminal'{ l.log_file(s, .warning)
f := term.yellow('W') } else {
t := time.now() l.log_cli(s, .warning)
println('[$f ${t.format_ss()}] $s') }
} else { }
l.log_file(s, 'W')
}
}
}
} }
pub fn (l Log) info(s string){ pub fn (l Log) info(s string){
if l.level >= INFO{ if l.level in [.info, .debug] {
match l.output { if l.output_to_file {
'terminal'{ l.log_file(s, .info)
f := term.white('I') } else {
t := time.now() l.log_cli(s, .info)
println('[$f ${t.format_ss()}] $s') }
} else { }
l.log_file(s, 'I')
}
}
}
} }
pub fn (l Log) debug(s string){ pub fn (l Log) debug(s string){
if l.level >= DEBUG{ if l.level != .debug {
match l.output { return
'terminal' { }
f := term.blue('D') if l.output_to_file {
t := time.now() l.log_file(s, .debug)
println('[$f ${t.format_ss()}] $s') } else {
} else { l.log_cli(s, .debug)
l.log_file(s, 'D') }
}
}
}
} }