Autostrada
Autostrada
Create a new codebase Get Autostrada Plus
Documentation Changelog Roadmap Give feedback
Login

DocumentationJSON API › Logging

Logging

The generated codebase supports leveled logging via the standard library log/slog package. For instructions on how to use log/slog, please see the official documentation for the package.

The logger is initialized in the main() function in cmd/api/main.go. It will be configured to output log lines in either structured plaintext, colorized plaintext, or JSON, depending on the option you choose when generating your codebase. For example:

# Structured plaintext
time=2026-05-20T10:59:00.755+02:00 level=INFO msg="starting server" server.addr=:2100

# Colorized plaintext
May 20 10:57:47.098 INF starting server server.addr=:2100

# JSON
{"time":"2026-05-20T11:03:33.832712699+02:00","level":"INFO","msg":"starting server","server":{"addr":":2100"}}

By default, the logger writes all log messages above Debug level to os.Stdout. You can change this if needed by editing the main() function.

The logger is available in handlers, helpers and middleware as a dependency in the application struct. For example, you can use it in a handler to log a warning message like so:

func (app *application) exampleHandler(w http.ResponseWriter, r *http.Request) {
    app.logger.Warn("slow request detected", "path", r.URL.Path, "duration_ms", 980)
}        

If you look inside the generated cmd/api/server.go file, you will also see that the http.Server for the application is configured to use the logger. Any messages emitted by http.Server will be recorded at the Warn level.