refactor: replace logrus with stdlib log/slog via pkg/log wrapper#878
refactor: replace logrus with stdlib log/slog via pkg/log wrapper#878AdeshDeshmukh wants to merge 1 commit into
Conversation
Signed-off-by: AdeshDeshmukh <adeshkd123@gmail.com>
There was a problem hiding this comment.
Pull request overview
Implements issue #720 by migrating the CLI codebase from github.com/sirupsen/logrus to Go stdlib log/slog, using a new pkg/log wrapper intended to preserve the existing logrus-like call sites and APIs.
Changes:
- Added
pkg/log/log.goimplementing a logrus-compatible logging wrapper on top oflog/slog. - Replaced logrus imports/usages across commands, views, API handlers, and utilities to use
pkg/log. - Removed
logrusfrom module dependencies (and updated module sums accordingly).
Reviewed changes
Copilot reviewed 96 out of 97 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/views/webhook/edit/view.go | Replace logrus import with pkg/log. |
| pkg/views/user/create/view.go | Replace logrus import with pkg/log. |
| pkg/views/scanner/update/view.go | Replace logrus import with pkg/log. |
| pkg/views/scanner/create/view.go | Replace logrus import with pkg/log. |
| pkg/views/scan-all/update/view.go | Replace logrus import with pkg/log. |
| pkg/views/robot/update/view.go | Replace logrus import with pkg/log. |
| pkg/views/robot/create/view.go | Replace logrus import with pkg/log. |
| pkg/views/replication/policies/create/view.go | Replace logrus import with pkg/log. |
| pkg/views/registry/update/view.go | Replace logrus import with pkg/log. |
| pkg/views/registry/create/view.go | Replace logrus import with pkg/log. |
| pkg/views/quota/update/view.go | Replace logrus import with pkg/log. |
| pkg/views/project/create/view.go | Replace logrus import with pkg/log. |
| pkg/views/project/config/update/view.go | Replace logrus import with pkg/log. |
| pkg/views/password/reset/view.go | Replace logrus import with pkg/log. |
| pkg/views/password/change/view.go | Replace logrus import with pkg/log. |
| pkg/views/login/create.go | Replace logrus import with pkg/log. |
| pkg/views/label/update/view.go | Replace logrus import with pkg/log. |
| pkg/views/label/create/view.go | Replace logrus import with pkg/log. |
| pkg/views/instance/create/view.go | Replace logrus import with pkg/log. |
| pkg/views/immutable/create/view.go | Replace logrus import with pkg/log. |
| pkg/views/cveallowlist/update/view.go | Replace logrus import with pkg/log. |
| pkg/views/confirmation.go | Replace logrus import with pkg/log. |
| pkg/views/artifact/tags/create/view.go | Replace logrus import with pkg/log. |
| pkg/utils/utils.go | Replace logrus import with pkg/log. |
| pkg/utils/encryption.go | Migrate log calls from logrus to pkg/log. |
| pkg/utils/config.go | Replace logrus import with pkg/log. |
| pkg/utils/client.go | Replace logrus import with pkg/log. |
| pkg/prompt/prompt.go | Replace logrus import with pkg/log. |
| pkg/log/log.go | Add slog-backed logrus-compatible wrapper implementation. |
| pkg/config/replication/policies.go | Replace logrus import with pkg/log. |
| pkg/api/webhook_handler.go | Replace logrus import with pkg/log. |
| pkg/api/user_handler.go | Replace logrus import with pkg/log. |
| pkg/api/scan_all_handler.go | Replace logrus import with pkg/log. |
| pkg/api/robot_handler.go | Replace logrus import with pkg/log. |
| pkg/api/repository_handler.go | Replace logrus import with pkg/log. |
| pkg/api/registry_handler.go | Replace logrus import with pkg/log. |
| pkg/api/project_handler.go | Replace logrus import with pkg/log. |
| pkg/api/project_config_handler.go | Replace logrus import with pkg/log. |
| pkg/api/ping_handler.go | Migrate log calls from logrus to pkg/log. |
| pkg/api/member_handler.go | Replace logrus import with pkg/log. |
| pkg/api/instance_handler.go | Replace logrus import with pkg/log. |
| pkg/api/immutable_handler.go | Replace logrus import with pkg/log. |
| pkg/api/cveallowlist_handler.go | Replace logrus import with pkg/log. |
| pkg/api/artifact_handler.go | Replace logrus import with pkg/log. |
| go.sum | Remove logrus checksums; update dependency sums. |
| go.mod | Remove direct logrus dependency; update golang.org/x/term version. |
| doc/man-docs/man_doc.go | Replace logrus import with pkg/log. |
| doc/doc.go | Replace logrus import with pkg/log. |
| doc/doc_test.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/vulnerability/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/user/password.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/user/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/user/elevate.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/user/elevate_test.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/user/delete.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/scanner/update.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/scanner/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/scanner/delete.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/scan_all/view_schedule.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/scan_all/update_schedule.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/scan_all/stop.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/scan_all/run.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/scan_all/metrics.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/robot/update.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/robot/refresh.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/robot/create.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/repository/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/replication/stop.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/replication/start.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/replication/policies/update.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/replication/policies/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/replication/policies/create.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/replication/executions/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/registry/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/quota/update.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/view.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/search.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/robot/refresh.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/robot/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/robot/delete.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/robot/create.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/project/member/delete.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/member/create.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/project/logs.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/delete.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/project/create.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/logs.go | Replace logrus import with pkg/log (audit log streaming uses WithTime). |
| cmd/harbor/root/login.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/labels/list.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/info.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/context/update.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/context/delete.go | Replace logrus import and calls with pkg/log. |
| cmd/harbor/root/cmd.go | Update root logging setup to use pkg/log (debug level vs discard). |
| cmd/harbor/root/artifact/view.go | Replace logrus import with pkg/log. |
| cmd/harbor/root/artifact/delete.go | Replace logrus import with pkg/log. |
| .dagger/publishimage.go | Minor formatting/syntax cleanup in function call. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // WithTime returns a Logger with the given time added as the "time" attribute. | ||
| func (l *Logger) WithTime(t time.Time) *Logger { | ||
| return l.WithField("time", t) | ||
| } |
| func (l *Logger) rebuildHandler() { | ||
| opts := &slog.HandlerOptions{Level: l.level} | ||
| if l.formatter != nil && l.formatter.TimestampFormat != "" { | ||
| opts.ReplaceAttr = func(groups []string, a slog.Attr) slog.Attr { | ||
| if a.Key == slog.TimeKey && len(groups) == 0 { | ||
| a.Value = slog.StringValue(time.Now().Format(l.formatter.TimestampFormat)) | ||
| } | ||
| return a | ||
| } | ||
| } | ||
| l.logger = slog.New(slog.NewTextHandler(l.Out, opts)) |
| func rebuildDefaultLogger() { | ||
| opts := &slog.HandlerOptions{Level: currentLevel} | ||
| defaultLogger = &Logger{ | ||
| logger: slog.New(slog.NewTextHandler(currentOutput, opts)), | ||
| Out: currentOutput, | ||
| level: currentLevel, | ||
| } | ||
| } | ||
|
|
||
| // SetLevel sets the minimum log level on the default logger. | ||
| func SetLevel(level Level) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| currentLevel = level | ||
| rebuildDefaultLogger() | ||
| } | ||
|
|
||
| // SetOutput sets the output destination on the default logger. | ||
| func SetOutput(w io.Writer) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| currentOutput = w | ||
| rebuildDefaultLogger() | ||
| } |
| // StandardLogger returns the default package-level logger. | ||
| func StandardLogger() *Logger { | ||
| return defaultLogger | ||
| } | ||
|
|
||
| // Out returns the underlying io.Writer. | ||
|
|
||
| // SetLevel sets the minimum log level on this Logger instance. | ||
| func (l *Logger) SetLevel(level Level) { |
| github.com/zalando/go-keyring v0.2.8 | ||
| go.yaml.in/yaml/v4 v4.0.0-rc.4 | ||
| golang.org/x/term v0.42.0 | ||
| golang.org/x/term v0.40.0 | ||
| gopkg.in/yaml.v2 v2.4.0 |
|
I kindly request you to look for any the opened discussion regarding your area of work, before getting stared. Because the migration from So for your notice, this has been a long standing issue for more than a couple of months, and with detailed discussion with the maintainers and community, it has been decided to start the migration, once both Hence, I appreciate your interest in contributing to Harbor CLI, but I would highly suggest you to consult with the maintainers before proceeding with any such huge changes to the codebase. Thank you. |
|
closing this as duplicate of #722 |
Summary
Implements #720 — systematically replaces
github.com/sirupsen/logruswith stdliblog/slog, wrapped in a thinpkg/logpackage for full backward compatibility.What changed
New file
pkg/log/log.go— Backward-compatiblelog/slogwrapper providing the full logrus API surface:Debug,Infof,Warn,Error,Fatal,Printf,PrintlnWithField,WithFields,WithTimeNew(),StandardLogger(),SetLevel(),SetOutput(),SetFormatter()Outfield + level constants for test compatModified files (96 files)
logrusimports replaced withpkg/loglogrus.X()→log.X()+ added aliascmd/harbor/root/cmd.go— Simplified logging config from 8 lines to 4 (removed TextFormatter boilerplate)go.mod/go.sum— Removedgitlite.zycloud.tk/sirupsen/logrus v1.9.4dependencyVerification
go build ./...— passesgo test ./...— 183/183 pass (zero regressions)golangci-lint run ./...— zero issuesCloses #720