# README
Log Module
Logging module based on Zerolog.
Installation
go get github.com/ankorstore/yokai/log
Documentation
This module provides a Logger, offering all Zerolog methods.
Usage
To create a Logger
:
package main
import (
"os"
"github.com/ankorstore/yokai/log"
"github.com/rs/zerolog"
)
var logger, _ = log.NewDefaultLoggerFactory().Create()
// equivalent to:
var logger, _ = log.NewDefaultLoggerFactory().Create(
log.WithServiceName("default"), // adds {"service":"default"} to log records
log.WithLevel(zerolog.InfoLevel), // logs records with level >= info
log.WithOutputWriter(os.Stdout), // sends logs records to stdout
)
To use the Logger
:
package main
import (
"github.com/ankorstore/yokai/log"
)
func main() {
logger, _ := log.NewDefaultLoggerFactory().Create()
logger.Info().Msg("some message") // {"level:"info", "service":"default", "message":"some message"}
}
See Zerolog documentation for more details about available methods.
Context
This module provides the log.CtxLogger()
function that allow to extract the logger from a context.Context
.
If no logger is found in context, a default Zerolog based logger will be used.
Testing
This module provides a TestLogBuffer, recording log records to be able to assert on them after logging:
package main
import (
"fmt"
"github.com/ankorstore/yokai/log"
"github.com/ankorstore/yokai/log/logtest"
)
func main() {
buffer := logtest.NewDefaultTestLogBuffer()
logger, _ := log.NewDefaultLoggerFactory().Create(log.WithOutputWriter(buffer))
logger.Info().Msg("some message example")
// test on attributes exact matching
hasRecord, _ := buffer.HasRecord(map[string]interface{}{
"level": "info",
"message": "some message example",
})
fmt.Printf("has record: %v", hasRecord) // has record: true
// test on attributes partial matching
containRecord, _ := buffer.ContainRecord(map[string]interface{}{
"level": "info",
"message": "message",
})
fmt.Printf("contain record: %v", containRecord) // contain record: true
}
You can also use the provided test assertion helpers in your tests:
AssertHasLogRecord
: to assert on exact attributes matchAssertHasNotLogRecord
: to assert on exact attributes non matchAssertContainLogRecord
: to assert on partial attributes matchAssertContainNotLogRecord
: to assert on partial attributes non match
and use Dump()
to print the current content of the TestLogBuffer.
For example:
package main_test
import (
"fmt"
"testing"
"github.com/ankorstore/yokai/log"
"github.com/ankorstore/yokai/log/logtest"
)
func TestLogger(t *testing.T) {
buffer := logtest.NewDefaultTestLogBuffer()
logger, _ := log.NewDefaultLoggerFactory().Create(log.WithOutputWriter(buffer))
logger.Info().Msg("some message example")
// print records
buffer.Dump()
// assertion success
logtest.AssertHasLogRecord(t, buffer, map[string]interface{}{
"level": "info",
"message": "some message example",
})
// assertion success
logtest.AssertHasNotLogRecord(t, buffer, map[string]interface{}{
"level": "info",
"message": "some invalid example",
})
// assertion success
logtest.AssertContainLogRecord(t, buffer, map[string]interface{}{
"level": "info",
"message": "message",
})
// assertion success
logtest.AssertContainNotLogRecord(t, buffer, map[string]interface{}{
"level": "info",
"message": "invalid",
})
}
# Packages
No description provided by the author
# Functions
CtxLogger retrieves a [Logger] from a provided context (or creates and appends a new one if missing).
DefaultLoggerOptions are the default options used in the [DefaultLoggerFactory].
FetchLogLevel returns a [Zerolog level] for a given value.
FetchLogOutputWriter returns a [LogOutputWriter] for a given value.
FromZerolog converts as [Zerolog logger] into a [Logger].
NewDefaultLoggerFactory returns a [DefaultLoggerFactory], implementing [LoggerFactory].
WithLevel is used to specify the log level to use.
WithOutputWriter is used to specify the output writer to use.
WithServiceName is used to add automatically a service log field value.
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
DefaultLoggerFactory is the default [LoggerFactory] implementation.
Logger provides the possibility to generate logs, and inherits of all [Zerolog] features.
Options are options for the [LoggerFactory] implementations.
# Interfaces
LoggerFactory is the interface for [Logger] factories.
# Type aliases
LoggerOption are functional options for the [LoggerFactory] implementations.
LogOutputWriter is an enum for the log output writers.