Browse code

daemon/command: configureDaemonLogs: don't panic

Return an error instead of panicking.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/10/16 08:14:11
Showing 2 changed files
... ...
@@ -102,7 +102,9 @@ func newDaemonCLI(opts *daemonOptions) (*daemonCLI, error) {
102 102
 
103 103
 func (cli *daemonCLI) start(ctx context.Context) (err error) {
104 104
 	configureProxyEnv(ctx, cli.Config.Proxies)
105
-	configureDaemonLogs(cli.Config)
105
+	if err := configureDaemonLogs(ctx, cli.Config.DaemonLogConfig); err != nil {
106
+		return fmt.Errorf("failed to configure daemon logging: %w", err)
107
+	}
106 108
 
107 109
 	log.G(ctx).Info("Starting up")
108 110
 
... ...
@@ -965,15 +967,15 @@ func systemContainerdRunning(honorXDG bool) (string, bool, error) {
965 965
 
966 966
 // configureDaemonLogs sets the logging level and formatting. It expects
967 967
 // the passed configuration to already be validated, and ignores invalid options.
968
-func configureDaemonLogs(conf *config.Config) {
968
+func configureDaemonLogs(ctx context.Context, conf config.DaemonLogConfig) error {
969 969
 	switch conf.LogFormat {
970 970
 	case log.JSONFormat:
971 971
 		if err := log.SetFormat(log.JSONFormat); err != nil {
972
-			panic(err.Error())
972
+			return err
973 973
 		}
974 974
 	case log.TextFormat, "":
975 975
 		if err := log.SetFormat(log.TextFormat); err != nil {
976
-			panic(err.Error())
976
+			return err
977 977
 		}
978 978
 		if conf.RawLogs {
979 979
 			// FIXME(thaJeztah): this needs a better solution: containerd doesn't allow disabling colors, and this code is depending on internal knowledge of "log.SetFormat"
... ...
@@ -982,7 +984,7 @@ func configureDaemonLogs(conf *config.Config) {
982 982
 			}
983 983
 		}
984 984
 	default:
985
-		panic("unsupported log format " + conf.LogFormat)
985
+		return fmt.Errorf("unknown log format: %s", conf.LogFormat)
986 986
 	}
987 987
 
988 988
 	logLevel := conf.LogLevel
... ...
@@ -990,8 +992,9 @@ func configureDaemonLogs(conf *config.Config) {
990 990
 		logLevel = "info"
991 991
 	}
992 992
 	if err := log.SetLevel(logLevel); err != nil {
993
-		log.G(context.TODO()).WithError(err).Warn("configure log level")
993
+		log.G(ctx).WithError(err).Warn("configure log level")
994 994
 	}
995
+	return nil
995 996
 }
996 997
 
997 998
 func configureProxyEnv(ctx context.Context, cfg config.Proxies) {
... ...
@@ -198,17 +198,18 @@ func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
198 198
 }
199 199
 
200 200
 func TestConfigureDaemonLogs(t *testing.T) {
201
-	conf := &config.Config{}
202
-	configureDaemonLogs(conf)
201
+	ctx := t.Context()
202
+	err := configureDaemonLogs(ctx, config.DaemonLogConfig{})
203
+	assert.NilError(t, err)
203 204
 	assert.Check(t, is.Equal(log.InfoLevel, log.GetLevel()))
204 205
 
205 206
 	// log level should not be changed when passing an invalid value
206
-	conf.LogLevel = "foobar"
207
-	configureDaemonLogs(conf)
207
+	err = configureDaemonLogs(ctx, config.DaemonLogConfig{LogLevel: "foobar"})
208
+	assert.NilError(t, err)
208 209
 	assert.Check(t, is.Equal(log.InfoLevel, log.GetLevel()))
209 210
 
210
-	conf.LogLevel = "warn"
211
-	configureDaemonLogs(conf)
211
+	err = configureDaemonLogs(ctx, config.DaemonLogConfig{LogLevel: "warn"})
212
+	assert.NilError(t, err)
212 213
 	assert.Check(t, is.Equal(log.WarnLevel, log.GetLevel()))
213 214
 }
214 215