Skip to content

Commit bdc662c

Browse files
authored
fix(cmd/librarian): log errors with error level (#2754)
Log level Error were lost in main when calling `log.Fatal` instead of `slog.Error`. Errors returned appear as `INFO` messages instead of `ERROR`. This was likely an oversight switching to slog as default logger. In internal/librarian/librarian.go, the `setupLogger` function calls [slog.SetDefault()](https://github.com/googleapis/librarian/blob/13bcf280d28167d2c23ca40b61abf5e27e62972a/internal/librarian/librarian.go#L40). This tells the slog package to become the default logger for the entire application. A side effect of this is that slog also intercepts any output from the standard log package with no level info. Also added a validation step in e2e test on failure case for generate, to verify error is reported with Error level. Test run below to verify now it correctly reports error: ``` ╰─$ go run ./cmd/librarian generate -push time=2025-11-03T12:56:21.109-05:00 level=INFO msg="Temporary working directory" dir=/tmp/librarian-4043056686 time=2025-11-03T12:56:21.110-05:00 level=INFO msg="repo not specified, using current working directory as repo root" path=/usr/local/google/home/zhumin/repos/librarian time=2025-11-03T12:56:21.110-05:00 level=ERROR msg="librarian command failed" err="failed to validate config: no GitHub token supplied for push" exit status 1 ``` Fix #2686
1 parent 13bcf28 commit bdc662c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

cmd/librarian/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package main
1616

1717
import (
1818
"context"
19-
"log"
19+
"log/slog"
2020
"os"
2121

2222
"github.com/googleapis/librarian/internal/librarian"
@@ -25,6 +25,7 @@ import (
2525
func main() {
2626
ctx := context.Background()
2727
if err := librarian.Run(ctx, os.Args[1:]...); err != nil {
28-
log.Fatal(err)
28+
slog.Error("librarian command failed", "err", err)
29+
os.Exit(1)
2930
}
3031
}

e2e_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package librarian
1818

1919
import (
20+
"bytes"
2021
"encoding/json"
2122
"fmt"
2223
"html"
@@ -135,14 +136,18 @@ func TestRunGenerate(t *testing.T) {
135136
cmd := exec.Command("go", cmdArgs...)
136137
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=fake-token", config.LibrarianGithubToken))
137138
cmd.Env = append(cmd.Env, "LIBRARIAN_GITHUB_BASE_URL="+server.URL)
138-
cmd.Stderr = os.Stderr
139+
var stderr bytes.Buffer
140+
cmd.Stderr = &stderr
139141
cmd.Stdout = os.Stdout
140142
err := cmd.Run()
141143
if test.wantErr {
142144
if err == nil {
143145
t.Fatalf("%s should fail", test.name)
144146
}
145147

148+
if g, w := stderr.String(), "level=ERROR"; !strings.Contains(g, w) {
149+
t.Errorf("got %q, wanted it to contain %q", stderr.String(), w)
150+
}
146151
// the exact message is not populated here, but we can check it's
147152
// indeed an error returned from docker container.
148153
if g, w := err.Error(), "exit status 1"; !strings.Contains(g, w) {

0 commit comments

Comments
 (0)