-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(logging)!: switch to slog for logging
Go now includes a nice way to do structured logs, including JSON. Use that instead of a third party module. BREAKING CHANGE: log format will change a little bit
- Loading branch information
Showing
8 changed files
with
177 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import "log/slog" | ||
|
||
// structuredError is an error that can be logged with additional data | ||
type structuredError struct { | ||
err error | ||
args []any | ||
} | ||
|
||
// NewStructuredError returns a new structure error that will be logged with additional data | ||
func serror(err error, args ...any) *structuredError { | ||
return &structuredError{err: err, args: args} | ||
} | ||
|
||
func (e *structuredError) Error() string { | ||
return e.err.Error() | ||
} | ||
|
||
func (e *structuredError) Unwrap() error { | ||
return e.err | ||
} | ||
|
||
// LogValue returns a structured log value for use with slog | ||
func (e structuredError) LogValue() slog.Value { | ||
args := make([]slog.Attr, 0, (len(e.args)/2)+1) | ||
args = append(args, slog.String("message", e.Error())) | ||
|
||
var c interface{} | ||
for _, a := range e.args { | ||
if c == nil { | ||
c = a | ||
continue | ||
} | ||
args = append(args, slog.Any(c.(string), a)) | ||
c = nil | ||
} | ||
|
||
return slog.GroupValue(args...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStructuredError(t *testing.T) { | ||
wrapped := fmt.Errorf("test error: %w", os.ErrNotExist) | ||
err := serror(wrapped, "foo", "bar") | ||
|
||
require.ErrorContains(t, err, "test error") | ||
require.ErrorIs(t, err, os.ErrNotExist) | ||
|
||
l, log := testLogger() | ||
l.Error("test", "error", err) | ||
assert.Contains(t, log.String(), `"error":{"message":"test error: file does not exist","foo":"bar"}`) | ||
} | ||
|
||
func testLogger() (*slog.Logger, *bytes.Buffer) { | ||
log := &bytes.Buffer{} | ||
return slog.New(slog.NewJSONHandler(log, &slog.HandlerOptions{ | ||
Level: slog.LevelDebug, | ||
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr { | ||
if a.Key == "time" { | ||
a.Value = slog.StringValue("test-time") | ||
} | ||
return a | ||
}, | ||
})), log | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.