-
-
Notifications
You must be signed in to change notification settings - Fork 3
Use Slog #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Use Slog #32
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,7 +6,7 @@ import ( | |||||||||||||
| "errors" | ||||||||||||||
| "fmt" | ||||||||||||||
| "io" | ||||||||||||||
| "log" | ||||||||||||||
| "log/slog" | ||||||||||||||
| "net/http" | ||||||||||||||
| "os" | ||||||||||||||
| "path/filepath" | ||||||||||||||
|
|
@@ -20,19 +20,13 @@ import ( | |||||||||||||
| var ErrPathIsNotDir = errors.New("path is not a dir") | ||||||||||||||
| var validGhEvent = regexp.MustCompile(`^[a-z\d_]{1,30}$`) | ||||||||||||||
|
|
||||||||||||||
| // Logger handles Printf | ||||||||||||||
| type Logger interface { | ||||||||||||||
| Printf(format string, v ...any) | ||||||||||||||
| Println(v ...any) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // HookServer implements net/http.Handler | ||||||||||||||
| type HookServer struct { | ||||||||||||||
| RootDir string | ||||||||||||||
|
|
||||||||||||||
| Timeout time.Duration | ||||||||||||||
| ErrorLog Logger | ||||||||||||||
| InfoLog Logger | ||||||||||||||
| ErrorLog *slog.Logger | ||||||||||||||
| InfoLog *slog.Logger | ||||||||||||||
|
|
||||||||||||||
| sync.Mutex | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -86,15 +80,15 @@ func ServerExecTimeout(timeout time.Duration) ServerOption { | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // ServerErrorLog configures the HookServer error logger | ||||||||||||||
| func ServerErrorLog(log Logger) ServerOption { | ||||||||||||||
| func ServerErrorLog(log *slog.Logger) ServerOption { | ||||||||||||||
| return func(h *HookServer) error { | ||||||||||||||
| h.ErrorLog = log | ||||||||||||||
| return nil | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // ServerInfoLog configures the HookServer info logger | ||||||||||||||
| func ServerInfoLog(log Logger) ServerOption { | ||||||||||||||
| func ServerInfoLog(log *slog.Logger) ServerOption { | ||||||||||||||
| return func(h *HookServer) error { | ||||||||||||||
| h.InfoLog = log | ||||||||||||||
| return nil | ||||||||||||||
|
|
@@ -123,7 +117,7 @@ func (h *HookServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |||||||||||||
| b, err := io.ReadAll(r.Body) | ||||||||||||||
| if err != nil { | ||||||||||||||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||||||||||||||
| log.Println(ghDelivery, err) | ||||||||||||||
| slog.Info("request error", "delivery", ghDelivery, "error", err) | ||||||||||||||
|
||||||||||||||
| slog.Info("request error", "delivery", ghDelivery, "error", err) | |
| if h.ErrorLog != nil { | |
| h.ErrorLog.Error("request error", "delivery", ghDelivery, "error", err) | |
| } |
Copilot
AI
Jun 24, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace this global slog.Info call with the instance’s h.ErrorLog.Error(...) to log decoding failures correctly and honor configured log destinations and levels.
| slog.Info("json decode error", "delivery", ghDelivery, "error", err) | |
| if h.ErrorLog != nil { | |
| h.ErrorLog.Error("json decode error", | |
| "delivery", ghDelivery, | |
| "error", err) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Calling
os.Exitand logging with the global logger here hinders testability and errors handling. Instead, return an error for unknown levels and let the caller decide to exit.