Testing Transport
The transports/testing package is the transport you want in test code: it captures every log entry into a mutex-safe in-memory library, exposing Messages, Data, and Metadata as typed fields on each captured LogLine.
For a usage walkthrough see the Mocking page in the logging API section. This page covers the package surface.
go get go.loglayer.dev/transports/testingimport lltest "go.loglayer.dev/transports/testing"(The package name is testing, which collides with the standard testing package, most users alias the import as lltest.)
Setup
lib := &lltest.TestLoggingLibrary{}
trans := lltest.New(lltest.Config{
BaseConfig: transport.BaseConfig{ID: "test"},
Library: lib,
})
log := loglayer.New(loglayer.Config{Transport: trans})If Library is nil, the transport allocates one and exposes it as trans.Library.
Config
type Config struct {
transport.BaseConfig
Library *TestLoggingLibrary // nil → auto-allocate
}LogLine Shape
type LogLine struct {
Level loglayer.LogLevel
Messages []any
Data loglayer.Data // assembled fields + error map; nil when neither were set
Metadata any // raw value passed to WithMetadata
}TestLoggingLibrary API
| Method | Purpose |
|---|---|
Lines() | Snapshot copy of all captured lines |
GetLastLine() | Most recent line (does not remove); nil if empty |
PopLine() | Most recent line (removes it); nil if empty |
ClearLines() | Drop all captured lines |
Len() | Number of captured lines |
Log(line) | Manually append a line (useful for adapter tests) |
All methods are safe for concurrent use.
Reaching the Library Back
The transport's GetLoggerInstance returns *TestLoggingLibrary:
lib := log.GetLoggerInstance("test").(*lltest.TestLoggingLibrary)
lib.ClearLines()Fatal Behavior
This transport writes fatal entries normally; whether the process actually exits is the core's decision via Config.DisableFatalExit (default: exit). See Fatal Exits the Process.
See Also
- Mocking, examples and patterns for using this transport in your test suite, plus
loglayer.NewMock()for the silent-mock case.
