fxlog

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 9 Imported by: 1

README

Fx Log Module

ci go report codecov Deps PkgGoDev

Fx module for log.

Installation

go get github.com/ankorstore/yokai/fxlog

Documentation

Dependencies

This module is intended to be used alongside the fxconfig module.

Loading

To load the module in your Fx application:

package main

import (
	"github.com/ankorstore/yokai/config"
	"github.com/ankorstore/yokai/fxconfig"
	"github.com/ankorstore/yokai/fxlog"
	"github.com/ankorstore/yokai/log"
	"go.uber.org/fx"
)

func main() {
	fx.New(
		fxconfig.FxConfigModule, // load the module dependency
		fxlog.FxLogModule,       // load the module
		fx.Invoke(func(logger *log.Logger) { // invoke the logger
			logger.Info().Msg("message")
		}),
	).Run()
}

If needed, you can also configure Fx to use this logger for its own event logs:

package main

import (
	"github.com/ankorstore/yokai/config"
	"github.com/ankorstore/yokai/fxconfig"
	"github.com/ankorstore/yokai/fxlog"
	"go.uber.org/fx"
)

func main() {
	fx.New(
		fxconfig.FxConfigModule,               // load the module dependency
		fxlog.FxLogModule,                     // load the module
		fx.WithLogger(fxlog.NewFxEventLogger), // configure Fx event logging
	).Run()
}
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
app:
  name: app
  env: dev
  version: 0.1.0
  debug: false
modules:
  log:
    level: info    # by default
    output: stdout # by default

Notes:

  • the config app.name (or env var APP_NAME) will be used in each log record service field: {"service":"app"}
  • if the config app.debug=true (or env var APP_DEBUG=true), the debug level will be used, no matter given configuration
  • if the config app.env=test (or env var APP_ENV=test), the test output will be used, no matter given configuration
Override

By default, the log.Logger is created by the DefaultLoggerFactory.

If needed, you can provide your own factory and override the module:

package main

import (
	"github.com/ankorstore/yokai/fxconfig"
	"github.com/ankorstore/yokai/fxlog"
	"github.com/ankorstore/yokai/log"
	"go.uber.org/fx"
)

type CustomLoggerFactory struct{}

func NewCustomLoggerFactory() log.LoggerFactory {
	return &CustomLoggerFactory{}
}

func (f *CustomLoggerFactory) Create(options ...log.LoggerOption) (*log.Logger, error) {
	return &log.Logger{...}, nil
}

func main() {
	fx.New(
		fxconfig.FxConfigModule,             // load the module dependency
		fxlog.FxLogModule,                   // load the module
		fx.Decorate(NewCustomLoggerFactory), // override the module with a custom factory
		fx.Invoke(func(logger *log.Logger) { // invoke the custom logger
			logger.Info().Msg("custom message")
		}),
	).Run()
}
Testing

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

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

You can then test:

package main_test

import (
	"testing"

	"github.com/ankorstore/yokai/config"
	"github.com/ankorstore/yokai/fxconfig"
	"github.com/ankorstore/yokai/fxlog"
	"github.com/ankorstore/yokai/log"
	"github.com/ankorstore/yokai/log/logtest"
	"go.uber.org/fx"
	"go.uber.org/fx/fxtest"
)

func TestLogger(t *testing.T) {
	t.Setenv("APP_NAME", "test")
	t.Setenv("APP_ENV", "test")

	var buffer logtest.TestLogBuffer

	fxtest.New(
		t,
		fx.NopLogger,
		fxconfig.FxConfigModule,
		fxlog.FxLogModule,
		fx.Invoke(func(logger *log.Logger) {
			logger.Debug().Msg("test message")
		}),
		fx.Populate(&buffer), // extracts the TestLogBuffer from the Fx container
	).RequireStart().RequireStop()

	// assertion success
	logtest.AssertHasLogRecord(t, buffer, map[string]interface{}{
		"level":   "debug",
		"service": "test",
		"message": "test message",
	})
}

See the log module testing documentation for more details.

Documentation

Index

Constants

View Source
const ModuleName = "log"

ModuleName is the module name.

Variables

FxLogModule is the Fx log module.

Functions

func NewFxEventLogger

func NewFxEventLogger(logger *log.Logger) fxevent.Logger

NewFxEventLogger returns a new NewFxEventLogger from a provided log.Logger.

func NewFxLogger

func NewFxLogger(p FxLogParam) (*log.Logger, error)

NewFxLogger returns a log.Logger.

Types

type FxEventLogger

type FxEventLogger struct {
	// contains filtered or unexported fields
}

FxEventLogger is a logger compatible with Fx, decorating log.Logger.

func (*FxEventLogger) LogEvent

func (l *FxEventLogger) LogEvent(event fxevent.Event)

LogEvent logs a fxevent.Event.

type FxLogParam

type FxLogParam struct {
	fx.In
	Factory log.LoggerFactory
	Buffer  logtest.TestLogBuffer
	Config  *config.Config
}

FxLogParam allows injection of the required dependencies in NewFxLogger.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL