-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Msg.SlogRecord and use slog formatting to marshal it
- Loading branch information
Showing
7 changed files
with
96 additions
and
2 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
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,48 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"log/slog" | ||
"sync" | ||
) | ||
|
||
var ( | ||
gstbhLocker sync.Mutex | ||
globalSlogTextBufferHandler = slogTextBufferHandler{} | ||
) | ||
|
||
func init() { | ||
globalSlogTextBufferHandler.init() | ||
} | ||
|
||
func (me *slogTextBufferHandler) handleAppend(b []byte, r slog.Record) []byte { | ||
me.buf.Reset() | ||
err := me.handler.Handle(context.Background(), r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return append(b, me.buf.Bytes()...) | ||
} | ||
|
||
type slogTextBufferHandler struct { | ||
buf bytes.Buffer | ||
handler *slog.TextHandler | ||
} | ||
|
||
func (me *slogTextBufferHandler) init() { | ||
me.handler = slog.NewTextHandler(&me.buf, &slog.HandlerOptions{ | ||
AddSource: false, | ||
Level: toSlogMinLevel(NotSet), | ||
ReplaceAttr: func(groups []string, a slog.Attr) (ret slog.Attr) { | ||
if len(groups) == 0 { | ||
switch a.Key { | ||
case slog.TimeKey, slog.LevelKey: | ||
return | ||
} | ||
} | ||
ret = a | ||
return | ||
}, | ||
}) | ||
} |
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