Skip to content

Commit 22540bb

Browse files
authored
Merge pull request #31 from open-edge-platform/loglevel-commandline-override-option
Fix --log-level flag not working
2 parents 80e6f33 + c0fc994 commit 22540bb

3 files changed

Lines changed: 45 additions & 18 deletions

File tree

cmd/image-composer/main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,21 @@ func main() {
3939
os.Exit(1)
4040
}
4141

42-
// Handle global log level override
43-
if logLevel != "" {
44-
globalConfig.Logging.Level = logLevel
45-
}
46-
4742
// Setup logger with configured level
4843
_, cleanup := utils.InitWithLevel(globalConfig.Logging.Level)
4944
defer cleanup()
5045

46+
// Create and execute root command
47+
rootCmd := createRootCommand()
48+
49+
// Handle log level override after flag parsing
50+
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
51+
if logLevel != "" {
52+
globalConfig.Logging.Level = logLevel
53+
utils.SetLogLevel(logLevel)
54+
}
55+
}
56+
5157
// Log configuration info
5258
logger := utils.Logger()
5359
if configFilePath != "" {
@@ -58,8 +64,6 @@ func main() {
5864
globalConfig.Workers, globalConfig.CacheDir, globalConfig.WorkDir, globalConfig.TempDir)
5965
}
6066

61-
// Create and execute root command
62-
rootCmd := createRootCommand()
6367
if err := rootCmd.Execute(); err != nil {
6468
os.Exit(1)
6569
}
@@ -78,7 +82,7 @@ from different Operating System Vendors (OSVs).`,
7882
// Add global flags
7983
rootCmd.PersistentFlags().StringVar(&configFile, "config", "",
8084
"Path to configuration file")
81-
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", globalConfig.Logging.Level,
85+
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "",
8286
"Log level (debug, info, warn, error)")
8387

8488
// Add all subcommands

internal/debutils/resolver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ func compareDebianVersions(a, b string) (int, error) {
354354
splitEpoch := func(ver string) (epoch int, rest string) {
355355
parts := strings.SplitN(ver, ":", 2)
356356
if len(parts) == 2 {
357-
fmt.Sscanf(parts[0], "%d", &epoch)
357+
if _, err := fmt.Sscanf(parts[0], "%d", &epoch); err != nil {
358+
epoch = 0
359+
}
358360
rest = parts[1]
359361
} else {
360362
epoch = 0

internal/utils/logger/logger.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func (n nopSyncer) Sync() error {
2525
var (
2626
sugarLogger *zap.SugaredLogger
2727
baseLogger *zap.Logger
28+
atomicLevel zap.AtomicLevel // This allows dynamic level changes
2829
once sync.Once
2930
)
3031

@@ -49,15 +50,18 @@ func initLoggerWithLevel(level string) {
4950
zapLevel = zapcore.InfoLevel // Default to info
5051
}
5152

53+
// Create atomic level for dynamic changes
54+
atomicLevel = zap.NewAtomicLevelAt(zapLevel)
55+
5256
cfg := zap.NewDevelopmentConfig()
53-
cfg.Level = zap.NewAtomicLevelAt(zapLevel)
57+
cfg.Level = atomicLevel
5458
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
5559
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
5660
cfg.EncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
5761

5862
encoder := zapcore.NewConsoleEncoder(cfg.EncoderConfig)
5963
writer := nopSyncer{os.Stderr}
60-
core := zapcore.NewCore(encoder, writer, cfg.Level)
64+
core := zapcore.NewCore(encoder, writer, atomicLevel)
6165

6266
opts := []zap.Option{
6367
zap.AddCaller(),
@@ -95,6 +99,11 @@ func InitWithLevel(level string) (*zap.SugaredLogger, func()) {
9599
initLoggerWithLevel(level)
96100
})
97101

102+
// If logger already exists, just change the level dynamically
103+
if atomicLevel.Enabled(zapcore.InfoLevel) { // Check if atomicLevel is initialized
104+
SetLogLevel(level)
105+
}
106+
98107
if baseLogger == nil {
99108
panic("logger initialization failed: baseLogger is nil")
100109
}
@@ -119,13 +128,25 @@ func With(args ...interface{}) *zap.SugaredLogger {
119128
return Logger().With(args...)
120129
}
121130

122-
// SetLogLevel dynamically changes the log level
131+
// SetLogLevel dynamically changes the log level without re-initializing the logger
123132
func SetLogLevel(level string) {
124-
if baseLogger != nil {
125-
// For now, we'll just log a message about the level change
126-
// Note: Dynamic level changing requires reconfiguring the logger core
127-
// which is more complex than this simple implementation
128-
sugarLogger.Infof("Log level change requested to: %s", level)
129-
sugarLogger.Infof("Note: Restart application to apply new log level")
133+
if atomicLevel == (zap.AtomicLevel{}) {
134+
return // Not initialized yet
135+
}
136+
137+
var zapLevel zapcore.Level
138+
switch strings.ToLower(level) {
139+
case "debug":
140+
zapLevel = zapcore.DebugLevel
141+
case "info":
142+
zapLevel = zapcore.InfoLevel
143+
case "warn", "warning":
144+
zapLevel = zapcore.WarnLevel
145+
case "error":
146+
zapLevel = zapcore.ErrorLevel
147+
default:
148+
zapLevel = zapcore.InfoLevel
130149
}
150+
151+
atomicLevel.SetLevel(zapLevel)
131152
}

0 commit comments

Comments
 (0)