Skip to content

Commit

Permalink
feat(log): add LOG_CALLER configuration
Browse files Browse the repository at this point in the history
- `LOG_CALLER` environment variable
- `--log-caller` flag
  • Loading branch information
qdm12 committed Jan 29, 2024
1 parent 81a7223 commit 482d1ba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ ENV \
DOH_TIMEOUT=3s \
LISTENING_ADDRESS=":53" \
LOG_LEVEL=info \
LOG_CALLER=hidden \
MIDDLEWARE_LOG_ENABLED=off \
MIDDLEWARE_LOG_DIRECTORY=/var/log/dns/ \
MIDDLEWARE_LOG_REQUESTS=on \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ For example, the environment variable `UPSTREAM_TYPE` corresponds to the CLI fla
| `BLOCK_IPS` | | comma separated list of IPs to block from being returned to clients |
| `BLOCK_CIDRS` | | comma separated list of IP networks (CIDRs) to block from being returned to clients |
| `LOG_LEVEL` | `info` | `debug`, `info`, `warning` or `error` |
| `LOG_CALLER` | `hidden` | `hidden` or `short` |
| `MIDDLEWARE_LOG_ENABLED` | `off` | `on` or `off` |
| `MIDDLEWARE_LOG_DIRECTORY` | `/var/log/dns/` | Any valid file path |
| `MIDDLEWARE_LOG_REQUESTS` | `off` | `on` or `off` to log DNS requests to the file path specified |
Expand Down
6 changes: 1 addition & 5 deletions cmd/dns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, //nolint:cycl
return fmt.Errorf("invalid settings: %w", err)
}

logLevel, err := log.ParseLevel(settings.Log.Level)
if err != nil {
return fmt.Errorf("parsing log level: %w", err)
}
logger.Patch(log.SetLevel(logLevel))
logger.Patch(settings.Log.ToOptions()...)

logger.Info(settings.String())

Expand Down
28 changes: 27 additions & 1 deletion internal/config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,35 @@ import (

"github.com/qdm12/gosettings"
"github.com/qdm12/gosettings/reader"
"github.com/qdm12/gosettings/validate"
"github.com/qdm12/gotree"
"github.com/qdm12/log"
)

type Log struct {
Level string
Level string
Caller string
}

func (l Log) ToOptions() (options []log.Option) {
logLevel, err := log.ParseLevel(l.Level)
if err != nil {
panic(err) // Log should be validated before
}
options = append(options, log.SetLevel(logLevel))
switch l.Caller {
case "hidden":
options = append(options, log.SetCallerFile(false), log.SetCallerLine(false))
case "short":
options = append(options, log.SetCallerFile(true), log.SetCallerLine(true))
}

return options
}

func (l *Log) setDefaults() {
l.Level = gosettings.DefaultComparable(l.Level, "info")
l.Caller = gosettings.DefaultComparable(l.Caller, "hidden")
}

func (l *Log) validate() (err error) {
Expand All @@ -23,6 +42,11 @@ func (l *Log) validate() (err error) {
return fmt.Errorf("log level: %w", err)
}

err = validate.IsOneOf(l.Caller, "hidden", "short")
if err != nil {
return fmt.Errorf("log caller: %w", err)
}

return nil
}

Expand All @@ -33,9 +57,11 @@ func (l *Log) String() string {
func (l *Log) ToLinesNode() (node *gotree.Node) {
node = gotree.New("Logging:")
node.Appendf("Level: %s", l.Level)
node.Appendf("Caller: %s", l.Caller)
return node
}

func (l *Log) read(reader *reader.Reader) {
l.Level = reader.String("LOG_LEVEL")
l.Caller = reader.String("LOG_CALLER")
}

0 comments on commit 482d1ba

Please sign in to comment.