log: fix log_to_file (#7955)

pull/7960/head
yuyi 2021-01-08 18:26:37 +08:00 committed by GitHub
parent b299fb1e92
commit a481c1785b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 3 deletions

View File

@ -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')
}