Skip to content

Log Module

ci go report codecov Deps PkgGoDev

Overview

Yokai provides a fxlog module, allowing your application to produce logs.

It wraps the log module, based on Zerolog.

Installation

The fxlog module is automatically loaded by Yokai's core.

When you use a Yokai application template, you have nothing to install, it's ready to use.

Configuration

This module provides the possibility to configure:

  • the log level (possible values: trace, debug, info, warning, error, fatal, panic, no-level or disabled)
  • the log output (possible values: noop, stdout or test)

Regarding the output:

  • stdout: to send the log records to os.Stdout (default)
  • noop: to void the log records via os.Discard
  • console: pretty prints logs record to os.Stdout
  • test: to send the log records to the TestLogBuffer made available in the Fx container, for further assertions
configs/config.yaml
modules:
  log:
    level: info    # by default
    output: stdout # by default

Usage

This module makes available the Logger in Yokai dependency injection system.

It is built on top of Zerolog, see its documentation for more details about available methods.

You can inject the logger where needed, but it's recommended to use the one carried by the context.Context when possible (for automatic logs correlation).

Testing

This module provides the possibility to easily test your application logs, using the TestLogBuffer with modules.log.output=test.

configs/config.test.yaml
modules:
  log:
    output: test # to send logs to test buffer

You can use the provided test assertion helpers in your tests:

  • AssertHasLogRecord: to assert on exact attributes match
  • AssertHasNotLogRecord: to assert on exact attributes non match
  • AssertContainLogRecord: to assert on partial attributes match
  • AssertContainNotLogRecord: to assert on partial attributes non match

and use Dump() to print the current content of the TestLogBuffer.

For example:

internal/example_test.go
package internal_test

import (
    "testing"

    "github.com/ankorstore/yokai/log"
    "github.com/ankorstore/yokai/log/logtest"
    "github.com/foo/bar/internal"
    "go.uber.org/fx"
)

func TestExample(t *testing.T) {
    var logBuffer logtest.TestLogBuffer

    internal.RunTest(
        t,
        fx.Populate(&logBuffer),
        fx.Invoke(func(logger *log.Logger) {
            logger.Debug().Msg("test message")
        }),
    )

    // print logs records
    logBuffer.Dump()

    // log assertion example
    logtest.AssertHasLogRecord(
        t,
        logBuffer,
        map[string]interface{}{
            "level":   "debug",
            "message": "test message",
        }, 
    )
}