-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding support for AddSource+ReplaceAttr - moving some code to …
…samber/slog-common
- Loading branch information
Showing
8 changed files
with
62 additions
and
148 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,49 @@ | ||
package sloggraylog | ||
|
||
import ( | ||
"encoding" | ||
"fmt" | ||
"reflect" | ||
|
||
"log/slog" | ||
) | ||
|
||
type Converter func(loggerAttr []slog.Attr, record *slog.Record) map[string]any | ||
slogcommon "github.com/samber/slog-common" | ||
) | ||
|
||
func DefaultConverter(loggerAttr []slog.Attr, record *slog.Record) map[string]any { | ||
log := map[string]any{ | ||
"timestamp": record.Time.UTC(), | ||
"level": record.Level.String(), | ||
"message": record.Message, | ||
} | ||
var SourceKey = "source" | ||
var ContextKey = "extra" | ||
var ErrorKeys = []string{"error", "err"} | ||
|
||
extra := attrsToValue(loggerAttr) | ||
type Converter func(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record) map[string]any | ||
|
||
record.Attrs(func(attr slog.Attr) bool { | ||
for k, v := range attrsToValue([]slog.Attr{attr}) { | ||
extra[k] = v | ||
} | ||
return true | ||
}) | ||
func DefaultConverter(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record) map[string]any { | ||
// aggregate all attributes | ||
attrs := slogcommon.AppendRecordAttrsToAttrs(loggerAttr, groups, record) | ||
|
||
if err, ok := extra["error"]; ok { | ||
log["error"] = err | ||
delete(extra, "error") | ||
// developer formatters | ||
if addSource { | ||
attrs = append(attrs, slogcommon.Source(SourceKey, record)) | ||
} | ||
attrs = slogcommon.ReplaceAttrs(replaceAttr, []string{}, attrs...) | ||
|
||
log["extra"] = extra | ||
|
||
return log | ||
} | ||
|
||
func attrsToValue(attrs []slog.Attr) map[string]any { | ||
log := map[string]any{} | ||
|
||
for i := range attrs { | ||
k, v := attrToValue(attrs[i]) | ||
log[k] = v | ||
// handler formatter | ||
log := map[string]any{ | ||
"logger.name": name, | ||
"logger.version": version, | ||
"timestamp": record.Time.UTC(), | ||
"level": record.Level.String(), | ||
"message": record.Message, | ||
} | ||
|
||
return log | ||
} | ||
|
||
func attrToValue(attr slog.Attr) (string, any) { | ||
k := attr.Key | ||
v := attr.Value | ||
kind := v.Kind() | ||
extra := slogcommon.AttrsToMap(attrs...) | ||
|
||
switch kind { | ||
case slog.KindAny: | ||
if k == "error" { | ||
if err, ok := v.Any().(error); ok { | ||
return k, buildExceptions(err) | ||
for _, errorKey := range ErrorKeys { | ||
if v, ok := extra[errorKey]; ok { | ||
if err, ok := v.(error); ok { | ||
log[errorKey] = slogcommon.FormatError(err) | ||
delete(extra, errorKey) | ||
break | ||
} | ||
} | ||
|
||
return k, v.Any() | ||
case slog.KindLogValuer: | ||
return k, v.Any() | ||
case slog.KindGroup: | ||
return k, attrsToValue(v.Group()) | ||
case slog.KindInt64: | ||
return k, v.Int64() | ||
case slog.KindUint64: | ||
return k, v.Uint64() | ||
case slog.KindFloat64: | ||
return k, v.Float64() | ||
case slog.KindString: | ||
return k, v.String() | ||
case slog.KindBool: | ||
return k, v.Bool() | ||
case slog.KindDuration: | ||
return k, v.Duration() | ||
case slog.KindTime: | ||
return k, v.Time().UTC() | ||
default: | ||
return k, anyValueToString(v) | ||
} | ||
} | ||
|
||
func anyValueToString(v slog.Value) string { | ||
if tm, ok := v.Any().(encoding.TextMarshaler); ok { | ||
data, err := tm.MarshalText() | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return string(data) | ||
} | ||
|
||
return fmt.Sprintf("%+v", v.Any()) | ||
} | ||
log[ContextKey] = extra | ||
|
||
func buildExceptions(err error) map[string]any { | ||
return map[string]any{ | ||
"kind": reflect.TypeOf(err).String(), | ||
"error": err.Error(), | ||
"stack": nil, // @TODO | ||
} | ||
return 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
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,4 @@ | ||
package sloggraylog | ||
|
||
const name = "samber/slog-graylog" | ||
const version = "VERSION" // replaced by .github/workflows/release.yml |