Skip to content

Audit package primer

The audit package is responsible for initialising and providing a configured zap.Logger to other packages.

Overview

During application startup, the logger configuration is loaded from config.GlobalConfig. When audit.Init() is called, an Auditor instance containing the configured zap.Logger is returned. This Auditor is then propagated through middleware to various handler functions.

The audit package is initialised in main.go as follows:

Go
1
2
3
4
5
auditor, err := audit.Init(config.GlobalConfig.InDevelopment)
if err != nil {
    log.Fatalf("Failed to initialize audit logger: %v", err)
}
log.Println("Audit logger initialized")

Propagation through middleware and handlers

The initialised Auditor is passed to the middleware.DefineRoutes function that sets up all application routes.

Go
router := middleware.DefineRoutes(auditor)

Handlers that require the configured zap.Logger are wrapped using WrapWithAuditor, providing each handler with the Auditor instance.

Go
router.HandleFunc("/users/{id}", CatchError(WrapWithAuditor(auditor, routes.UserPage))).Methods("GET")