mirror of
https://codeberg.org/git-pages/git-pages.git
synced 2026-08-18 01:46:03 +00:00
Switch log handling to slog.
This currently doesn't add any structure to the logs, changing just the handler and output format. It does also add Sentry logging support. The `log-format` configuration value now accepts values `none`, `text`, and `json`.
This commit is contained in:
+1
-1
@@ -37,7 +37,7 @@ func (t *Duration) MarshalText() ([]byte, error) {
|
||||
type Config struct {
|
||||
Insecure bool `toml:"-" env:"insecure"`
|
||||
Features []string `toml:"features"`
|
||||
LogFormat string `toml:"log-format" default:"datetime+message"`
|
||||
LogFormat string `toml:"log-format" default:"text"`
|
||||
Server ServerConfig `toml:"server"`
|
||||
Wildcard []WildcardConfig `toml:"wildcard"`
|
||||
Storage StorageConfig `toml:"storage"`
|
||||
|
||||
+2
-9
@@ -71,9 +71,6 @@ func serve(listener net.Listener, handler http.Handler) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
InitObservability()
|
||||
defer FiniObservability()
|
||||
|
||||
printConfigEnvVars := flag.Bool("print-config-env-vars", false,
|
||||
"print every recognized configuration environment variable and exit")
|
||||
printConfig := flag.Bool("print-config", false,
|
||||
@@ -107,12 +104,8 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
switch config.LogFormat {
|
||||
case "message":
|
||||
log.SetFlags(0)
|
||||
case "datetime+message":
|
||||
log.SetFlags(log.Ldate | log.Ltime | log.LUTC)
|
||||
}
|
||||
InitObservability()
|
||||
defer FiniObservability()
|
||||
|
||||
if len(config.Features) > 0 {
|
||||
log.Println("features:", strings.Join(config.Features, ", "))
|
||||
|
||||
+36
-1
@@ -1,16 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/honeybadger-io/honeybadger-go"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
sentryhttp "github.com/getsentry/sentry-go/http"
|
||||
sentryslog "github.com/getsentry/sentry-go/slog"
|
||||
|
||||
slogmulti "github.com/samber/slog-multi"
|
||||
)
|
||||
|
||||
func hasHoneybadger() bool {
|
||||
@@ -27,6 +33,21 @@ func InitObservability() {
|
||||
environment = value
|
||||
}
|
||||
|
||||
logHandlers := []slog.Handler{}
|
||||
|
||||
switch config.LogFormat {
|
||||
case "none":
|
||||
// nothing to do
|
||||
case "text":
|
||||
logHandlers = append(logHandlers,
|
||||
slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{}))
|
||||
case "json":
|
||||
logHandlers = append(logHandlers,
|
||||
slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{}))
|
||||
default:
|
||||
log.Println("unknown log format", config.LogFormat)
|
||||
}
|
||||
|
||||
if hasHoneybadger() {
|
||||
honeybadger.Configure(honeybadger.Configuration{
|
||||
Env: environment,
|
||||
@@ -35,13 +56,27 @@ func InitObservability() {
|
||||
}
|
||||
|
||||
if hasSentry() {
|
||||
enableLogs := false
|
||||
if value, err := strconv.ParseBool(os.Getenv("SENTRY_LOGS")); err == nil {
|
||||
enableLogs = value
|
||||
}
|
||||
|
||||
options := sentry.ClientOptions{}
|
||||
options.Environment = environment
|
||||
options.Dsn = os.Getenv("SENTRY_DSN")
|
||||
options.Environment = environment
|
||||
options.EnableLogs = enableLogs
|
||||
if err := sentry.Init(options); err != nil {
|
||||
log.Fatalf("sentry: %s\n", err)
|
||||
}
|
||||
|
||||
if enableLogs {
|
||||
logHandlers = append(logHandlers, sentryslog.Option{
|
||||
AddSource: true,
|
||||
}.NewSentryHandler(context.Background()))
|
||||
}
|
||||
}
|
||||
|
||||
slog.SetDefault(slog.New(slogmulti.Fanout(logHandlers...)))
|
||||
}
|
||||
|
||||
func FiniObservability() {
|
||||
|
||||
Reference in New Issue
Block a user