You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
4.1 KiB
201 lines
4.1 KiB
package util
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/labstack/gommon/log"
|
|
"io"
|
|
"os"
|
|
sorbetLog "sorbet/pkg/log"
|
|
)
|
|
|
|
type EchoLogger struct {
|
|
l sorbetLog.Logger
|
|
}
|
|
|
|
func NewEchoLogger() *EchoLogger {
|
|
return NewCustomLogger(sorbetLog.Default())
|
|
}
|
|
|
|
func NewCustomLogger(l sorbetLog.Logger) *EchoLogger {
|
|
return &EchoLogger{l}
|
|
}
|
|
|
|
func (l *EchoLogger) Output() io.Writer {
|
|
return l.l.Writer()
|
|
}
|
|
|
|
func (l *EchoLogger) SetOutput(w io.Writer) {
|
|
l.l.SetWriter(w)
|
|
}
|
|
|
|
func (l *EchoLogger) Prefix() string {
|
|
return ""
|
|
}
|
|
|
|
func (l *EchoLogger) SetPrefix(_ string) {
|
|
// FIXME(hupeh): 能否使用 WithGroup 或者 Attr 实现?
|
|
fmt.Println("cannot set a prefix into logging")
|
|
}
|
|
|
|
func (l *EchoLogger) Level() log.Lvl {
|
|
switch v := l.l.Level(); v {
|
|
case sorbetLog.LevelDebug, sorbetLog.LevelTrace:
|
|
return log.DEBUG
|
|
case sorbetLog.LevelInfo:
|
|
return log.INFO
|
|
case sorbetLog.LevelWarn:
|
|
return log.WARN
|
|
case sorbetLog.LevelError:
|
|
return log.ERROR
|
|
case sorbetLog.LevelOff:
|
|
return log.OFF
|
|
case sorbetLog.LevelFatal:
|
|
return log.Lvl(7)
|
|
default:
|
|
if v < sorbetLog.LevelTrace {
|
|
return log.DEBUG
|
|
} else {
|
|
return log.Lvl(7)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (l *EchoLogger) SetLevel(v log.Lvl) {
|
|
switch v {
|
|
case log.DEBUG:
|
|
l.l.SetLevel(sorbetLog.LevelDebug)
|
|
case log.INFO:
|
|
l.l.SetLevel(sorbetLog.LevelInfo)
|
|
case log.WARN:
|
|
l.l.SetLevel(sorbetLog.LevelWarn)
|
|
case log.ERROR:
|
|
l.l.SetLevel(sorbetLog.LevelError)
|
|
case log.OFF:
|
|
l.l.SetLevel(sorbetLog.LevelOff)
|
|
}
|
|
}
|
|
|
|
func (l *EchoLogger) SetHeader(_ string) {
|
|
fmt.Println("cannot set a header into logging")
|
|
}
|
|
|
|
func (l *EchoLogger) Print(i ...interface{}) {
|
|
l.log(l.l.Level(), i...)
|
|
}
|
|
|
|
func (l *EchoLogger) Printf(format string, args ...interface{}) {
|
|
l.logf(l.l.Level(), format, args...)
|
|
}
|
|
|
|
func (l *EchoLogger) Printj(j log.JSON) {
|
|
l.logj(l.l.Level(), j)
|
|
}
|
|
|
|
func (l *EchoLogger) Debug(i ...interface{}) {
|
|
l.log(sorbetLog.LevelDebug, i...)
|
|
}
|
|
|
|
func (l *EchoLogger) Debugf(format string, args ...interface{}) {
|
|
l.logf(sorbetLog.LevelDebug, format, args...)
|
|
}
|
|
|
|
func (l *EchoLogger) Debugj(j log.JSON) {
|
|
l.logj(sorbetLog.LevelDebug, j)
|
|
}
|
|
|
|
func (l *EchoLogger) Info(i ...interface{}) {
|
|
l.log(sorbetLog.LevelInfo, i...)
|
|
}
|
|
|
|
func (l *EchoLogger) Infof(format string, args ...interface{}) {
|
|
l.logf(sorbetLog.LevelInfo, format, args...)
|
|
}
|
|
|
|
func (l *EchoLogger) Infoj(j log.JSON) {
|
|
l.logj(sorbetLog.LevelInfo, j)
|
|
}
|
|
|
|
func (l *EchoLogger) Warn(i ...interface{}) {
|
|
l.log(sorbetLog.LevelWarn, i...)
|
|
}
|
|
|
|
func (l *EchoLogger) Warnf(format string, args ...interface{}) {
|
|
l.logf(sorbetLog.LevelInfo, format, args...)
|
|
}
|
|
|
|
func (l *EchoLogger) Warnj(j log.JSON) {
|
|
l.logj(sorbetLog.LevelWarn, j)
|
|
}
|
|
|
|
func (l *EchoLogger) Error(i ...interface{}) {
|
|
l.log(sorbetLog.LevelError, i...)
|
|
}
|
|
|
|
func (l *EchoLogger) Errorf(format string, args ...interface{}) {
|
|
l.logf(sorbetLog.LevelError, format, args...)
|
|
}
|
|
|
|
func (l *EchoLogger) Errorj(j log.JSON) {
|
|
l.logj(sorbetLog.LevelError, j)
|
|
}
|
|
|
|
func (l *EchoLogger) Fatal(i ...interface{}) {
|
|
l.log(sorbetLog.LevelFatal, i...)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func (l *EchoLogger) Fatalj(j log.JSON) {
|
|
l.logj(sorbetLog.LevelFatal, j)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func (l *EchoLogger) Fatalf(format string, args ...interface{}) {
|
|
l.logf(sorbetLog.LevelFatal, format, args...)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func (l *EchoLogger) Panic(i ...interface{}) {
|
|
l.log(sorbetLog.LevelPanic, i...)
|
|
panic(fmt.Sprint(i...))
|
|
}
|
|
|
|
func (l *EchoLogger) Panicj(j log.JSON) {
|
|
l.logj(sorbetLog.LevelPanic, j)
|
|
panic(j)
|
|
}
|
|
|
|
func (l *EchoLogger) Panicf(format string, args ...interface{}) {
|
|
l.logf(sorbetLog.LevelPanic, format, args...)
|
|
panic(fmt.Sprintf(format, args...))
|
|
}
|
|
|
|
func (l *EchoLogger) log(level sorbetLog.Level, args ...any) {
|
|
var attrs []any
|
|
var formats []any
|
|
for _, arg := range args {
|
|
switch arg.(type) {
|
|
case sorbetLog.Attr:
|
|
attrs = append(attrs, attrs)
|
|
default:
|
|
formats = append(formats, args)
|
|
}
|
|
}
|
|
msg := ""
|
|
if len(formats) > 0 {
|
|
msg = fmt.Sprint(formats...)
|
|
}
|
|
l.l.Log(level, msg, attrs...)
|
|
}
|
|
|
|
func (l *EchoLogger) logf(level sorbetLog.Level, format string, args ...any) {
|
|
l.l.Log(level, format, args...)
|
|
}
|
|
|
|
func (l *EchoLogger) logj(level sorbetLog.Level, j log.JSON) {
|
|
b, err := json.Marshal(j)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
l.l.Log(level, string(b))
|
|
}
|
|
|