Skip to content

Commit

Permalink
slog
Browse files Browse the repository at this point in the history
  • Loading branch information
mazrean committed Aug 9, 2024
1 parent 3717b35 commit 3b7390f
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 43 deletions.
8 changes: 6 additions & 2 deletions db/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package isudb

import (
"database/sql"
"log"
"log/slog"
"strconv"
"sync/atomic"
"time"
Expand Down Expand Up @@ -37,7 +37,11 @@ func DBMetricsSetup[T interface {
if fixInterpolateParams {
config, err := mysql.ParseDSN(dataSourceName)
if err != nil {
log.Printf("failed to parse dsn: %v\n", err)
slog.Error("failed to parse DSN",
slog.String("driver", driverName),
slog.String("dsn", dataSourceName),
slog.String("error", err.Error()),
)
goto CONNECT
}

Expand Down
11 changes: 8 additions & 3 deletions http/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package isuhttp
import (
"errors"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"strconv"
Expand All @@ -28,7 +28,10 @@ func init() {

subEnableGoJson, err := strconv.ParseBool(strEnableGoJson)
if err != nil {
log.Printf("failed to parse GO_JSON: %s\n", err)
slog.Error("failed to parse GO_JSON",
slog.String("GO_JSON", strEnableGoJson),
slog.String("error", err.Error()),
)
return
}

Expand All @@ -44,7 +47,9 @@ func EchoSetting(e *echo.Echo) *echo.Echo {

listener, ok, err := newUnixDomainSockListener()
if err != nil {
log.Printf("failed to init unix domain socket: %s\n", err)
slog.Error("failed to create unix domain socket listener",
slog.String("error", err.Error()),
)
}

if ok {
Expand Down
10 changes: 7 additions & 3 deletions http/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package isuhttp
import (
"errors"
"fmt"
"log"
"log/slog"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -35,13 +35,17 @@ func FiberNew(conf ...fiber.Config) *fiber.App {

listener, ok, err := newUnixDomainSockListener()
if err != nil {
log.Printf("failed to init unix domain socket: %s\n", err)
slog.Error("failed to create unix domain socket listener",
slog.String("error", err.Error()),
)
}

if ok {
err := app.Listener(listener)
if err != nil {
log.Printf("failed to set listener: %s\n", err)
slog.Error("failed to set unix domain socket listener",
slog.String("error", err.Error()),
)
}
}

Expand Down
10 changes: 7 additions & 3 deletions http/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package isuhttp

import (
"fmt"
"log"
"log/slog"
"strconv"
"strings"
"time"
Expand All @@ -21,7 +21,9 @@ func GinNew(engine *gin.Engine) *gin.Engine {
func GinRun(engine *gin.Engine, addrs ...string) error {
listener, ok, err := newUnixDomainSockListener()
if err != nil {
log.Printf("failed to init unix domain socket: %s\n", err)
slog.Error("failed to create unix domain socket listener",
slog.String("error", err.Error()),
)
}

if ok {
Expand All @@ -34,7 +36,9 @@ func GinRun(engine *gin.Engine, addrs ...string) error {
func GinRunTLS(engine *gin.Engine, addr, certFile, keyFile string) error {
listener, ok, err := newUnixDomainSockListener()
if err != nil {
log.Printf("failed to init unix domain socket: %s\n", err)
slog.Error("failed to create unix domain socket listener",
slog.String("error", err.Error()),
)
}

if ok {
Expand Down
37 changes: 27 additions & 10 deletions internal/benchmark/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/gob"
"encoding/json"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -52,7 +52,10 @@ func init() {
f, err := os.Open(gobFile)
if err != nil {
if !os.IsNotExist(err) {
log.Printf("failed to open gob file(%s): %s\n", gobFile, err)
slog.Error("failed to open gob file",
slog.String("file", gobFile),
slog.String("error", err.Error()),
)
}

return
Expand All @@ -61,7 +64,10 @@ func init() {

err = gob.NewDecoder(f).Decode(latest)
if err != nil {
log.Printf("failed to decode gob file(%s): %s\n", gobFile, err)
slog.Error("failed to decode gob file",
slog.String("file", gobFile),
slog.String("error", err.Error()),
)
}

scoreGauge.Set(float64(latest.Score))
Expand Down Expand Up @@ -99,19 +105,30 @@ func setScore(ctx context.Context, score int64) {
scoreGauge.Set(float64(score))
durationGauge.Set(latest.End.Sub(latest.Start).Seconds())

for _, f := range endHooks {
f(ctx, latest)
}

f, err := os.Create(gobFile)
if err != nil {
log.Printf("failed to create gob file(%s): %s\n", gobFile, err)
return
slog.Error("failed to create gob file",
slog.String("file", gobFile),
slog.String("error", err.Error()),
)
}
defer f.Close()

err = gob.NewEncoder(f).Encode(latest)
if err != nil {
log.Printf("failed to encode gob file(%s): %s\n", gobFile, err)
}

for _, f := range endHooks {
f(ctx, latest)
slog.Error("failed to encode gob file",
slog.String("file", gobFile),
slog.Group("latest",
slog.Time("start", latest.Start),
slog.Time("end", latest.End),
slog.Int64("score", latest.Score),
),
slog.String("error", err.Error()),
)
}
}

Expand Down
7 changes: 5 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"log"
"log/slog"
"os"
"strconv"
"strings"
Expand All @@ -18,7 +18,10 @@ func init() {
if ok {
enable, err := strconv.ParseBool(strings.TrimSpace(strEnable))
if err != nil {
log.Printf("failed to parse ISUTOOLS_ENABLE: %v", err)
slog.Error("failed to parse ISUTOOLS_ENABLE",
slog.String("ISUTOOLS_ENABLE", strEnable),
slog.String("error", err.Error()),
)
return
}

Expand Down
40 changes: 40 additions & 0 deletions internal/log/slog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package log

import (
"context"
"log/slog"
"os"

"github.com/mazrean/isucon-go-tools/v2/internal/config"
)

func init() {
var logger *slog.Logger
if config.Enable {
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
} else {
logger = slog.New(DiscardHandler{})
}

slog.SetDefault(logger)
}

type DiscardHandler struct{}

func (h DiscardHandler) Enabled(_ context.Context, _ slog.Level) bool {
return false
}

func (h DiscardHandler) Handle(_ context.Context, _ slog.Record) error {
return nil
}

func (h DiscardHandler) WithAttrs(_ []slog.Attr) slog.Handler {
return h
}

func (h DiscardHandler) WithGroup(_ string) slog.Handler {
return h
}
18 changes: 13 additions & 5 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package profiler

import (
"log"
"log/slog"
"net/http"
"os"
"runtime"
Expand All @@ -21,7 +21,10 @@ func init() {
if ok {
blockRate, err := strconv.Atoi(strBlockRate)
if err != nil {
log.Printf("failed to parse BLOCK_RATE(%s): %v", strBlockRate, err)
slog.Error("failed to parse BLOCK_RATE",
slog.String("BLOCK_RATE", strBlockRate),
slog.String("error", err.Error()),
)
} else {
runtime.SetBlockProfileRate(blockRate)
}
Expand All @@ -31,15 +34,20 @@ func init() {
if ok {
mutexRate, err := strconv.Atoi(strMutexRate)
if err != nil {
log.Printf("failed to parse MUTEX_RATE(%s): %v", strMutexRate, err)
slog.Error("failed to parse MUTEX_RATE",
slog.String("MUTEX_RATE", strMutexRate),
slog.String("error", err.Error()),
)
} else {
runtime.SetMutexProfileFraction(mutexRate)
}
}

err := pyroscopeStart()
err := setupPyroscope()
if err != nil {
log.Printf("failed to init pyroscope: %v", err)
slog.Error("failed to setup pyroscope",
slog.String("error", err.Error()),
)
}
}

Expand Down
Loading

0 comments on commit 3b7390f

Please sign in to comment.