log: extended logging level names when printed

pull/3882/head
Major Taylor 2020-02-29 08:31:59 -05:00 committed by GitHub
parent f9d5c0110f
commit d4b0de2dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 24 deletions

View File

@ -1,9 +1,11 @@
module log module log
import os import (
import time os
import term time
import filepath term
filepath
)
pub enum LogLevel { pub enum LogLevel {
fatal = 1 fatal = 1
@ -15,12 +17,12 @@ pub enum LogLevel {
fn tag(l LogLevel) string { fn tag(l LogLevel) string {
return match l { return match l {
.fatal { term.red('F') } .fatal { term.red('FATAL') }
.error { term.red('E') } .error { term.red('ERROR') }
.warn { term.yellow('W') } .warn { term.yellow('WARN ') }
.info { term.white('I') } .info { term.white('INFO ') }
.debug { term.blue('D') } .debug { term.blue('DEBUG') }
else { ' ' } else { ' ' }
} }
} }
@ -42,15 +44,15 @@ interface Logger {
pub struct Log { pub struct Log {
mut: mut:
level LogLevel level LogLevel
output_label string output_label string
ofile os.File ofile os.File
output_to_file bool output_to_file bool
pub mut: pub mut:
output_file_name string output_file_name string
} }
pub fn (l mut Log) set_level(level int){ pub fn (l mut Log) set_level(level int) {
l.level = match level { l.level = match level {
FATAL { LogLevel.fatal } FATAL { LogLevel.fatal }
ERROR { LogLevel.error } ERROR { LogLevel.error }
@ -61,7 +63,7 @@ pub fn (l mut Log) set_level(level int){
} }
} }
pub fn (l mut Log) set_output_level(level LogLevel){ pub fn (l mut Log) set_output_level(level LogLevel) {
l.level = level l.level = level
} }
@ -79,13 +81,13 @@ pub fn (l mut Log) set_output_path(output_file_path string) {
if l.ofile.is_opened() { l.ofile.close() } if l.ofile.is_opened() { l.ofile.close() }
l.output_to_file = true l.output_to_file = true
l.output_file_name = filepath.join( os.realpath( output_file_path ) , l.output_label ) l.output_file_name = filepath.join( os.realpath( output_file_path ) , l.output_label )
ofile := os.open_append( l.output_file_name ) or { ofile := os.open_append( l.output_file_name ) or {
panic('error while opening log file ${l.output_file_name} for appending') panic('error while opening log file ${l.output_file_name} for appending')
} }
l.ofile = ofile l.ofile = ofile
} }
pub fn (l mut Log) close(){ pub fn (l mut Log) close() {
l.ofile.close() l.ofile.close()
} }
@ -101,7 +103,7 @@ fn (l &Log) log_cli(s string, level LogLevel) {
println('[$f ${t.format_ss()}] $s') println('[$f ${t.format_ss()}] $s')
} }
fn (l mut Log) send_output(s &string, level LogLevel){ fn (l mut Log) send_output(s &string, level LogLevel) {
if l.output_to_file { if l.output_to_file {
l.log_file(s, level) l.log_file(s, level)
} else { } else {
@ -116,23 +118,23 @@ pub fn (l mut Log) fatal(s string){
panic('$l.output_label: $s') panic('$l.output_label: $s')
} }
pub fn (l mut Log) error(s string){ pub fn (l mut Log) error(s string) {
if l.level < .error { return } if l.level < .error { return }
l.send_output(s, .error) l.send_output(s, .error)
} }
pub fn (l mut Log) warn(s string){ pub fn (l mut Log) warn(s string) {
if l.level < .warn { return } if l.level < .warn { return }
l.send_output(s, .warn) l.send_output(s, .warn)
} }
pub fn (l mut Log) info(s string){ pub fn (l mut Log) info(s string) {
if l.level < .info { return } if l.level < .info { return }
l.send_output(s, .info) l.send_output(s, .info)
} }
pub fn (l mut Log) debug(s string){ pub fn (l mut Log) debug(s string) {
if l.level < .debug { return } if l.level < .debug { return }
l.send_output(s, .debug) l.send_output(s, .debug)
} }