mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: fix slog to always use array of Fields (#21426)
Upgrades to slog v3 which includes a small, but backward incompatible API change to the acceptible call arguments when logging. This change allows us to verify via compile time type checking that arguments are correct and won't cause a panic, as was possible in slog v1, which this replaces (v2 was tagged but never used in coder/coder). It also updates dependencies that also use slog and were updated. I've left the `aibridge` dependency as a commit SHA, under the assumption that the team there (cc @pawbana @dannykopping ) will tag and update the dependency soon and on their own schedule. Other dependencies, I pushed new tags.
This commit is contained in:
@@ -12,8 +12,8 @@ import (
|
||||
"golang.org/x/tools/go/packages"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"cdr.dev/slog/sloggers/sloghuman"
|
||||
"cdr.dev/slog/v3"
|
||||
"cdr.dev/slog/v3/sloggers/sloghuman"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
+15
-10
@@ -19,8 +19,9 @@ import (
|
||||
"golang.org/x/mod/semver"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"cdr.dev/slog/sloggers/sloghuman"
|
||||
"cdr.dev/slog/v3"
|
||||
"cdr.dev/slog/v3/sloggers/sloghuman"
|
||||
|
||||
"github.com/coder/coder/v2/cli/cliui"
|
||||
"github.com/coder/serpent"
|
||||
)
|
||||
@@ -129,7 +130,7 @@ func main() {
|
||||
if errors.Is(err, cliui.ErrCanceled) {
|
||||
os.Exit(1)
|
||||
}
|
||||
r.logger.Error(context.Background(), "release command failed", "err", err)
|
||||
r.logger.Error(context.Background(), "release command failed", slog.Error(err))
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -250,9 +251,9 @@ func (r *releaseCommand) promoteVersionToStable(ctx context.Context, inv *serpen
|
||||
if err != nil {
|
||||
return xerrors.Errorf("edit release failed: %w", err)
|
||||
}
|
||||
logger.Info(ctx, "selected version promoted to stable", "url", newStable.GetHTMLURL())
|
||||
logger.Info(ctx, "selected version promoted to stable", slog.F("url", newStable.GetHTMLURL()))
|
||||
} else {
|
||||
logger.Info(ctx, "dry-run: release not updated", "uncommitted_changes", cmp.Diff(newStable, updatedNewStable))
|
||||
logger.Info(ctx, "dry-run: release not updated", slog.F("uncommitted_changes", cmp.Diff(newStable, updatedNewStable)))
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -367,7 +368,8 @@ func (r *releaseCommand) autoversionFile(ctx context.Context, file, channel, ver
|
||||
matchChannel := matches[1]
|
||||
match := matches[2]
|
||||
|
||||
logger := logger.With(slog.F("line_number", i+1), slog.F("match_channel", matchChannel), slog.F("match", match))
|
||||
logger := logger.With(slog.F("line_number", i+1),
|
||||
slog.F("match_channel", matchChannel), slog.F("match", match))
|
||||
|
||||
logger.Debug(ctx, "autoversion pragma detected")
|
||||
|
||||
@@ -379,7 +381,7 @@ func (r *releaseCommand) autoversionFile(ctx context.Context, file, channel, ver
|
||||
logger.Info(ctx, "autoversion pragma found with channel match")
|
||||
|
||||
match = strings.Replace(match, "[version]", `(?P<version>[0-9]+\.[0-9]+\.[0-9]+)`, 1)
|
||||
logger.Debug(ctx, "compiling match regexp", "match", match)
|
||||
logger.Debug(ctx, "compiling match regexp", slog.F("match", match))
|
||||
matchRe, err = regexp.Compile(match)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("regexp compile failed: %w", err)
|
||||
@@ -391,12 +393,15 @@ func (r *releaseCommand) autoversionFile(ctx context.Context, file, channel, ver
|
||||
if match := matchRe.FindStringSubmatchIndex(line); match != nil {
|
||||
vg := matchRe.SubexpIndex("version")
|
||||
if vg == -1 {
|
||||
logger.Error(ctx, "version group not found in match", "num_subexp", matchRe.NumSubexp(), "subexp_names", matchRe.SubexpNames(), "match", match)
|
||||
logger.Error(ctx, "version group not found in match",
|
||||
slog.F("num_subexp", matchRe.NumSubexp()),
|
||||
slog.F("subexp_names", matchRe.SubexpNames()),
|
||||
slog.F("match", match))
|
||||
return xerrors.Errorf("bug: version group not found in match")
|
||||
}
|
||||
start := match[vg*2]
|
||||
end := match[vg*2+1]
|
||||
logger.Info(ctx, "updating version number", "line_number", i+1, "match_start", start, "match_end", end, "old_version", line[start:end])
|
||||
logger.Info(ctx, "updating version number", slog.F("line_number", i+1), slog.F("match_start", start), slog.F("match_end", end), slog.F("old_version", line[start:end]))
|
||||
lines[i] = line[:start] + version + line[end:]
|
||||
matchRe = nil
|
||||
break
|
||||
@@ -421,7 +426,7 @@ func (r *releaseCommand) autoversionFile(ctx context.Context, file, channel, ver
|
||||
}
|
||||
logger.Info(ctx, "file autoversioned")
|
||||
} else {
|
||||
logger.Info(ctx, "dry-run: file not updated", "uncommitted_changes", diff)
|
||||
logger.Info(ctx, "dry-run: file not updated", slog.F("uncommitted_changes", diff))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
+5
-5
@@ -393,7 +393,7 @@ func notImplementsFullResponseWriter(ctx *dsl.VarFilterContext) bool {
|
||||
// slogFieldNameSnakeCase is a lint rule that ensures naming consistency
|
||||
// of logged field names.
|
||||
func slogFieldNameSnakeCase(m dsl.Matcher) {
|
||||
m.Import("cdr.dev/slog")
|
||||
m.Import("cdr.dev/slog/v3")
|
||||
m.Match(
|
||||
`slog.F($name, $value)`,
|
||||
).
|
||||
@@ -404,7 +404,7 @@ func slogFieldNameSnakeCase(m dsl.Matcher) {
|
||||
// slogUUIDFieldNameHasIDSuffix ensures that "uuid.UUID" field has ID prefix
|
||||
// in the field name.
|
||||
func slogUUIDFieldNameHasIDSuffix(m dsl.Matcher) {
|
||||
m.Import("cdr.dev/slog")
|
||||
m.Import("cdr.dev/slog/v3")
|
||||
m.Import("github.com/google/uuid")
|
||||
m.Match(
|
||||
`slog.F($name, $value)`,
|
||||
@@ -416,7 +416,7 @@ func slogUUIDFieldNameHasIDSuffix(m dsl.Matcher) {
|
||||
// slogMessageFormat ensures that the log message starts with lowercase, and does not
|
||||
// end with special character.
|
||||
func slogMessageFormat(m dsl.Matcher) {
|
||||
m.Import("cdr.dev/slog")
|
||||
m.Import("cdr.dev/slog/v3")
|
||||
m.Match(
|
||||
`logger.Error($ctx, $message, $*args)`,
|
||||
`logger.Warn($ctx, $message, $*args)`,
|
||||
@@ -454,7 +454,7 @@ func slogMessageFormat(m dsl.Matcher) {
|
||||
|
||||
// slogMessageLength ensures that important log messages are meaningful, and must be at least 16 characters long.
|
||||
func slogMessageLength(m dsl.Matcher) {
|
||||
m.Import("cdr.dev/slog")
|
||||
m.Import("cdr.dev/slog/v3")
|
||||
m.Match(
|
||||
`logger.Error($ctx, $message, $*args)`,
|
||||
`logger.Warn($ctx, $message, $*args)`,
|
||||
@@ -484,7 +484,7 @@ func slogMessageLength(m dsl.Matcher) {
|
||||
|
||||
// slogErr ensures that errors are logged with "slog.Error" instead of "slog.F"
|
||||
func slogError(m dsl.Matcher) {
|
||||
m.Import("cdr.dev/slog")
|
||||
m.Import("cdr.dev/slog/v3")
|
||||
m.Match(
|
||||
`slog.F($name, $value)`,
|
||||
).
|
||||
|
||||
@@ -14,8 +14,9 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"cdr.dev/slog/sloggers/sloghuman"
|
||||
"cdr.dev/slog/v3"
|
||||
"cdr.dev/slog/v3/sloggers/sloghuman"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/coderdtest/oidctest"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user