mirror of
https://codeberg.org/git-pages/git-pages.git
synced 2026-08-28 03:46:06 +00:00
Add the ability to send logs to a syslog daemon.
This commit is contained in:
+26
-2
@@ -13,10 +13,13 @@ import (
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
slogmulti "github.com/samber/slog-multi"
|
||||
|
||||
syslog "codeberg.org/git-pages/go-slog-syslog"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
|
||||
@@ -42,6 +45,8 @@ var (
|
||||
}, []string{"method"})
|
||||
)
|
||||
|
||||
var syslogHandler syslog.Handler
|
||||
|
||||
func hasSentry() bool {
|
||||
return os.Getenv("SENTRY_DSN") != ""
|
||||
}
|
||||
@@ -83,6 +88,19 @@ func InitObservability() {
|
||||
log.Println("unknown log format", config.LogFormat)
|
||||
}
|
||||
|
||||
if syslogAddr := os.Getenv("SYSLOG_ADDR"); syslogAddr != "" {
|
||||
var err error
|
||||
syslogHandler, err = syslog.NewHandler(&syslog.HandlerOptions{
|
||||
Address: syslogAddr,
|
||||
AppName: "git-pages",
|
||||
StructuredDataID: "git-pages",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("syslog: %v", err)
|
||||
}
|
||||
logHandlers = append(logHandlers, syslogHandler)
|
||||
}
|
||||
|
||||
if hasSentry() {
|
||||
enableLogs := false
|
||||
if value, err := strconv.ParseBool(os.Getenv("SENTRY_LOGS")); err == nil {
|
||||
@@ -154,9 +172,15 @@ func levelsFromMinimum(minLevel slog.Level) []slog.Level {
|
||||
}
|
||||
|
||||
func FiniObservability() {
|
||||
if hasSentry() {
|
||||
sentry.Flush(2 * time.Second)
|
||||
var wg sync.WaitGroup
|
||||
timeout := 2 * time.Second
|
||||
if syslogHandler != nil {
|
||||
wg.Go(func() { syslogHandler.Flush(timeout) })
|
||||
}
|
||||
if hasSentry() {
|
||||
wg.Go(func() { sentry.Flush(timeout) })
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func ObserveError(err error) {
|
||||
|
||||
Reference in New Issue
Block a user