Skip to main content

Logging with slog

In this lesson, you will learn how to add structured logging to your Mizu applications using Go’s built-in log/slog package. Logs help you see what your app is doing, measure how long things take, and understand when something goes wrong. Mizu already includes a built-in request logger, so every request is automatically logged in a human-readable format by default. You can customize the log output, format, and level using your own slog setup.

Code

Create a file named main.go and add this code:

Run

Start the server:
Then open these routes in your browser:
By default, Mizu logs all requests using a readable text format. If you run your app in a terminal, it will even use color output if your environment supports it.

How it works

Mizu automatically adds a request logger middleware when your app starts. It logs details such as:
  • HTTP method
  • Path
  • Status code
  • Duration
  • Any errors returned by handlers
When you set a custom slog.Logger using app.SetLogger(), all logs from Mizu and your handlers use that logger instance.

Try something new

You can switch to JSON output for production environments:
That will print structured logs like this:
You can also set LevelDebug to get more detailed logs during development. Logging gives you a clear picture of what your app is doing, helps you understand user behavior, and makes it easy to spot and fix problems. Continue to the next lesson: Request IDs and Correlation to learn how to attach unique IDs to each request for tracing across logs and services.