-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add logger middleware config migrator for Output -> Stream rename #269
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
Merged
ReneWerner87
merged 2 commits into
master
from
claude/logger-middleware-migrator-v3-mVvvD
Feb 7, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package v3 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
|
|
||
| semver "github.com/Masterminds/semver/v3" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/gofiber/cli/cmd/internal" | ||
| ) | ||
|
|
||
| var ( | ||
| reLoggerImport = regexp.MustCompile(`(?m)^\s*(?:import\s+)?(?:([\w.]+)\s+)?"github\.com/gofiber/fiber/(?:v2|v3)/middleware/logger"`) | ||
| reLoggerOutputField = regexp.MustCompile(`((?:^|[{\n])\s*)Output(\s*:)`) | ||
| ) | ||
|
|
||
| func MigrateLoggerConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version) error { | ||
| changed, err := internal.ChangeFileContent(cwd, func(content string) string { | ||
| aliases := collectAliases(content, reLoggerImport, []string{"logger"}) | ||
| if len(aliases) == 0 { | ||
| return content | ||
| } | ||
|
|
||
| for _, alias := range aliases { | ||
| reConfig := regexp.MustCompile(regexp.QuoteMeta(alias) + `\.Config\s*\{`) | ||
| content = IterateConfigBlocks(content, reConfig, func(s string) string { | ||
| return reLoggerOutputField.ReplaceAllString(s, "${1}Stream${2}") | ||
| }) | ||
| } | ||
|
|
||
| return content | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to migrate logger configs: %w", err) | ||
| } | ||
| if !changed { | ||
| return nil | ||
| } | ||
|
|
||
| cmd.Println("Migrating logger middleware configs") | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package v3_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/gofiber/cli/cmd/internal/migrations/v3" | ||
| ) | ||
|
|
||
| func Test_MigrateLoggerConfig(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mlogger") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import ( | ||
| "os" | ||
| "github.com/gofiber/fiber/v2/middleware/logger" | ||
| ) | ||
| var _ = logger.New(logger.Config{ | ||
| Output: os.Stdout, | ||
| })`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateLoggerConfig(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "Stream:") | ||
| assert.NotContains(t, content, "Output:") | ||
| assert.Contains(t, buf.String(), "Migrating logger middleware configs") | ||
| } | ||
ReneWerner87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func Test_MigrateLoggerConfig_NoChange(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mlogger_noop") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| writeTempFile(t, dir, `package main | ||
| import "fmt" | ||
| func main() { fmt.Println("hello") } | ||
| `) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateLoggerConfig(cmd, dir, nil, nil)) | ||
|
|
||
| assert.Empty(t, buf.String()) | ||
| } | ||
|
|
||
| func Test_MigrateLoggerConfig_MultipleFields(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mlogger_multi") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import ( | ||
| "os" | ||
| "github.com/gofiber/fiber/v2/middleware/logger" | ||
| ) | ||
| var _ = logger.New(logger.Config{ | ||
| Format: "[${time}] ${status}\n", | ||
| Output: os.Stderr, | ||
| TimeFormat: "2006-01-02", | ||
| })`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateLoggerConfig(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "Stream:") | ||
| assert.NotContains(t, content, "Output:") | ||
| assert.Contains(t, content, "Format:") | ||
| assert.Contains(t, content, "TimeFormat:") | ||
| } | ||
|
|
||
| func Test_MigrateLoggerConfig_OutputInStringLiteral(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mlogger_str") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import ( | ||
| "os" | ||
| "github.com/gofiber/fiber/v2/middleware/logger" | ||
| ) | ||
| var _ = logger.New(logger.Config{ | ||
| Format: "Output: ${status}\n", | ||
| Output: os.Stdout, | ||
| })`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateLoggerConfig(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "Stream:") | ||
| assert.Contains(t, content, `"Output: ${status}\n"`, "string literal should not be modified") | ||
| assert.Contains(t, buf.String(), "Migrating logger middleware configs") | ||
| } | ||
|
|
||
| func Test_MigrateLoggerConfig_WhitespaceBeforeBrace(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mlogger_ws") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import ( | ||
| "os" | ||
| "github.com/gofiber/fiber/v2/middleware/logger" | ||
| ) | ||
| var _ = logger.New(logger.Config { | ||
| Output: os.Stdout, | ||
| })`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateLoggerConfig(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "Stream:") | ||
| assert.NotContains(t, content, "Output:") | ||
| } | ||
|
|
||
| func Test_MigrateLoggerConfig_Alias(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mlogger_alias") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import ( | ||
| "os" | ||
| fiberlog "github.com/gofiber/fiber/v2/middleware/logger" | ||
| ) | ||
| var _ = fiberlog.New(fiberlog.Config{ | ||
| Output: os.Stdout, | ||
| })`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateLoggerConfig(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "Stream:") | ||
| assert.NotContains(t, content, "Output:") | ||
| } | ||
|
|
||
| func Test_MigrateLoggerConfig_SingleLine(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| dir, err := os.MkdirTemp("", "mlogger_single") | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, os.RemoveAll(dir)) }() | ||
|
|
||
| file := writeTempFile(t, dir, `package main | ||
| import ( | ||
| "os" | ||
| "github.com/gofiber/fiber/v2/middleware/logger" | ||
| ) | ||
| var _ = logger.New(logger.Config{Output: os.Stdout})`) | ||
|
|
||
| var buf bytes.Buffer | ||
| cmd := newCmd(&buf) | ||
| require.NoError(t, v3.MigrateLoggerConfig(cmd, dir, nil, nil)) | ||
|
|
||
| content := readFile(t, file) | ||
| assert.Contains(t, content, "Stream:") | ||
| assert.NotContains(t, content, "Output:") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.