-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: add more option-ful logging configuration
This moves the logging configuration from the single "level" key to top-level struct like all the new additions. This also does some internal shuffling of types and constants, taking inspiration from the `log/slog` package. Signed-off-by: Hank Donnay <[email protected]>
- Loading branch information
Showing
5 changed files
with
153 additions
and
97 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,99 @@ | ||
package config | ||
|
||
import ( | ||
"encoding" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Logging is all the log configuration. | ||
type Logging struct { | ||
Level LogLevel `yaml:"level,omitempty" json:"level,omitempty"` | ||
OmitTimestamps bool `yaml:"omit_timestamps,omitempty" json:"omit_timestamps,omitempty"` | ||
} | ||
|
||
// A LogLevel is a log level recognized by Clair. | ||
// | ||
// The zero value is [InfoLog]. | ||
type LogLevel int | ||
|
||
// The recognized log levels, with their string representations as the comments. | ||
// | ||
// NB [FatalLog] and [PanicLog] are not used in Clair or Claircore, and will | ||
// result in almost no logging. | ||
const ( | ||
TraceLog LogLevel = iota - 3 // trace | ||
DebugColorLog // debug-color | ||
DebugLog // debug | ||
InfoLog // info | ||
WarnLog // warn | ||
ErrorLog // error | ||
FatalLog // fatal | ||
PanicLog // panic | ||
) | ||
|
||
// Assert that the zero value is correct: | ||
var _ = [1]struct{}{{}}[InfoLog] | ||
|
||
// ParseLogLevel returns the log level for the given string. | ||
// | ||
// The passed string is case-insensitive. | ||
func ParseLogLevel(s string) (LogLevel, error) { | ||
const offset = int(TraceLog) | ||
for i, lim := 0, len(_LogLevel_index); i < lim; i++ { | ||
l := LogLevel(i + offset) | ||
if strings.EqualFold(s, l.String()) { | ||
return l, nil | ||
} | ||
} | ||
return LogLevel(-127), fmt.Errorf(`unknown log level %q`, s) | ||
} | ||
|
||
// UnmarshalText implements [encoding.TextUnmarshaler]. | ||
func (l *LogLevel) UnmarshalText(b []byte) (err error) { | ||
*l, err = ParseLogLevel(string(b)) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// MarshalText implements [encoding.TextMarshaler]. | ||
func (l *LogLevel) MarshalText() ([]byte, error) { | ||
const offset = int(TraceLog) | ||
if l == nil { | ||
return nil, errors.New("invalid LogLevel pointer: <nil>") | ||
} | ||
i := int(*l) - offset | ||
if i < 0 || i >= len(_LogLevel_index)-1 { | ||
return nil, fmt.Errorf("invalid LogLevel: %q", l.String()) | ||
} | ||
return []byte(_LogLevel_name[_LogLevel_index[i]:_LogLevel_index[i+1]]), nil | ||
} | ||
|
||
// Assert LogLevel implements everything that's needed. | ||
var ( | ||
_ encoding.TextUnmarshaler = (*LogLevel)(nil) | ||
_ encoding.TextMarshaler = (*LogLevel)(nil) | ||
) | ||
|
||
func (l *Logging) validate(mode Mode) ([]Warning, error) { | ||
return l.lint() | ||
} | ||
|
||
func (l *Logging) lint() (ws []Warning, _ error) { | ||
if l.Level > ErrorLog { | ||
ws = append(ws, Warning{ | ||
path: ".level", | ||
msg: `"fatal" and "panic" levels are not used and will result in almost no logging`, | ||
}) | ||
} | ||
if l.Level == DebugColorLog { | ||
ws = append(ws, Warning{ | ||
path: ".level", | ||
msg: `"debug-color" is deprecated; it will become an alias for "debug" in a future release`, | ||
}) | ||
} | ||
return ws, nil | ||
} |