# Add Structured JSON Logging to a Fiber App

Yes. An agent switches a Fiber v3 app's access log to one JSON line per request, moves its own log lines to log/slog, adds a test for the line shape and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-structured-logging/fiber/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts from what the app logs today. In a project like gofiber/boilerplate, `go.mod` names module `boilerplate` on Go 1.26 with `github.com/gofiber/fiber/v3` 3.6.1, and `app.go` builds the app with `recover.New()` and a bare `logger.New()`, so every request prints the middleware's default text line, `[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}`, coloured when stdout is a terminal. `database/database.go` announces itself with `fmt.Println("Connected with Database")`, and `app.Listen` failures go through `log.Fatal`. Two flags, `-port` and `-prod`, pick the address and turn on prefork, which forks one child process per CPU, each writing to the same stdout. There are no test files, `.github/workflows/test.yml` runs `go test ./... -v -race` on Go 1.26 and 1.27 on Linux and Windows runners, and `lint.yml` runs golangci-lint v2.14.0 with the `modernize` linter enabled in `.golangci.yml`.

Then it changes the access log without changing the middleware. Fiber's logger ships predefined formats, and `logger.JSONFormat` emits `time`, `ip`, `method`, `url`, `status` and `bytesSent` as one JSON object per line. That format carries no latency or error, which are the two fields people grep for, so the agent writes the app's own format string in the same JSON shape with `${latency}`, `${error}` and `${pid}` added, the last so lines from prefork children can be told apart, and sets `TimeFormat` to RFC 3339 with `TimeZone` `UTC` so the timestamp is sortable across processes. It keeps `Stream` at `os.Stdout`, the right place for a container the `Dockerfile` runs under `dumb-init`.

Application logs move to the standard library. The agent installs a `slog.JSONHandler` on stdout as the default logger in `main`, replaces the `fmt.Println` in `database.go` with `slog.Info("connected to database")`, and replaces `log.Fatal(app.Listen(...))` with an error log that names the address and `os.Exit(1)`, so a startup failure is a JSON line with the same keys as everything else. The recover middleware and the `NotFound` handler are left as they are. A log level flag is not added unless the prompt asks, because `slog`'s handler takes one in a single option and the pull request shows where.

Finally it proves the line shape rather than describing it. A new `app_test.go` builds the same middleware with `Stream` pointed at a `bytes.Buffer`, sends a request through `app.Test`, and unmarshals the captured line into a map to assert that `status`, `method`, `url`, `latency` and `pid` are present and that `status` is a number, not a string. That test is the first one in the module, so `test.yml` now runs something. The agent runs `go build ./...`, `go vet ./...`, `gofmt -l .`, the test with `-race`, and golangci-lint at the version `lint.yml` pins, then starts the binary and requests `/api/v1/users` once to paste a real line into the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add structured JSON logging to this Fiber v3 application. Read go.mod, app.go, handlers/handlers.go, database/database.go, Dockerfile, Makefile, .golangci.yml and .github/workflows/test.yml first. Configure the existing logger middleware with a JSON format string in the shape of logger.JSONFormat plus ${latency}, ${error} and ${pid}, with TimeFormat set to RFC 3339 and TimeZone UTC, keeping stdout as the stream. Set a log/slog JSON handler as the default logger in main, replace the fmt.Println in database.go and the log.Fatal around app.Listen with slog calls, and keep the recover middleware and the NotFound handler as they are. Write app_test.go that captures one access-log line in a bytes.Buffer through app.Test and asserts the JSON keys and types. Run go build ./..., go vet ./..., gofmt -l, go test ./... -race and golangci-lint at the version lint.yml pins, start the binary once and request /api/v1/users to capture a real line, and open a pull request that shows that line and explains the pid field.",
  "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 touching `app.go`, `database/database.go` and a new `app_test.go`, with `go.mod` and `go.sum` unchanged because `log/slog` is standard library and the logger middleware was already a dependency. The description shows one access-log line and one application line as the binary printed them, lists the keys each carries, and explains why `pid` is there and why the timestamp is UTC.

The run's record keeps the build, vet, gofmt and golangci-lint output, the race-enabled test run, and the request the agent made against the running binary, so a reviewer sees the line was produced by the code in the branch and not typed into the description.

Before merging, a reviewer decides whether the Makefile's `up-silent` targets should pass a log level, whether the README's `docker run` example should mention that output is now JSON, and whether their log collector expects `@timestamp` instead of `time`, in which case `logger.ECSFormat` is the one-line switch.

## Run it on every new service from the boilerplate

A team that stamps services out of this boilerplate can run this once per new repository from its own server: one `POST /o/{org}/sessions` per repository, each with that 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 rather than a second pull request.

An automation on the Project with a `github` trigger on `pull_request.opened` and the prompt "flag any fmt.Println or log.Printf this pull request adds and rewrite it as a slog call with the same fields" keeps later changes on the same footing. The trigger puts the pull request after the prompt; only pushes by an owner, member or collaborator fire it, never a fork's.

## Why run it on Rush?

Logging changes are judged by what the lines look like under real traffic, and a laptop terminal shows coloured text that a container never prints. The agent runs the binary in a sandbox where stdout is not a terminal, so the lines it pastes are the ones a collector will ingest, and it runs the module's own vet, lint and race-enabled test commands at the versions the workflows pin.

Every command and its output stays with the run, so the pull request can show a captured line, the passing test and the lint result rather than asking a reviewer to pull the branch and start the server to see what changed.

## Frequently asked questions

### Why not just set Format to logger.JSONFormat?

You can, and it is one line. But `JSONFormat` carries `time`, `ip`, `method`, `url`, `status` and `bytesSent` only. Latency and the handler error are the two fields most people search a log for, so the agent writes a format string in the same shape with `${latency}`, `${error}` and `${pid}` added.

### Does Fiber's own log package switch to slog too?

Not by default. Fiber's `log` package takes a custom logger through `SetLogger`, but that means implementing its full interface, and the boilerplate never calls it. The agent leaves it alone and says so; if your handlers use `fiberlog`, ask for an adapter in the prompt.

### What happens with prefork on?

With `-prod`, Fiber forks one child per CPU and each writes to the same stdout, so lines from different processes interleave. The `${pid}` tag in the access log and the process id in the slog handler's output tell them apart, and the UTC RFC 3339 timestamp lets a collector sort them.

### Will the lint workflow still pass?

The agent runs golangci-lint at v2.14.0, the version `lint.yml` pins, with the `modernize` linter from `.golangci.yml`, before opening the pull request. `log/slog` is standard library, so the change adds no dependency for the linter to inspect, and the test file is excluded by the `--tests=false` argument the workflow passes.

## More Fiber jobs

- [Add WebSockets to a Fiber App](https://getrush.ai/run/add-websockets/fiber/)
- [Review Pull Requests on a Fiber v3 App](https://getrush.ai/run/review-pull-requests/fiber/)
- [Write Handler Tests for a Fiber Auth API](https://getrush.ai/run/write-handler-tests/fiber/)

## The same job on other stacks

- [Add Structured Logging to a Flask App](https://getrush.ai/run/add-structured-logging/flask/)
- [Add Structured Logging to a Gin API from a Linear Issue](https://getrush.ai/run/add-structured-logging/gin/)
- [Add Structured Logging to a Hono Worker](https://getrush.ai/run/add-structured-logging/hono/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
