mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-05-19 18:58:03 +02:00
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:
@@ -0,0 +1,63 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
var _ slog.Handler = (*LogHandler)(nil)
|
||||
|
||||
// LogHandler is a slog.Handler implementation that can be used to log to a file.
|
||||
type LogHandler struct {
|
||||
handler slog.Handler
|
||||
}
|
||||
|
||||
func NewHandler(logger *Logger) LogHandler {
|
||||
if logger == nil {
|
||||
logger = Default
|
||||
}
|
||||
|
||||
level := logger.Level
|
||||
if logger.IsVerbose {
|
||||
level = slog.LevelDebug
|
||||
}
|
||||
|
||||
output := logger.Output
|
||||
if output == nil {
|
||||
output = os.Stderr
|
||||
}
|
||||
|
||||
handlerOptions := &slog.HandlerOptions{
|
||||
AddSource: logger.AddSource,
|
||||
Level: level,
|
||||
ReplaceAttr: logger.ReplaceAttr,
|
||||
}
|
||||
|
||||
if logger.IsJSONHandler {
|
||||
return LogHandler{
|
||||
handler: slog.NewJSONHandler(output, handlerOptions),
|
||||
}
|
||||
}
|
||||
return LogHandler{
|
||||
handler: slog.NewTextHandler(output, handlerOptions),
|
||||
}
|
||||
}
|
||||
|
||||
var _ slog.Handler = (*LogHandler)(nil)
|
||||
|
||||
func (t LogHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
return t.handler.Handle(ctx, r)
|
||||
}
|
||||
|
||||
func (t LogHandler) Enabled(ctx context.Context, l slog.Level) bool {
|
||||
return t.handler.Enabled(ctx, l)
|
||||
}
|
||||
|
||||
func (t LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return LogHandler{handler: t.handler.WithAttrs(attrs)}
|
||||
}
|
||||
|
||||
func (t LogHandler) WithGroup(name string) slog.Handler {
|
||||
return LogHandler{handler: t.handler.WithGroup(name)}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Default is the default *Logger for the default handler.
|
||||
var Default = &Logger{
|
||||
AddSource: true,
|
||||
IsVerbose: false,
|
||||
IsJSONHandler: true,
|
||||
Level: slog.LevelWarn,
|
||||
ReplaceAttr: defaultReplaceAttrFunc,
|
||||
Output: os.Stderr,
|
||||
}
|
||||
|
||||
func init() {
|
||||
Configure(Default)
|
||||
}
|
||||
|
||||
// Configure configures the logger by the given options.
|
||||
func Configure(opts *Logger) {
|
||||
customHandler := NewHandler(opts)
|
||||
slog.SetDefault(slog.New(customHandler))
|
||||
}
|
||||
|
||||
type Logger struct {
|
||||
// AddSource indicates whether to add source code location to the log.
|
||||
AddSource bool
|
||||
|
||||
// IsVerbose indicates whether to enable verbose mode. If true, debug level will be enabled.
|
||||
// If false, only warn and error level will be enabled.
|
||||
IsVerbose bool
|
||||
|
||||
// Level indicates the log level of the handler. If IsVerbose is true, Level will be slog.LevelDebug.
|
||||
Level slog.Level
|
||||
|
||||
// IsJSONHandler indicates whether to use JSON format for log output.
|
||||
IsJSONHandler bool
|
||||
|
||||
// ReplaceAttr is a function that can be used to replace the value of an attribute.
|
||||
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
|
||||
|
||||
// Output is the writer to write the log to. If nil, os.Stderr will be used.
|
||||
Output io.Writer
|
||||
}
|
||||
|
||||
func (o *Logger) clone() *Logger {
|
||||
return &Logger{
|
||||
AddSource: o.AddSource,
|
||||
IsVerbose: o.IsVerbose,
|
||||
IsJSONHandler: o.IsJSONHandler,
|
||||
ReplaceAttr: o.ReplaceAttr,
|
||||
Output: o.Output,
|
||||
}
|
||||
}
|
||||
|
||||
// SetMaxLevel sets the max logging level for logger, default is slog.LevelWarn.
|
||||
// if IsVerbose is true, level will be slog.LevelDebug.
|
||||
func (o *Logger) SetMaxLevel(level slog.Level) {
|
||||
o.Level = level
|
||||
}
|
||||
|
||||
func (o *Logger) SetJSONHandler() {
|
||||
o.IsJSONHandler = true
|
||||
}
|
||||
|
||||
func (o *Logger) SetTextHandler() {
|
||||
o.IsJSONHandler = false
|
||||
}
|
||||
|
||||
func (o *Logger) SetOutput(output io.Writer) {
|
||||
o.Output = output
|
||||
}
|
||||
|
||||
func (o *Logger) SetVerbose() {
|
||||
o.IsVerbose = true
|
||||
o.Level = slog.LevelDebug
|
||||
}
|
||||
|
||||
func (o *Logger) SetReplaceAttrFunc(replaceAttrFunc func(groups []string, a slog.Attr) slog.Attr) {
|
||||
o.ReplaceAttr = replaceAttrFunc
|
||||
}
|
||||
|
||||
// defaultReplaceAttrFunc is a function that can be used to replace the value of an attribute.
|
||||
// remove time key and source prefix
|
||||
var defaultReplaceAttrFunc = func(groups []string, a slog.Attr) slog.Attr {
|
||||
// Remove time attributes from the log.
|
||||
if a.Key == slog.TimeKey {
|
||||
return slog.Attr{}
|
||||
}
|
||||
// Remove source filepath prefix attributes from the log.
|
||||
if a.Key == slog.SourceKey {
|
||||
source, ok := a.Value.Any().(*slog.Source)
|
||||
if !ok {
|
||||
return slog.Attr{}
|
||||
}
|
||||
if source != nil {
|
||||
source.File = filepath.Base(source.File)
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestConfigure(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
opts := &Logger{
|
||||
AddSource: true,
|
||||
IsVerbose: false,
|
||||
IsJSONHandler: true,
|
||||
Output: buf,
|
||||
}
|
||||
Configure(opts)
|
||||
|
||||
slog.Warn("test message")
|
||||
|
||||
output := buf.String()
|
||||
asserts.Contains(output, "test message", "Log output should contain the test message")
|
||||
}
|
||||
|
||||
func TestSetVerbose(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
logger := Default.clone()
|
||||
logger.SetVerbose()
|
||||
logger.SetOutput(buf)
|
||||
Configure(logger)
|
||||
|
||||
slog.Debug("verbose test")
|
||||
|
||||
output := buf.String()
|
||||
asserts.Contains(output, "verbose test", "Verbose mode should enable debug level logs")
|
||||
}
|
||||
|
||||
func TestLogger_SetJSONHandler(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
logger := Default.clone()
|
||||
logger.SetJSONHandler()
|
||||
asserts.True(logger.IsJSONHandler, "IsJSONHandler should be true")
|
||||
}
|
||||
|
||||
func TestOptionsClone(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
original := &Logger{
|
||||
AddSource: true,
|
||||
IsVerbose: true,
|
||||
IsJSONHandler: true,
|
||||
Output: os.Stdout,
|
||||
}
|
||||
cloned := original.clone()
|
||||
|
||||
asserts.Equal(original.AddSource, cloned.AddSource, "AddSource should be equal")
|
||||
asserts.Equal(original.IsVerbose, cloned.IsVerbose, "IsVerbose should be equal")
|
||||
asserts.Equal(original.IsJSONHandler, cloned.IsJSONHandler, "IsJSONHandler should be equal")
|
||||
asserts.Equal(original.Output, cloned.Output, "Output should be equal")
|
||||
}
|
||||
Reference in New Issue
Block a user