HackBrowserData/internal/log/log.go

120 lines
2.8 KiB
Go
Raw Normal View History

2020-06-19 12:43:31 +00:00
package log
import (
"os"
2022-04-15 19:57:19 +00:00
"github.com/gookit/color"
"github.com/gookit/slog"
)
2020-06-19 12:43:31 +00:00
2022-04-15 19:57:19 +00:00
var std = &slog.SugaredLogger{}
func Init(l string) {
if l == "debug" {
std = newStdLogger(slog.DebugLevel)
} else {
std = newStdLogger(slog.ErrorLevel)
2020-06-19 12:43:31 +00:00
}
}
2022-04-15 19:57:19 +00:00
const template = "[{{datetime}}] [{{level}}] [{{caller}}] {{message}} {{data}} {{extra}}\n"
// NewStdLogger instance
func newStdLogger(level slog.Level) *slog.SugaredLogger {
return slog.NewSugaredLogger(os.Stdout, level).Configure(func(sl *slog.SugaredLogger) {
sl.SetName("stdLogger")
sl.ReportCaller = true
sl.CallerSkip = 3
// auto enable console color
sl.Formatter.(*slog.TextFormatter).EnableColor = color.SupportColor()
sl.Formatter.(*slog.TextFormatter).SetTemplate(template)
})
}
2020-06-19 12:43:31 +00:00
2022-04-15 19:57:19 +00:00
// Trace logs a message at level Trace
func Trace(args ...interface{}) {
std.Log(slog.TraceLevel, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Tracef logs a message at level Trace
func Tracef(format string, args ...interface{}) {
std.Logf(slog.TraceLevel, format, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Info logs a message at level Info
func Info(args ...interface{}) {
std.Log(slog.InfoLevel, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Infof logs a message at level Info
func Infof(format string, args ...interface{}) {
std.Logf(slog.InfoLevel, format, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Notice logs a message at level Notice
func Notice(args ...interface{}) {
std.Log(slog.NoticeLevel, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Noticef logs a message at level Notice
func Noticef(format string, args ...interface{}) {
std.Logf(slog.NoticeLevel, format, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Warn logs a message at level Warn
func Warn(args ...interface{}) {
std.Log(slog.WarnLevel, args...)
}
// Warnf logs a message at level Warn
func Warnf(format string, args ...interface{}) {
std.Logf(slog.WarnLevel, format, args...)
}
// Error logs a message at level Error
func Error(args ...interface{}) {
std.Log(slog.ErrorLevel, args...)
}
// ErrorT logs a error type at level Error
func ErrorT(err error) {
if err != nil {
std.Log(slog.ErrorLevel, err)
}
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Errorf logs a message at level Error
func Errorf(format string, args ...interface{}) {
std.Logf(slog.ErrorLevel, format, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Debug logs a message at level Debug
func Debug(args ...interface{}) {
std.Log(slog.DebugLevel, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Debugf logs a message at level Debug
func Debugf(format string, args ...interface{}) {
std.Logf(slog.DebugLevel, format, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Fatal logs a message at level Fatal
func Fatal(args ...interface{}) {
std.Log(slog.FatalLevel, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Fatalf logs a message at level Fatal
func Fatalf(format string, args ...interface{}) {
std.Logf(slog.FatalLevel, format, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-15 19:57:19 +00:00
// Panic logs a message at level Panic
func Panic(args ...interface{}) {
std.Log(slog.PanicLevel, args...)
2020-06-19 12:43:31 +00:00
}
2022-04-12 05:48:03 +00:00
2022-04-15 19:57:19 +00:00
// Panicf logs a message at level Panic
func Panicf(format string, args ...interface{}) {
std.Logf(slog.PanicLevel, format, args...)
}