From a481c1785b5fff8f5acfdd1eaf7de0c2dfb61334 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 8 Jan 2021 18:26:37 +0800 Subject: [PATCH] log: fix log_to_file (#7955) --- vlib/log/log.v | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/vlib/log/log.v b/vlib/log/log.v index cd443f6faa..67ca7bdd03 100644 --- a/vlib/log/log.v +++ b/vlib/log/log.v @@ -17,7 +17,7 @@ pub enum Level { } // tag returns the tag for log level `l` as a string. -fn tag(l Level) string { +fn tag_to_cli(l Level) string { return match l { .fatal { term.red('FATAL') } .error { term.red('ERROR') } @@ -27,6 +27,17 @@ fn tag(l Level) string { } } +// tag returns the tag for log level `l` as a string. +fn tag_to_file(l Level) string { + return match l { + .fatal { 'FATAL' } + .error { 'ERROR' } + .warn { 'WARN ' } + .info { 'INFO ' } + .debug { 'DEBUG' } + } +} + interface Logger { fatal(s string) error(s string) @@ -94,13 +105,13 @@ pub fn (mut l Log) close() { // log_file writes log line `s` with `level` to the log file. fn (mut l Log) log_file(s string, level Level) { timestamp := time.now().format_ss() - e := tag(level) + e := tag_to_file(level) l.ofile.writeln('$timestamp [$e] $s') } // log_cli writes log line `s` with `level` to stdout. fn (l &Log) log_cli(s string, level Level) { - f := tag(level) + f := tag_to_cli(level) t := time.now() println('[$f $t.format_ss()] $s') }