feat(diary): channels, components and config/log.yaml
- stderr and null drivers; per-component levels from log.components - SUMMER_LOG__ overrides parse as strings and overlay the defaults key by key - Validation names the key: driver, format, level, days, stack children and cycles, daily path outside the root - summer new writes config/log.yaml (stack of console and daily, river warn, http info) - docs/services/logging.md: channels, config/log.yaml, component loggers, daily files, redaction - Log keys in the configuration docs; ExampleFor backs the docs snippet
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -84,7 +85,9 @@ type ChannelConfig struct {
|
||||
|
||||
// DefaultConfig returns the framework defaults: a stack of a console channel
|
||||
// and a daily file at storage/logs/summer.log (JSON, level warn, kept 14
|
||||
// days), global level info, and River at warn.
|
||||
// days), global level info, and River at warn. A stderr channel (logfmt) and
|
||||
// a null channel are defined too, so log.default can name them without
|
||||
// defining them.
|
||||
func DefaultConfig() Config {
|
||||
return Config{
|
||||
Default: "stack",
|
||||
@@ -92,6 +95,8 @@ func DefaultConfig() Config {
|
||||
Channels: map[string]ChannelConfig{
|
||||
"stack": {Driver: DriverStack, Channels: []string{"console", "daily"}},
|
||||
"console": {Driver: DriverConsole, Format: FormatAuto},
|
||||
"stderr": {Driver: DriverStderr, Format: FormatLogfmt},
|
||||
"null": {Driver: DriverNull},
|
||||
"daily": {
|
||||
Driver: DriverDaily,
|
||||
Path: "storage/logs/summer.log",
|
||||
@@ -150,6 +155,23 @@ func ParseConfig(cfg *compass.Config) (Config, error) {
|
||||
out.Channels[name] = ch
|
||||
}
|
||||
}
|
||||
if raw, ok := cfg.Lookup(ComponentsKey); ok {
|
||||
components, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return Config{}, fmt.Errorf("diary: %s must be a map of component levels", ComponentsKey)
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(components)) {
|
||||
key := ComponentsKey + "." + name
|
||||
if _, nested := components[name].(map[string]any); nested {
|
||||
return Config{}, fmt.Errorf("diary: %s must be a level; component names cannot contain dots", key)
|
||||
}
|
||||
lvl, err := parseLevel(components[name])
|
||||
if err != nil {
|
||||
return Config{}, keyError(key, err)
|
||||
}
|
||||
out.Components[name] = lvl
|
||||
}
|
||||
}
|
||||
if err := out.validate(); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
@@ -204,11 +226,11 @@ func (c Config) validate() error {
|
||||
ch := c.Channels[name]
|
||||
key := ChannelsKey + "." + name
|
||||
switch ch.Driver {
|
||||
case DriverConsole, DriverDaily, DriverStack:
|
||||
case DriverConsole, DriverStderr, DriverDaily, DriverStack, DriverNull:
|
||||
case "":
|
||||
return fmt.Errorf("diary: %s.driver is required", key)
|
||||
default:
|
||||
return fmt.Errorf("diary: %s.driver %q is not one of console, daily, stack", key, ch.Driver)
|
||||
return fmt.Errorf("diary: %s.driver %q is not one of console, stderr, daily, stack, null", key, ch.Driver)
|
||||
}
|
||||
switch ch.Format {
|
||||
case "", FormatJSON, FormatLogfmt:
|
||||
@@ -219,8 +241,48 @@ func (c Config) validate() error {
|
||||
default:
|
||||
return fmt.Errorf("diary: %s.format %q is not one of auto, json, logfmt", key, ch.Format)
|
||||
}
|
||||
if ch.Driver == DriverDaily && ch.Path == "" {
|
||||
return fmt.Errorf("diary: %s.path is required for the daily driver", key)
|
||||
if ch.Driver == DriverDaily {
|
||||
if ch.Path == "" {
|
||||
return fmt.Errorf("diary: %s.path is required for the daily driver", key)
|
||||
}
|
||||
if !filepath.IsAbs(ch.Path) && !filepath.IsLocal(filepath.Clean(ch.Path)) {
|
||||
return fmt.Errorf("diary: %s.path %q leaves the application root; use a path inside it or an absolute path", key, ch.Path)
|
||||
}
|
||||
}
|
||||
if ch.Driver == DriverStack {
|
||||
if len(ch.Channels) == 0 {
|
||||
return fmt.Errorf("diary: %s.channels must list at least one channel", key)
|
||||
}
|
||||
for _, child := range ch.Channels {
|
||||
if _, ok := c.Channels[child]; !ok {
|
||||
return fmt.Errorf("diary: %s.channels names channel %q, which is not defined in %s", key, child, ChannelsKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, name := range slices.Sorted(maps.Keys(c.Channels)) {
|
||||
if c.Channels[name].Driver != DriverStack {
|
||||
continue
|
||||
}
|
||||
if err := c.checkCycle(name, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkCycle reports a stack that contains itself through its children.
|
||||
func (c Config) checkCycle(name string, path []string) error {
|
||||
if slices.Contains(path, name) {
|
||||
return fmt.Errorf("diary: %s.%s.channels forms a cycle (%s)", ChannelsKey, path[0], strings.Join(append(path, name), " -> "))
|
||||
}
|
||||
ch := c.Channels[name]
|
||||
if ch.Driver != DriverStack {
|
||||
return nil
|
||||
}
|
||||
for _, child := range ch.Channels {
|
||||
if err := c.checkCycle(child, append(slices.Clip(path), name)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user