bingobot/internal/logging/logging.go
Piper Pentagram 0c64670c33 Use absolute paths where possible
bingobot blindly uses the shell's working dir to write logs and read
configs. This change introduces the following changes:

1. start.sh now determines the project root's absolute path before
   reading the token file and starting the app.
2. the config package now determine's the executable's working directory
   (rather than assuming it's the same as the shell's) before loading
   configs. It also now exposes this for use in other packages.
3. the logging package now uses config.GetWorkingDir() to determine
   where to write logs.
2024-11-14 11:16:30 -08:00

51 lines
842 B
Go

package logging
import (
"log/slog"
"path/filepath"
"gitlab.com/whom/bingobot/internal/config"
"gopkg.in/natefinch/lumberjack.v2"
)
type Logger struct {
*slog.Logger
}
var (
logger Logger
)
func Init() {
cfg := config.Get()
lj := &lumberjack.Logger{
Filename: filepath.Join(config.GetWorkingDir(), cfg.LogDir, cfg.LogFile),
MaxSize: cfg.LogMaxSizeMB,
MaxBackups: cfg.LogMaxBackups,
MaxAge: cfg.LogMaxAgeDays,
Compress: cfg.LogCompression,
}
logger = Logger{
slog.New(
slog.NewJSONHandler(lj, &slog.HandlerOptions{}),
),
}
}
func Debug(msg string, args ...any) {
logger.Debug(msg, args...)
}
func Info(msg string, args ...any) {
logger.Info(msg, args...)
}
func Warn(msg string, args ...any) {
logger.Warn(msg, args...)
}
func Error(msg string, args ...any) {
logger.Error(msg, args...)
}