diff --git a/go.mod b/go.mod index 3e30465..29ab0ac 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.22.7 require ( github.com/gorilla/websocket v1.5.3 + github.com/lmittmann/tint v1.0.5 github.com/spf13/viper v1.19.0 ) diff --git a/go.sum b/go.sum index 4f80d2c..cd5a169 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw= +github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= diff --git a/internal/updater/updater.go b/internal/updater/updater.go index 02e0573..95a64a7 100644 --- a/internal/updater/updater.go +++ b/internal/updater/updater.go @@ -2,26 +2,26 @@ package updater import ( "fmt" - "log/slog" "os" "os/signal" "syscall" "time" "github.com/sapphiregaze/discord-gorp/pkg/config" + "github.com/sapphiregaze/discord-gorp/pkg/logger" "github.com/sapphiregaze/discord-gorp/pkg/rpc" ) func Start() { cfg, err := config.Load() if err != nil { - slog.Error(fmt.Sprintf("Failed to load config: %v", err)) + logger.Error(fmt.Sprintf("Failed to load config: %v", err)) os.Exit(1) } client, err := rpc.NewClient() if err != nil { - slog.Error(fmt.Sprintf("Failed to connect to Discord: %v", err)) + logger.Error(fmt.Sprintf("Failed to connect to Discord: %v", err)) os.Exit(1) } defer client.Close() @@ -40,7 +40,7 @@ func Start() { case <-ticker.C: client.SetActivity(activity) case <-sigs: - slog.Info("Shutting down...") + logger.Info("Shutting down...") return } } diff --git a/pkg/config/loader.go b/pkg/config/loader.go index 07c35ec..f54ec17 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -2,11 +2,12 @@ package config import ( "fmt" - "log/slog" "os" "path/filepath" "github.com/spf13/viper" + + "github.com/sapphiregaze/discord-gorp/pkg/logger" ) type Timestamp struct { @@ -88,7 +89,7 @@ func Load() (*Config, error) { return nil, err } - slog.Info(fmt.Sprintf("Config file not found. Copying example config to: %s", configPath)) + logger.Info(fmt.Sprintf("Config file not found. Copying example config to: %s...", configPath)) if err := copy(filepath.Join(currentDir, "configs/config.example.yaml"), configPath); err != nil { return nil, fmt.Errorf("failed to copy example config: %w", err) } diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go new file mode 100644 index 0000000..4a39971 --- /dev/null +++ b/pkg/logger/logger.go @@ -0,0 +1,37 @@ +package logger + +import ( + "log/slog" + "os" + "time" + + "github.com/lmittmann/tint" +) + +var logger *slog.Logger + +type logFunc func(msg string, args ...any) + +func makeLogFunc(level slog.Level) logFunc { + return func(msg string, args ...any) { + logger.Log(nil, level, msg, args...) + } +} + +var ( + Info = makeLogFunc(slog.LevelInfo) + Error = makeLogFunc(slog.LevelError) + Warn = makeLogFunc(slog.LevelWarn) + Debug = makeLogFunc(slog.LevelDebug) +) + +func init() { + w := os.Stderr + logger = slog.New(tint.NewHandler(w, nil)) + slog.SetDefault(slog.New( + tint.NewHandler(w, &tint.Options{ + Level: slog.LevelDebug, + TimeFormat: time.Kitchen, + }), + )) +} diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index 204e049..8786993 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -2,13 +2,13 @@ package rpc import ( "encoding/json" - "log/slog" "os" "time" "github.com/gorilla/websocket" "github.com/sapphiregaze/discord-gorp/pkg/config" + "github.com/sapphiregaze/discord-gorp/pkg/logger" ) type RPCClient struct { @@ -30,7 +30,7 @@ func NewClient() (*RPCClient, error) { func (r *RPCClient) Close() { r.conn.Close() - slog.Info("Disconnected from Discord RPC") + logger.Info("Disconnected from Discord RPC server") } func (r *RPCClient) SetActivity(activity *config.Activity) error { @@ -53,7 +53,7 @@ func (r *RPCClient) SetActivity(activity *config.Activity) error { return err } - slog.Info("Sent activity update to Discord RPC") + logger.Info("Sent activity update to Discord RPC server") return nil }