diff options
author | Keuin <[email protected]> | 2022-09-10 16:05:10 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-10 16:05:10 +0800 |
commit | e999937d75d8e8c40b06add376ebac423b0c2079 (patch) | |
tree | 8a30abaa5fc8fc68d283e0be52a73ee83bdaf3a2 /logging | |
parent | f028bff042f471a68dff681af9c79ef96bc952e5 (diff) |
Fix task is not properly restarted when the live is closed and started again.
Use more friendly log format to replace golang's default `log.Logger`. (not completed)
Cleaner task status management.
Diffstat (limited to 'logging')
-rw-r--r-- | logging/logger.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/logging/logger.go b/logging/logger.go new file mode 100644 index 0000000..9724c2f --- /dev/null +++ b/logging/logger.go @@ -0,0 +1,77 @@ +package logging + +/* +golang's `log` package sucks, so we wrap it. +*/ + +import ( + "fmt" + "log" + "runtime" +) + +type Logger struct { + delegate *log.Logger + prefix string + debugHeader string + infoHeader string + warningHeader string + errorHeader string + fatalHeader string +} + +const ( + kDebug = "DEBUG" + kInfo = "INFO" + kWarning = "WARNING" + kError = "ERROR" + kFatal = "FATAL" +) + +func NewWrappedLogger(delegate *log.Logger, name string) Logger { + return Logger{ + delegate: delegate, + debugHeader: fmt.Sprintf("[%v][%v]", name, kDebug), + infoHeader: fmt.Sprintf("[%v][%v]", name, kInfo), + warningHeader: fmt.Sprintf("[%v][%v]", name, kWarning), + errorHeader: fmt.Sprintf("[%v][%v]", name, kError), + fatalHeader: fmt.Sprintf("[%v][%v]", name, kFatal), + } +} + +func getCallerInfo() string { + _, file, line, ok := runtime.Caller(2) + if !ok { + file = "???" + line = 0 + } + short := file + for i := len(file) - 1; i > 0; i-- { + if file[i] == '/' { + short = file[i+1:] + break + } + } + file = short + return fmt.Sprintf("[%v:%v]", file, line) +} + +func (l Logger) Debug(format string, v ...any) { + l.delegate.Printf(l.debugHeader+getCallerInfo()+" "+format, v...) +} + +func (l Logger) Info(format string, v ...any) { + l.delegate.Printf(l.infoHeader+getCallerInfo()+" "+format, v...) +} + +func (l Logger) Warning(format string, v ...any) { + l.delegate.Printf(l.warningHeader+getCallerInfo()+" "+format, v...) +} + +func (l Logger) Error(format string, v ...any) { + l.delegate.Printf(l.errorHeader+getCallerInfo()+" "+format, v...) +} + +func (l Logger) Fatal(format string, v ...any) { + l.delegate.Printf(l.fatalHeader+getCallerInfo()+" "+format, v...) +} |