feat: refactor logger to standard library (#280)

* refactor: Refactor logging to use structured slog package.

- Remove `gookit` dependencies from `go.sum`
- Improve error logging in multiple packages by replacing `log` with `log/slog`
- Update dependencies in `go.mod`
- Add new `logger` package with test cases
- Refactor logging statements in multiple packages to use `slog` instead of `log`
- Change logging format and level in multiple packages for better structured logging

* refactor: Refactor logger package and add handler interface

- Refactor logger package
- Rename `defaultHandler` to `DefaultLogger`
- Move `ReplaceAttr` function to `Logger` struct
- Implement `LogHandler` struct with `slog.Handler` interface
- Add new `Logger` methods for configuration
- Add `SetMaxLevel`, `SetJSONHandler`, `SetTextHandler`, `SetOutput`, `SetVerbose`, `SetReplaceAttrFunc`
- Add verbose flag to `cmd/hack-browser-data/main.go` to increase logging

* refactor: Refactor logger package to use simplified handler initialization.

- Refactor logger package to use Default instead of DefaultLogger
- Update `NewHandler` method to correctly reference `Default` logger and simplify handler initialization
- Update tests for logger to reflect changes in Default usage
- Rename `DefaultLogger` to `Default` and update comments to better reflect its purpose
- Update function calls in hack-browser-data main.go to reflect logger package updates

* refactor: Refactor logging in Chromium implementation

Refactor logging and simplify decryption in chromium files
- Replace logger package import with shared slog package
- Change logging messages to use slog instead of logger
- Simplify decryption process by removing first 5 characters of encrypted key
- Refactor error logging in linux file to use shared slog package
- Replace string concatenation with formatted string in linux error message
This commit is contained in:
ᴍᴏᴏɴD4ʀᴋ
2024-01-16 14:06:13 +08:00
committed by GitHub
parent 23037e16fd
commit e48f35cfd3
23 changed files with 312 additions and 209 deletions
-119
View File
@@ -1,119 +0,0 @@
package log
import (
"os"
"github.com/gookit/slog"
)
var std = &slog.SugaredLogger{}
func init() {
std = newStdLogger(slog.NoticeLevel)
}
// SetVerbose set log level to debug
func SetVerbose() {
std = newStdLogger(slog.DebugLevel)
}
const template = "[{{level}}] [{{caller}}] {{message}} {{data}} {{extra}}\n"
// newStdLogger is a new std logger
func newStdLogger(level slog.Level) *slog.SugaredLogger {
return slog.NewSugaredLogger(os.Stdout, level).Config(func(sl *slog.SugaredLogger) {
sl.SetName("stdLogger")
sl.ReportCaller = true
sl.CallerSkip = 7
// auto enable console color
sl.Formatter.(*slog.TextFormatter).EnableColor = false
sl.Formatter.(*slog.TextFormatter).SetTemplate(template)
})
}
// Trace logs a message at level Trace
func Trace(args ...interface{}) {
std.Log(slog.TraceLevel, args...)
}
// Tracef logs a message at level Trace
func Tracef(format string, args ...interface{}) {
std.Logf(slog.TraceLevel, format, args...)
}
// Info logs a message at level Info
func Info(args ...interface{}) {
std.Log(slog.InfoLevel, args...)
}
// Infof logs a message at level Info
func Infof(format string, args ...interface{}) {
std.Logf(slog.InfoLevel, format, args...)
}
// Notice logs a message at level Notice
func Notice(args ...interface{}) {
std.Log(slog.NoticeLevel, args...)
}
// Noticef logs a message at level Notice
func Noticef(format string, args ...interface{}) {
std.Logf(slog.NoticeLevel, format, args...)
}
// 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)
}
}
// Errorf logs a message at level Error
func Errorf(format string, args ...interface{}) {
std.Logf(slog.ErrorLevel, format, args...)
}
// Debug logs a message at level Debug
func Debug(args ...interface{}) {
std.Log(slog.DebugLevel, args...)
}
// Debugf logs a message at level Debug
func Debugf(format string, args ...interface{}) {
std.Logf(slog.DebugLevel, format, args...)
}
// Fatal logs a message at level Fatal
func Fatal(args ...interface{}) {
std.Log(slog.FatalLevel, args...)
}
// Fatalf logs a message at level Fatal
func Fatalf(format string, args ...interface{}) {
std.Logf(slog.FatalLevel, format, args...)
}
// Panic logs a message at level Panic
func Panic(args ...interface{}) {
std.Log(slog.PanicLevel, args...)
}
// Panicf logs a message at level Panic
func Panicf(format string, args ...interface{}) {
std.Logf(slog.PanicLevel, format, args...)
}