Skip to content

Commit

Permalink
feat(logger): fallback to slog levels if no levels are specified (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane authored Sep 23, 2023
1 parent c7883af commit 62eb5ff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .slog.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ attrs:
- err: error

# if present, a wrapper for slog.Logger is generated with a method for each level.
# TODO: if no levels are specified, the builtin slog levels are used.
# if no levels are specified, the builtin slog levels are used.
logger:
# the API style for methods.
# possible values: [any, attr]
Expand Down
55 changes: 33 additions & 22 deletions sloggen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ import (
var tmpl = template.Must(template.New("").Funcs(funcs).Parse(
`// Code generated by go-simpler.org/sloggen. DO NOT EDIT.
package {{.Pkg}}
package {{$.Pkg}}
{{range .Imports -}}
{{range $.Imports -}}
import "{{.}}"
{{end}}
{{range .Levels -}}
{{range $.Levels -}}
const Level{{title .Name}} = slog.Level({{.Severity}})
{{end}}
{{range .Consts -}}
{{range $.Consts -}}
const {{snakeToCamel .}} = "{{.}}"
{{end}}
{{range .Attrs -}}
{{range $.Attrs -}}
func {{snakeToCamel .Key}}(value {{.Type}}) slog.Attr { return slog.{{slogFunc .Type}}("{{.Key}}", value) }
{{end}}
{{if .HasCustomLevels}}
{{if $.HasCustomLevels}}
func ParseLevel(s string) (slog.Level, error) {
switch strings.ToUpper(s) {
{{range .Levels -}}
{{range $.Levels -}}
case "{{upper .Name}}":
return Level{{title .Name}}, nil
{{end -}}
Expand All @@ -52,7 +52,7 @@ func ReplaceAttr(_ []string, attr slog.Attr) slog.Attr {
return attr
}
switch attr.Value.Any().(slog.Level) {
{{range .Levels -}}
{{range $.Levels -}}
case Level{{title .Name}}:
attr.Value = slog.StringValue("{{upper .Name}}")
{{end -}}
Expand All @@ -61,21 +61,21 @@ func ReplaceAttr(_ []string, attr slog.Attr) slog.Attr {
}
{{end}}
{{if .Logger}}
{{if $l := $.Logger}}
type Logger struct{ Logger *slog.Logger }
{{range .Levels}}
func (l *Logger) {{title .Name}}({{if $.Logger.Context}}ctx context.Context, {{end}}msg string, {{if $.Logger.AttrAPI}}attrs ...slog.Attr{{else}}args ...any{{end}}) {
l.log({{if $.Logger.Context}}ctx{{else}}context.Background(){{end}}, Level{{title .Name}}, msg, {{if $.Logger.AttrAPI}}attrs{{else}}args{{end}})
{{range $l.Levels}}
func (l *Logger) {{title .Name}}({{if $l.Context}}ctx context.Context, {{end}}msg string, {{if $l.AttrAPI}}attrs ...slog.Attr{{else}}args ...any{{end}}) {
l.log({{if $l.Context}}ctx{{else}}context.Background(){{end}}, {{if not $.HasCustomLevels}}slog.{{end}}Level{{title .Name}}, msg, {{if $l.AttrAPI}}attrs{{else}}args{{end}})
}
{{end}}
func (l *Logger) log(ctx context.Context, level slog.Level, msg string, {{if $.Logger.AttrAPI}}attrs []slog.Attr{{else}}args []any{{end}}) {
func (l *Logger) log(ctx context.Context, level slog.Level, msg string, {{if $l.AttrAPI}}attrs []slog.Attr{{else}}args []any{{end}}) {
if !l.Logger.Enabled(ctx, level) {
return
}
var pcs [1]uintptr
runtime.Callers(3, pcs[:])
r := slog.NewRecord(time.Now(), level, msg, pcs[0])
r.Add{{if $.Logger.AttrAPI}}Attrs(attrs...){{else}}(args...){{end}}
r.Add{{if $l.AttrAPI}}Attrs(attrs...){{else}}(args...){{end}}
_ = l.Logger.Handler().Handle(ctx, r)
}
{{end}}`,
Expand Down Expand Up @@ -121,6 +121,7 @@ type (
Type string
}
logger struct {
Levels []level
AttrAPI bool
Context bool
}
Expand Down Expand Up @@ -165,18 +166,34 @@ func readConfig(r io.Reader) (*config, error) {
cfg.HasCustomLevels = true
}
}
slices.SortFunc(cfg.Levels, func(l1, l2 level) int {
return cmp.Compare(l1.Severity, l2.Severity)
})

for i, m := range data.Attrs {
key, typ := getKV(m)
cfg.Attrs[i] = attr{key, typ}
}
slices.SortFunc(cfg.Attrs, func(a1, a2 attr) int {
return cmp.Compare(a1.Key, a2.Key)
})

if data.Logger != nil {
cfg.Logger = &logger{
Levels: cfg.Levels,
AttrAPI: false,
Context: data.Logger.Context,
}

if len(cfg.Levels) == 0 {
cfg.Logger.Levels = []level{
{"debug", int(slog.LevelDebug)},
{"info", int(slog.LevelInfo)},
{"warn", int(slog.LevelWarn)},
{"error", int(slog.LevelError)},
}
}

switch data.Logger.API {
case "any":
case "attr":
Expand All @@ -196,16 +213,10 @@ func readConfig(r io.Reader) (*config, error) {
cfg.Imports = append(cfg.Imports, "context", "runtime")
}

slices.Sort(cfg.Imports)
slices.Sort(cfg.Consts)
slices.SortFunc(cfg.Levels, func(l1, l2 level) int {
return cmp.Compare(l1.Severity, l2.Severity)
})
slices.SortFunc(cfg.Attrs, func(a1, a2 attr) int {
return cmp.Compare(a1.Key, a2.Key)
})

slices.Sort(cfg.Imports)
cfg.Imports = slices.Compact(cfg.Imports)

return &cfg, nil
}

Expand Down

0 comments on commit 62eb5ff

Please sign in to comment.