Skip to content

refactor: replace logrus with stdlib log/slog via pkg/log wrapper#878

Closed
AdeshDeshmukh wants to merge 1 commit into
goharbor:mainfrom
AdeshDeshmukh:feat/refactor-logrus-to-slog
Closed

refactor: replace logrus with stdlib log/slog via pkg/log wrapper#878
AdeshDeshmukh wants to merge 1 commit into
goharbor:mainfrom
AdeshDeshmukh:feat/refactor-logrus-to-slog

Conversation

@AdeshDeshmukh
Copy link
Copy Markdown

Summary

Implements #720 — systematically replaces github.com/sirupsen/logrus with stdlib log/slog, wrapped in a thin pkg/log package for full backward compatibility.

What changed

New file

  • pkg/log/log.go — Backward-compatible log/slog wrapper providing the full logrus API surface:
    • Package-level: Debug, Infof, Warn, Error, Fatal, Printf, Println
    • Structured: WithField, WithFields, WithTime
    • Logger API: New(), StandardLogger(), SetLevel(), SetOutput(), SetFormatter()
    • Exported Out field + level constants for test compat

Modified files (96 files)

  • Import replacement — All 94 logrus imports replaced with pkg/log
  • Call-site migration — 13 unaliased files: logrus.X()log.X() + added alias
  • cmd/harbor/root/cmd.go — Simplified logging config from 8 lines to 4 (removed TextFormatter boilerplate)
  • go.mod / go.sum — Removed github.com/sirupsen/logrus v1.9.4 dependency

Verification

  • go build ./...passes
  • go test ./...183/183 pass (zero regressions)
  • golangci-lint run ./...zero issues

Closes #720

Signed-off-by: AdeshDeshmukh <adeshkd123@gmail.com>
Copilot AI review requested due to automatic review settings May 5, 2026 11:46
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.go implementing a logrus-compatible logging wrapper on top of log/slog.
  • Replaced logrus imports/usages across commands, views, API handlers, and utilities to use pkg/log.
  • Removed logrus from 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.

Comment thread pkg/log/log.go
Comment on lines +142 to +145
// 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)
}
Comment thread pkg/log/log.go
Comment on lines +129 to +139
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))
Comment thread pkg/log/log.go
Comment on lines +70 to +93
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()
}
Comment thread pkg/log/log.go
Comment on lines +104 to +112
// 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) {
Comment thread go.mod
Comment on lines 17 to 20
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
@vg006
Copy link
Copy Markdown
Contributor

vg006 commented May 5, 2026

@AdeshDeshmukh

I kindly request you to look for any the opened discussion regarding your area of work, before getting stared. Because the migration from logrus to log/slog is originated here #635, followed by #717 and #721. I would encourage you to check them.

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 errors package (in #636) and logger package (in #722), by maintainer @NucleoFusion, where he implemented a custom logger, wrapped over the std log/slog package, get merged.

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.

@qcserestipy qcserestipy added duplicate This issue or pull request already exists wontfix This will not be worked on labels May 5, 2026
@bupd
Copy link
Copy Markdown
Member

bupd commented May 6, 2026

closing this as duplicate of #722

@bupd bupd closed this May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists wontfix This will not be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(logs): Refactor logrus support to log/slog, with appropriate format

5 participants