Outdated documentation

You are reading documentation for v3, which is an outdated version. Click here to go to the latest documentation.

# Logging Since v2.8.0

# Introduction

Logging is an important part of all applications. The framework provides a standard and flexible way to log accesses, errors and regular information. This standard logging feature should be preferred in all applications and modules developed using Goyave for consistency across the framework's environment.

# Custom loggers

The framework provides three Go standard loggers (opens new window):

  • goyave.Logger: the logger for regular and miscellaneous information. Outputs to os.Stdout with log.LstdFlags by default.
  • goyave.AccessLogger: the logger used by the logging middleware (see below). Outputs to os.Stdout with no flags by default.
  • goyave.ErrLogger: the logger for errors and stacktraces. Outputs to os.Stderr with log.LstdFlags by default.

All these loggers can be modified or replaced entirely. Modifications to standard loggers should be done before calling goyave.Start().


func main() {
    // Replace de default logger
    goyave.Logger = log.New(os.Stdout, "myapp", log.Ldate | log.Ltime | log.Lshortfile)

    goyave.RegisterStartupHook(func() {
    if err := goyave.Start(registerRoutes); err != nil {

# Common and Combined access logs

To enable logging of accesses using the Common Log Format (opens new window), simply register the CommonLogMiddleware. Alternatively, you can use CombinedLogMiddleware for Combined Log Format.

import (

func registerRoutes(router *goyave.Router) {
    // Common log format

    // Combined log format

Each request the server receives will be logged using the goyave.AccessLogger logger.

# log.CommonLogMiddleware

CommonLogMiddleware captures response data and outputs it to the default logger using the common log format.

Parameters Return

# log.CombinedLogMiddleware

CombinedLogMiddleware captures response data and outputs it to the default logger using the combined log format.

Parameters Return

# Custom format

It is possible to implement custom formatters for access logs. A Formatter is a function with the following signature:

func(now time.Time, response *goyave.Response, request *goyave.Request, length int) string
  • now is the time at which the server received the request
  • length the length of the response body


func CustomFormatter(now time.Time, response *goyave.Response, request *goyave.Request, length int) string {
  return fmt.Sprintf("%s %s %s %s %d %d",

# log.Middleware

Middleware captures response data and outputs it to the default logger using the given formatter.

Parameters Return
formatter Formatter goyave.Middleware