mirror of
https://github.com/h44z/wg-portal.git
synced 2025-09-15 07:11:15 +00:00
chore: replace logrus with standard lib log/slog
This commit is contained in:
@@ -4,14 +4,14 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/sirupsen/logrus"
|
||||
gormMySQL "gorm.io/driver/mysql"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlserver"
|
||||
"gorm.io/gorm"
|
||||
@@ -32,13 +32,15 @@ type SysStat struct {
|
||||
SchemaVersion uint64 `gorm:"primaryKey,column:schema_version"`
|
||||
}
|
||||
|
||||
// GormLogger is a custom logger for Gorm, making it use logrus.
|
||||
// GormLogger is a custom logger for Gorm, making it use slog
|
||||
type GormLogger struct {
|
||||
SlowThreshold time.Duration
|
||||
SourceField string
|
||||
IgnoreErrRecordNotFound bool
|
||||
Debug bool
|
||||
Silent bool
|
||||
|
||||
prefix string
|
||||
}
|
||||
|
||||
func NewLogger(slowThreshold time.Duration, debug bool) *GormLogger {
|
||||
@@ -48,6 +50,7 @@ func NewLogger(slowThreshold time.Duration, debug bool) *GormLogger {
|
||||
IgnoreErrRecordNotFound: true,
|
||||
Silent: false,
|
||||
SourceField: "src",
|
||||
prefix: "GORM-SQL: ",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,21 +67,21 @@ func (l *GormLogger) Info(ctx context.Context, s string, args ...any) {
|
||||
if l.Silent {
|
||||
return
|
||||
}
|
||||
logrus.WithContext(ctx).Infof(s, args...)
|
||||
slog.InfoContext(ctx, l.prefix+s, args...)
|
||||
}
|
||||
|
||||
func (l *GormLogger) Warn(ctx context.Context, s string, args ...any) {
|
||||
if l.Silent {
|
||||
return
|
||||
}
|
||||
logrus.WithContext(ctx).Warnf(s, args...)
|
||||
slog.WarnContext(ctx, l.prefix+s, args...)
|
||||
}
|
||||
|
||||
func (l *GormLogger) Error(ctx context.Context, s string, args ...any) {
|
||||
if l.Silent {
|
||||
return
|
||||
}
|
||||
logrus.WithContext(ctx).Errorf(s, args...)
|
||||
slog.ErrorContext(ctx, l.prefix+s, args...)
|
||||
}
|
||||
|
||||
func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
|
||||
@@ -88,26 +91,29 @@ func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (stri
|
||||
|
||||
elapsed := time.Since(begin)
|
||||
sql, rows := fc()
|
||||
fields := logrus.Fields{
|
||||
"rows": rows,
|
||||
"duration": elapsed,
|
||||
|
||||
attrs := []any{
|
||||
"rows", rows,
|
||||
"duration", elapsed,
|
||||
}
|
||||
|
||||
if l.SourceField != "" {
|
||||
fields[l.SourceField] = utils.FileWithLineNum()
|
||||
attrs = append(attrs, l.SourceField, utils.FileWithLineNum())
|
||||
}
|
||||
|
||||
if err != nil && !(errors.Is(err, gorm.ErrRecordNotFound) && l.IgnoreErrRecordNotFound) {
|
||||
fields[logrus.ErrorKey] = err
|
||||
logrus.WithContext(ctx).WithFields(fields).Errorf("%s", sql)
|
||||
attrs = append(attrs, "error", err)
|
||||
slog.ErrorContext(ctx, l.prefix+sql, attrs...)
|
||||
return
|
||||
}
|
||||
|
||||
if l.SlowThreshold != 0 && elapsed > l.SlowThreshold {
|
||||
logrus.WithContext(ctx).WithFields(fields).Warnf("%s", sql)
|
||||
slog.WarnContext(ctx, l.prefix+sql, attrs...)
|
||||
return
|
||||
}
|
||||
|
||||
if l.Debug {
|
||||
logrus.WithContext(ctx).WithFields(fields).Tracef("%s", sql)
|
||||
slog.DebugContext(ctx, l.prefix+sql, attrs...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +124,7 @@ func NewDatabase(cfg config.DatabaseConfig) (*gorm.DB, error) {
|
||||
|
||||
switch cfg.Type {
|
||||
case config.DatabaseMySQL:
|
||||
gormDb, err = gorm.Open(gormMySQL.Open(cfg.DSN), &gorm.Config{
|
||||
gormDb, err = gorm.Open(mysql.Open(cfg.DSN), &gorm.Config{
|
||||
Logger: NewLogger(cfg.SlowQueryThreshold, cfg.Debug),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -212,13 +218,13 @@ func (r *SqlRepo) preCheck() error {
|
||||
}
|
||||
|
||||
func (r *SqlRepo) migrate() error {
|
||||
logrus.Tracef("sysstat migration: %v", r.db.AutoMigrate(&SysStat{}))
|
||||
logrus.Tracef("user migration: %v", r.db.AutoMigrate(&domain.User{}))
|
||||
logrus.Tracef("interface migration: %v", r.db.AutoMigrate(&domain.Interface{}))
|
||||
logrus.Tracef("peer migration: %v", r.db.AutoMigrate(&domain.Peer{}))
|
||||
logrus.Tracef("peer status migration: %v", r.db.AutoMigrate(&domain.PeerStatus{}))
|
||||
logrus.Tracef("interface status migration: %v", r.db.AutoMigrate(&domain.InterfaceStatus{}))
|
||||
logrus.Tracef("audit data migration: %v", r.db.AutoMigrate(&domain.AuditEntry{}))
|
||||
slog.Debug("running migration: sys-stat", "result", r.db.AutoMigrate(&SysStat{}))
|
||||
slog.Debug("running migration: user", "result", r.db.AutoMigrate(&domain.User{}))
|
||||
slog.Debug("running migration: interface", "result", r.db.AutoMigrate(&domain.Interface{}))
|
||||
slog.Debug("running migration: peer", "result", r.db.AutoMigrate(&domain.Peer{}))
|
||||
slog.Debug("running migration: peer status", "result", r.db.AutoMigrate(&domain.PeerStatus{}))
|
||||
slog.Debug("running migration: interface status", "result", r.db.AutoMigrate(&domain.InterfaceStatus{}))
|
||||
slog.Debug("running migration: audit data", "result", r.db.AutoMigrate(&domain.AuditEntry{}))
|
||||
|
||||
existingSysStat := SysStat{}
|
||||
r.db.Where("schema_version = ?", SchemaVersion).First(&existingSysStat)
|
||||
@@ -230,7 +236,7 @@ func (r *SqlRepo) migrate() error {
|
||||
if err := r.db.Create(&sysStat).Error; err != nil {
|
||||
return fmt.Errorf("failed to write sysstat entry for schema version %d: %w", SchemaVersion, err)
|
||||
}
|
||||
logrus.Debugf("sysstat entry for schema version %d written", SchemaVersion)
|
||||
slog.Debug("sys-stat entry written", "schema_version", SchemaVersion)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@@ -3,10 +3,9 @@ package adapters
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type FilesystemRepo struct {
|
||||
@@ -46,7 +45,7 @@ func (r *FilesystemRepo) WriteFile(path string, contents io.Reader) error {
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
if err := file.Close(); err != nil {
|
||||
logrus.Errorf("failed to close file %s: %v", file.Name(), err)
|
||||
slog.Error("failed to close file", "file", file.Name(), "error", err)
|
||||
}
|
||||
}(file)
|
||||
|
||||
|
@@ -3,13 +3,13 @@ package adapters
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/h44z/wg-portal/internal"
|
||||
"github.com/h44z/wg-portal/internal/config"
|
||||
@@ -91,11 +91,11 @@ func (m *MetricsServer) Run(ctx context.Context) {
|
||||
// Run the metrics server in a goroutine
|
||||
go func() {
|
||||
if err := m.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
logrus.Errorf("metrics service on %s exited: %v", m.Addr, err)
|
||||
slog.Error("metrics service exited", "address", m.Addr, "error", err)
|
||||
}
|
||||
}()
|
||||
|
||||
logrus.Infof("started metrics service on %s", m.Addr)
|
||||
slog.Info("started metrics service", "address", m.Addr)
|
||||
|
||||
// Wait for the context to be done
|
||||
<-ctx.Done()
|
||||
@@ -106,9 +106,9 @@ func (m *MetricsServer) Run(ctx context.Context) {
|
||||
|
||||
// Attempt to gracefully shut down the metrics server
|
||||
if err := m.Shutdown(shutdownCtx); err != nil {
|
||||
logrus.Errorf("metrics service on %s shutdown failed: %v", m.Addr, err)
|
||||
slog.Error("metrics service shutdown failed", "address", m.Addr, "error", err)
|
||||
} else {
|
||||
logrus.Infof("metrics service on %s shutdown gracefully", m.Addr)
|
||||
slog.Info("metrics service shutdown gracefully", "address", m.Addr)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,11 +3,10 @@ package adapters
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/h44z/wg-portal/internal"
|
||||
"github.com/h44z/wg-portal/internal/domain"
|
||||
)
|
||||
@@ -35,7 +34,7 @@ func (r *WgQuickRepo) ExecuteInterfaceHook(id domain.InterfaceIdentifier, hookCm
|
||||
return nil
|
||||
}
|
||||
|
||||
logrus.Tracef("interface %s: executing hook %s", id, hookCmd)
|
||||
slog.Debug("executing interface hook", "interface", id, "hook", hookCmd)
|
||||
err := r.exec(hookCmd, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to exec hook: %w", err)
|
||||
@@ -107,6 +106,8 @@ func (r *WgQuickRepo) exec(command string, interfaceId domain.InterfaceIdentifie
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to exexute shell command %s: %w", commandWithInterfaceName, err)
|
||||
}
|
||||
logrus.Tracef("executed shell command %s, with output: %s", commandWithInterfaceName, string(out))
|
||||
slog.Debug("executed shell command",
|
||||
"command", commandWithInterfaceName,
|
||||
"output", string(out))
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user