Add JSON structured logging
This commit is contained in:
parent
60b9d77873
commit
c06d77485c
6 changed files with 51 additions and 6 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
|||
bot.token
|
||||
bingobot
|
||||
bingobot.log
|
||||
|
|
@ -15,6 +15,7 @@ type AppConfig struct {
|
|||
LogMaxBackups int `yaml:"log_max_backups"`
|
||||
LogMaxAgeDays int `yaml:"log_max_age_days"`
|
||||
LogCompression bool `yaml:"log_compression"`
|
||||
LogAddSource bool `yaml:"log_add_source"`
|
||||
}
|
||||
|
||||
func parseConfigs() error {
|
||||
|
|
@ -43,9 +44,10 @@ func parseConfigs() error {
|
|||
|
||||
func setDefaults() {
|
||||
viper.SetDefault("LogFile", "bingobot.log")
|
||||
viper.SetDefault("LogDir", "bingobot.log")
|
||||
viper.SetDefault("LogDir", "log")
|
||||
viper.SetDefault("LogMaxSizeMB", 500)
|
||||
viper.SetDefault("LogMaxBackups", 3)
|
||||
viper.SetDefault("LogMaxAgeDays", 365)
|
||||
viper.SetDefault("LogCompression", false)
|
||||
viper.SetDefault("LogAddSource", true)
|
||||
}
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -5,6 +5,7 @@ go 1.23.2
|
|||
require (
|
||||
github.com/bwmarrin/discordgo v0.28.1
|
||||
github.com/spf13/viper v1.19.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -78,6 +78,8 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogR
|
|||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
|||
27
log.go
Normal file
27
log.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
Log *slog.Logger
|
||||
)
|
||||
|
||||
func initLogger() {
|
||||
|
||||
lj := &lumberjack.Logger{
|
||||
Filename: filepath.Join(appConfig.LogDir, appConfig.LogFile),
|
||||
MaxSize: appConfig.LogMaxSizeMB,
|
||||
MaxBackups: appConfig.LogMaxBackups,
|
||||
MaxAge: appConfig.LogMaxAgeDays,
|
||||
Compress: appConfig.LogCompression,
|
||||
}
|
||||
|
||||
Log = slog.New(slog.NewJSONHandler(lj, &slog.HandlerOptions{
|
||||
AddSource: true,
|
||||
}))
|
||||
}
|
||||
22
main.go
22
main.go
|
|
@ -27,25 +27,37 @@ func main() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
startBot()
|
||||
initLogger()
|
||||
|
||||
err = startBot()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func startBot() {
|
||||
func startBot() error {
|
||||
session, _ := discordgo.New("Bot " + *Token)
|
||||
|
||||
err := session.Open()
|
||||
if err != nil {
|
||||
log.Fatalf("could not open session: %s", err)
|
||||
Log.Error("could not open discord session", "type", "error", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
Log.Info("shutting down gracefully", "type", "shutdown")
|
||||
|
||||
sigch := make(chan os.Signal, 1)
|
||||
signal.Notify(sigch, os.Interrupt)
|
||||
<-sigch
|
||||
|
||||
log.Printf("shutting down gracefully...")
|
||||
Log.Info("shutting down gracefully", "type", "shutdown")
|
||||
|
||||
err = session.Close()
|
||||
if err != nil {
|
||||
log.Printf("could not close session gracefully: %s", err)
|
||||
Log.Error("could not close discord session gracefully", "type", "error", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue