Skip to content

feat: Adding slog support and handlers#722

Open
NucleoFusion wants to merge 12 commits intogoharbor:mainfrom
NucleoFusion:feat/slog-refactor
Open

feat: Adding slog support and handlers#722
NucleoFusion wants to merge 12 commits intogoharbor:mainfrom
NucleoFusion:feat/slog-refactor

Conversation

@NucleoFusion
Copy link
Copy Markdown
Contributor

@NucleoFusion NucleoFusion commented Mar 4, 2026

resolves #717

Adding support for log/slog with flag based checking on output format or verbosity.

image

Changes

  • Added Slog level based logging
    • By Default only WARN and ERROR levels will be shown.
    • On adding the --verbose flag shows INFO and DEBUG level as well.
  • Removed dependency on cobra on handling errors, now logged via slog
  • lipgloss based formatting for levels

EDIT

image

Changes

  • No logs show for non-verbose
  • Modified styles
  • Flag logging in PersistentPreRunE

Added better formatting and color codes

Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 4, 2026

Codecov Report

❌ Patch coverage is 2.02020% with 97 lines in your changes missing coverage. Please review.
✅ Project coverage is 7.99%. Comparing base (60ad0bd) to head (29c6189).
⚠️ Report is 131 commits behind head on main.

Files with missing lines Patch % Lines
pkg/logger/handler.go 0.00% 76 Missing ⚠️
pkg/logger/logger.go 0.00% 11 Missing ⚠️
cmd/harbor/root/cmd.go 16.66% 9 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             main    #722      +/-   ##
=========================================
- Coverage   10.99%   7.99%   -3.00%     
=========================================
  Files         173     272      +99     
  Lines        8671   13256    +4585     
=========================================
+ Hits          953    1060     +107     
- Misses       7612   12082    +4470     
- Partials      106     114       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
@NucleoFusion
Copy link
Copy Markdown
Contributor Author

@bupd Did the stuff we discussed in the meet
Its under the EDIT section in the PR description

Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
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

Adds a log/slog-based logging setup for the Harbor CLI, including a custom “pretty” handler with lipgloss-styled level rendering and initialization from the root command’s PersistentPreRunE.

Changes:

  • Introduces a new pkg/logger package with Setup() and a custom PrettyHandler for slog output.
  • Wires logger initialization into cmd/harbor/root/cmd.go and logs CLI flags at debug level.
  • Supports JSON logging when the selected format is "json".

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
cmd/harbor/root/cmd.go Initializes slog logging during CLI startup and emits a debug log of flags.
pkg/logger/logger.go Adds a centralized slog setup function selecting JSON vs pretty handler.
pkg/logger/handler.go Implements a custom pretty slog handler with colored level formatting and attribute printing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Nucleo Fusion <148791547+NucleoFusion@users.noreply.github.com>
@bupd
Copy link
Copy Markdown
Collaborator

bupd commented Mar 17, 2026

@NucleoFusion please address and mark reviews as resolved. I have also added my thoughts too.

hope it helps. thanks

Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
@NucleoFusion
Copy link
Copy Markdown
Contributor Author

@bupd changes have been made, you can take a look if you have time

@bupd bupd requested a review from Copilot April 7, 2026 14:58
Copy link
Copy Markdown
Collaborator

@bupd bupd left a comment

Choose a reason for hiding this comment

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

@NucleoFusion looks like lint is failing.

@bupd bupd added Changes Requesed feedback that must be addressed before merging. enhancement New feature or request labels Apr 7, 2026
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

This PR adds structured logging support to the Harbor CLI by migrating from logrus to Go's standard library log/slog. The changes introduce a custom pretty-print handler for text output, JSON handler support, and integrates logging setup into the root command initialization with a new --log-format flag to control output format.

Changes:

  • Created new pkg/logger package with Setup function to initialize slog with custom handlers and a PrettyHandler for formatted text output with colored log levels via lipgloss
  • Replaced logrus-based logging in cmd/harbor/root/cmd.go with slog-based setup, added --log-format flag with "text" and "json" options, and integrated flag logging in PersistentPreRunE
  • Removed dependencies on logrus, io, and time imports where no longer needed

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
pkg/logger/logger.go New file establishing slog Setup function with format-based handler selection
pkg/logger/handler.go New file implementing PrettyHandler for colored, formatted text logging output
cmd/harbor/root/cmd.go Updated to use new slog-based logging with dedicated --log-format flag and flag logging in PersistentPreRunE

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +29 to +33
if format == "json" {
handler = slog.NewJSONHandler(outp, &slog.HandlerOptions{Level: slog.LevelDebug})
} else {
// Custom Text Handler
handler = NewPrettyHandler(outp, slog.LevelDebug)
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

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

The --log-format flag does not validate that the format value is one of the supported options ("json" or "text"). Currently, any invalid value silently defaults to the text handler. Consider adding explicit validation in the Setup function to reject unsupported formats, or document this fallback behavior more clearly. According to the PR discussion, only "json" and "text" should be accepted values.

Suggested change
if format == "json" {
handler = slog.NewJSONHandler(outp, &slog.HandlerOptions{Level: slog.LevelDebug})
} else {
// Custom Text Handler
handler = NewPrettyHandler(outp, slog.LevelDebug)
switch format {
case "json":
handler = slog.NewJSONHandler(outp, &slog.HandlerOptions{Level: slog.LevelDebug})
case "text":
// Custom Text Handler
handler = NewPrettyHandler(outp, slog.LevelDebug)
default:
panic(`unsupported log format: ` + format + `; supported formats are "json" and "text"`)

Copilot uses AI. Check for mistakes.
Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
@NucleoFusion NucleoFusion force-pushed the feat/slog-refactor branch 2 times, most recently from 5ed9e8e to a929b09 Compare April 7, 2026 15:29
NucleoFusion and others added 2 commits April 7, 2026 21:00
Signed-off-by: NucleoFusion <lakshit.singh.mail@gmail.com>
@NucleoFusion
Copy link
Copy Markdown
Contributor Author

@bupd fixed lint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changes Requesed feedback that must be addressed before merging. enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discussion: Use of log/slog for structured loggin in favor of logrus or log

3 participants