Skip to content

Commit

Permalink
Add util.EventHookWithContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Sep 12, 2024
1 parent 0e0d7d5 commit f8975f8
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions util/service.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
package util

import (
"context"
"log/slog"

"github.com/thejerf/suture/v4"
)

func EventHook(e suture.Event) {
var args []any
for k, v := range e.Map() {
args = append(args, k, v)
}
var EventHook = EventHookWithContext(context.Background())

func EventHookWithContext(ctx context.Context) suture.EventHook {
return func(e suture.Event) {
var args []any
for k, v := range e.Map() {
args = append(args, k, v)
}

switch e.Type() {
case suture.EventTypeBackoff:
slog.Info(e.String(), args...)
switch e.Type() {
case suture.EventTypeBackoff:
slog.InfoContext(ctx, e.String(), args...)

case suture.EventTypeResume:
slog.Info(e.String(), args...)
case suture.EventTypeResume:
slog.InfoContext(ctx, e.String(), args...)

case suture.EventTypeServicePanic:
slog.Warn(e.String(), args...)
case suture.EventTypeServicePanic:
slog.WarnContext(ctx, e.String(), args...)

case suture.EventTypeServiceTerminate:
slog.Warn(e.String(), args...)
case suture.EventTypeServiceTerminate:
slog.WarnContext(ctx, e.String(), args...)

case suture.EventTypeStopTimeout:
slog.Error(e.String(), args...)
case suture.EventTypeStopTimeout:
slog.ErrorContext(ctx, e.String(), args...)

default:
panic(e) // unhandled event
default:
panic(e) // unhandled event
}
}
}

0 comments on commit f8975f8

Please sign in to comment.