Skip to content

Conversation

sayboras
Copy link
Member

@sayboras sayboras commented Jun 9, 2025

Description

While the underlying issue didn't require to migrate to slog, taking this change to carry on with migration as:

  • logrus is under maintenance mode
  • slog is part of standard library
  • cilium/cilium upstream migration is almost done for 1.18 release.

Fixes: #3795

Changelog

logging: Migrate from logrus to slog

@sayboras sayboras added the release-note/minor This PR introduces a minor user-visible change label Jun 9, 2025
@sayboras sayboras force-pushed the pr/tammach/klog branch 4 times, most recently from 3a7c41a to bde5071 Compare June 9, 2025 14:54
@sayboras sayboras changed the title slog: Migrate from logrus to slog logging: Migrate from logrus to slog Jun 10, 2025
@sayboras sayboras force-pushed the pr/tammach/klog branch 2 times, most recently from 794dcf8 to 0f618d6 Compare June 11, 2025 04:24
@sayboras
Copy link
Member Author

/test

@sayboras sayboras force-pushed the pr/tammach/klog branch 7 times, most recently from 9dc0e94 to a211e5e Compare June 13, 2025 11:25
@sayboras sayboras marked this pull request as ready for review June 13, 2025 11:25
@sayboras sayboras requested a review from a team as a code owner June 13, 2025 11:25
@sayboras sayboras requested a review from kevsecurity June 13, 2025 11:25
@sayboras sayboras force-pushed the pr/tammach/klog branch 3 times, most recently from 6f1d67f to fa63584 Compare June 14, 2025 12:00
Copy link
Member

@mtardy mtardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, that's a massive migration. I wanted to do something similar but never had the motivation!

I think something we could do next (of course in another PR) is to grep for log.*(fmt.Sprintf and see if we can replace those with proper structured logging.

type FieldLogger interface {
Handler() slog.Handler
With(args ...any) *slog.Logger
WithGroup(name string) *slog.Logger
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering, why not redefine the WithError to ease the change? I guess there was already a debate on this when they created slog :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FieldLogger is implemented by slog. Yes, slog didn't offer WithError function sadly.

Comment on lines -54 to +58
log.WithField(option.KeyConfigDir, configDir).WithError(err).Fatal("Failed to read config from directory")
logger.Fatal(log, "Failed to read config from directory", option.KeyConfigDir, configDir, logfields.Error, err)
} else {
log.WithField(option.KeyConfigDir, configDir).Info("Loaded config from directory")
log.Info("Loaded config from directory", option.KeyConfigDir, configDir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does that work? I'm a little bit confused about the use of logger. and log.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.Fatal is syntactic sugar for the below, as slog didn't have Fatal method.

logger.Error(msg, args...)
os.Exit(-1)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'm also confused. When would we use log. and when would we use logger.? i.e. why isn't it logger.Info() on line 58?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, we don't have log.{Fatal,Panic,Trace} in slog, so that is why we need to use logger.{Fatal,Panic,Trace} utility functions.

Ideally, there will be not much change related to developer experience, as log.{Error,Warn,Info,Debug} are more common.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok! For some reason I thought you were writing your own interface and implementing it with slog under the hood from #3814 (comment)

Copy link
Contributor

@kevsecurity kevsecurity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:rubber-stamp:

@sayboras sayboras merged commit 09226dd into main Jun 17, 2025
50 of 51 checks passed
@sayboras sayboras deleted the pr/tammach/klog branch June 17, 2025 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/minor This PR introduces a minor user-visible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

configure klog
3 participants