Skip to content

fix: replace logrus with slog #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ricochet
Copy link

The golang's blog article introducing slog calls out logrus directly as tooling it aims to replace with a solution in the standard language library.

This change replaces logrus with slog and adjusts some of the log outputs to use structured logs.

Closes: #780

@Copilot Copilot AI review requested due to automatic review settings July 29, 2025 00:06
@ricochet ricochet requested a review from a team as a code owner July 29, 2025 00:06
Copilot

This comment was marked as outdated.

@ricochet ricochet force-pushed the fix/logrus-to-slog branch from 25c669f to 8dab75f Compare July 29, 2025 00:13
@mattdholloway mattdholloway requested a review from Copilot July 30, 2025 11:40
Copy link
Contributor

@Copilot 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 replaces the logrus logging library with Go's standard library slog for structured logging. The change modernizes the logging approach by adopting the recommended standard library solution that aims to replace third-party logging libraries like logrus.

  • Replace logrus imports with standard library slog across the codebase
  • Convert logging calls to use structured logging with key-value pairs
  • Update logger initialization and configuration to use slog handlers

Reviewed Changes

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

File Description
go.mod Remove logrus dependency from module requirements
pkg/log/io.go Update IOLogger to use slog.Logger and convert log messages to structured format
pkg/log/io_test.go Update test setup to use slog with custom time removal function
internal/ghmcp/server.go Replace logrus with slog in server initialization and logging calls

stdLogger := log.New(logrusLogger.Writer(), "stdioserver", 0)
logger := slog.New(slogHandler)
logger.Info("starting server", "version", cfg.Version, "host", cfg.Host, "dynamicToolsets", cfg.DynamicToolsets, "readOnly", cfg.ReadOnly)
stdLogger := log.New(os.Stderr, stdioServerLogPrefix, 0)
Copy link
Preview

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

The stdLogger is created with os.Stderr as output, but when a log file is configured, the slog logger uses the file while stdLogger still writes to stderr. This creates inconsistent logging destinations. Consider using the same output destination for both loggers.

Note: See the diff below for a potential fix:

@@ -206,18 +206,21 @@
 	stdioServer := server.NewStdioServer(ghServer)
 
 	var slogHandler slog.Handler
+	var logOutput io.Writer
 	if cfg.LogFilePath != "" {
 		file, err := os.OpenFile(cfg.LogFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
 		if err != nil {
 			return fmt.Errorf("failed to open log file: %w", err)
 		}
+		logOutput = file
 		slogHandler = slog.NewTextHandler(file, &slog.HandlerOptions{Level: slog.LevelDebug})
 	} else {
+		logOutput = os.Stderr
 		slogHandler = slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo})
 	}
 	logger := slog.New(slogHandler)
 	logger.Info("starting server", "version", cfg.Version, "host", cfg.Host, "dynamicToolsets", cfg.DynamicToolsets, "readOnly", cfg.ReadOnly)
-	stdLogger := log.New(os.Stderr, stdioServerLogPrefix, 0)
+	stdLogger := log.New(logOutput, stdioServerLogPrefix, 0)
 	stdioServer.SetErrorLogger(stdLogger)
 
 	if cfg.ExportTranslations {

Copilot uses AI. Check for mistakes.

Copy link
Author

Choose a reason for hiding this comment

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

Please double-check the io.Writer to stdLogger as file logOutput is desired. I updated the commit to follow the GH recommendation

@mattdholloway
Copy link
Contributor

mattdholloway commented Jul 30, 2025

@ricochet Could you run script/licenses to update the licenses on this branch? Otherwise I'm a fan of this change, and reducing our dependencies.

- Adjust some logs to use structured outputs
- Set stdioserver log prefix as const
- Do not export test func removeTimeAttr

Signed-off-by: Bailey Hayes <behayes2@gmail.com>
@ricochet ricochet force-pushed the fix/logrus-to-slog branch from 8dab75f to 79f9af5 Compare July 30, 2025 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use slog for logging instead of logrus
2 participants