All jobs

Add Structured Logging to an Echo API

Yes. An agent replaces Echo's text access log with JSON lines from slog through RequestLoggerWithConfig, adds a request id to each line and response, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads where logging happens today, because in an Echo app there are usually three loggers and only one of them is the access log. In an app like xesina/golang-echo-realworld-example-app, router/router.go builds the *echo.Echo, sets the framework logger to DEBUG through labstack/gommon/log, adds RemoveTrailingSlash, middleware.Logger() and a permissive CORS config; db/db.go opens SQLite through gorm v1.9.8 with LogMode(true), which prints every SQL statement in gorm's own format; and main.go ends with r.Logger.Fatal(r.Start("127.0.0.1:8585")). go.mod pins labstack/echo/v4 at v4.1.16 under go 1.18, and .github/workflows/go.yml builds and tests on Go 1.18. The handlers in handler/ answer errors themselves with c.JSON(http.StatusUnprocessableEntity, utils.NewError(err)) rather than returning an error up the chain.

The first decision is the Echo version. RequestLoggerWithConfig, the middleware that hands each request's values to a function you write, entered Echo in v4.6.0, so v4.1.16 does not have it. The agent runs go get github.com/labstack/echo/v4@v4.16.2, the latest v4 release, which raises the go directive to 1.26.0 as that release's own go.mod requires; with a Go 1.21 or newer toolchain installed, go fetches the 1.26 toolchain itself. It then runs go build -race . and go test -v -race ./... from the Makefile on the unchanged code to see what the bump alone breaks before it adds anything, and keeps dgrijalva/jwt-go, echo-swagger and the validator where they are.

The logging itself goes in router/router.go. middleware.Logger() is replaced by middleware.RequestID() followed by middleware.RequestLoggerWithConfig with LogMethod, LogURI, LogStatus, LogLatency, LogRemoteIP, LogRequestID and LogError on and HandleError: true, and a LogValuesFunc that writes one line through slog.New(slog.NewJSONHandler(os.Stdout, nil)) with those fields and a level of ERROR when v.Error is set. RequestID() also puts the id in the X-Request-ID response header, so a client can quote the line it caused. A Skipper drops /swagger/*, which main.go mounts, from the log. The framework's own logger stays for its startup and fatal messages, and gorm's LogMode(true) is left as it is with a note in the pull request, because turning off SQL echo is a separate choice and Makefile exports DEBUG=true for a reason.

The test is router/router_test.go: it builds the router with a bytes.Buffer as the handler's writer instead of os.Stdout, serves one request through httptest, decodes the single line with encoding/json and asserts the method, uri, status and a non-empty request_id key, with testify/assert as handler/user_test.go already uses. The constructor takes an io.Writer for that reason, defaulting to os.Stdout in main.go. The agent runs go vet ./..., make build and make test, starts the server and curls /api/tags and a bad route to capture two JSON lines, and changes go.yml to the Go version the new go.mod names.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Replace the text access log in this Echo API with structured JSON logging. Read go.mod, main.go, router/router.go, db/db.go, handler/handler.go, handler/routes.go, handler/user_test.go, utils/errors.go, Makefile and .github/workflows/go.yml first. Upgrade labstack/echo/v4 to the latest v4 release so RequestLoggerWithConfig is available, run the build and tests on the unchanged code to see what the bump alone changes, then replace middleware.Logger with RequestID plus RequestLoggerWithConfig writing one JSON line per request through log/slog with method, uri, status, latency, remote ip, request id and error, skipping the swagger routes. Let the writer be injected so a test can capture the line. Leave gorm's SQL logging as it is and say so. Add a router test that decodes one logged line and checks its keys. Update the CI workflow's Go version to match go.mod. Run go vet, make build and make test, start the server and curl two routes to show real log lines, then open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request that changes go.mod and go.sum for the Echo bump, router/router.go for the two middlewares and the slog handler, main.go to pass os.Stdout, .github/workflows/go.yml for the Go version, and adds router/router_test.go. The description shows two JSON lines from the running server, one 200 from /api/tags and one 404 with its error field, and the go test -v -race ./... output with the new test's name.

The run's record keeps the build and test run before and after the dependency bump, so a reviewer can tell a failure the upgrade caused from one the logging introduced, and keeps the curl calls that produced the sample lines.

Before merging, a reviewer decides whether to keep gorm's SQL echo on, whether latency should be logged in milliseconds or as a duration string, and whether the framework logger's DEBUG level in router.go should come down now that requests are logged elsewhere. The agent changes no handler and no route.

Run it on every Go service with a text access log

A team with several Echo services starts one run per repository from its own server: one POST /o/{org}/sessions each, with the repository in resources and a request_id that is a UUID v5 of the repository's full name, so a retried call returns the run already started. Each run reads its own go.mod, so a service already on an Echo release with RequestLoggerWithConfig skips the bump, and one on zerolog or zap gets its LogValuesFunc written against that logger instead of slog.

After that, an automation on the Project with a github trigger on pull_request.opened and pull_request.synchronize and the prompt "if this pull request adds a middleware or a route group, check it runs after RequestID and inside the request logger, and say so in a comment" keeps the log complete. The trigger adds the pull request after the prompt and fires only for owners, members and collaborators.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Structured logging written without running the service goes wrong in ways a diff hides: a logger middleware registered before RequestID so every line has an empty id, an Echo bump that compiles but changes how the error handler commits the response, or a LogError that never fires because the handlers answer errors themselves, as this app's do. The agent runs the real server in its sandbox, sends real requests and reads the real lines before it opens the pull request.

Every command stays with the run, from the dependency bump to the two curl calls, and the pull request cites them. Doing the same for a dozen Go services at once costs no more attention than one, because each run has its own machine, its own record and its own pull request.

Frequently asked questions

Why upgrade Echo rather than write a custom middleware on v4.1.16?

A hand-written middleware would have to measure latency, read the status after the handler and handle errors the way Echo's own does, and would diverge from it on the next bump. RequestLoggerWithConfig has been in Echo since v4.6.0 and is the supported way to feed any logger; staying on a release from before it means carrying that code yourself.

Why slog and not zerolog or zap?

log/slog is in the Go standard library, and the Echo bump already moves go.mod to a version that has it, so the change adds no dependency. A service that already imports zerolog or zap gets the same LogValuesFunc written against that logger instead; the middleware does not care which.

Why does the error field stay empty on most 4xx responses?

Because the handlers in this app build the error body themselves with utils.NewError and return nil, so nothing reaches the middleware's v.Error. The status is still logged. The pull request says so and leaves a reviewer the choice of returning echo.NewHTTPError from handlers later, which would fill the field without changing the log line.

What happens to gorm's SQL log?

Nothing in this pull request. db.New() calls LogMode(true) and the Makefile exports DEBUG=true, so SQL statements keep printing in gorm's format between the JSON lines. The pull request names the line to change if the team wants SQL echo off outside development.

More Echo jobs

The same job on other stacks

Keep reading