daemon: strongly type containerd log.OutputFormat

This type was introduced in
0a79e67e4f

Make use of it throughout our log-format handling code, and convert back
to a string before we pass it to the containerd client.

Signed-off-by: Bjorn Neergaard <bjorn.neergaard@docker.com>
This commit is contained in:
Bjorn Neergaard 2023-08-30 14:11:24 -06:00
parent 0d9da7367d
commit 0e80073e01
No known key found for this signature in database
4 changed files with 16 additions and 12 deletions

View file

@ -516,7 +516,7 @@ func loadDaemonCliConfig(opts *daemonOptions) (*config.Config, error) {
conf.Debug = opts.Debug
conf.Hosts = opts.Hosts
conf.LogLevel = opts.LogLevel
conf.LogFormat = opts.LogFormat
conf.LogFormat = log.OutputFormat(opts.LogFormat)
if flags.Changed(FlagTLS) {
conf.TLS = &opts.TLS
@ -914,7 +914,7 @@ func systemContainerdRunning(honorXDG bool) (string, bool, error) {
// configureDaemonLogs sets the logging level and formatting. It expects
// the passed configuration to already be validated, and ignores invalid options.
func configureDaemonLogs(conf *config.Config) {
switch log.OutputFormat(conf.LogFormat) {
switch conf.LogFormat {
case log.JSONFormat:
if err := log.SetFormat(log.JSONFormat); err != nil {
panic(err.Error())

View file

@ -164,7 +164,7 @@ func TestLoadDaemonCliConfigWithLogFormat(t *testing.T) {
loadedConfig, err := loadDaemonCliConfig(opts)
assert.NilError(t, err)
assert.Assert(t, loadedConfig != nil)
assert.Check(t, is.Equal(log.JSONFormat, log.OutputFormat(loadedConfig.LogFormat)))
assert.Check(t, is.Equal(log.JSONFormat, loadedConfig.LogFormat))
}
func TestLoadDaemonCliConfigWithInvalidLogFormat(t *testing.T) {

View file

@ -183,7 +183,7 @@ type CommonConfig struct {
Debug bool `json:"debug,omitempty"`
Hosts []string `json:"hosts,omitempty"`
LogLevel string `json:"log-level,omitempty"`
LogFormat string `json:"log-format,omitempty"`
LogFormat log.OutputFormat `json:"log-format,omitempty"`
TLS *bool `json:"tls,omitempty"`
TLSVerify *bool `json:"tlsverify,omitempty"`
@ -597,7 +597,7 @@ func Validate(config *Config) error {
// validate log-format
if logFormat := config.LogFormat; logFormat != "" {
switch log.OutputFormat(logFormat) {
switch logFormat {
case log.TextFormat, log.JSONFormat:
// These are valid
default:

View file

@ -1,5 +1,9 @@
package supervisor // import "github.com/docker/docker/libcontainerd/supervisor"
import (
"github.com/containerd/containerd/log"
)
// WithLogLevel defines which log level to start containerd with.
func WithLogLevel(lvl string) DaemonOpt {
return func(r *remote) error {
@ -15,9 +19,9 @@ func WithLogLevel(lvl string) DaemonOpt {
// WithLogFormat defines the containerd log format.
// This only makes sense if WithStartDaemon() was set to true.
func WithLogFormat(format string) DaemonOpt {
func WithLogFormat(format log.OutputFormat) DaemonOpt {
return func(r *remote) error {
r.Debug.Format = format
r.Debug.Format = string(format)
return nil
}
}