mirror of
https://github.com/sapphiregaze/discord-gorp.git
synced 2025-12-05 20:40:02 -08:00
Added Better Logging:
- Added logger package with colorized logging
This commit is contained in:
1
go.mod
1
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
|
||||
)
|
||||
|
||||
|
||||
2
go.sum
2
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=
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
37
pkg/logger/logger.go
Normal file
37
pkg/logger/logger.go
Normal file
@@ -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,
|
||||
}),
|
||||
))
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user