adapters

package
v1.57.3 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package adapters integrates the logger with go-logr/logr

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogrLogger

func NewLogrLogger(ctx context.Context) logr.Logger

NewLogr returns a gobox/pkg/log logger that implements the logr.Logger interface.

! This should ONLY be used if the consumer doesn't support calling ! gobox/pkg/log directly.

Example
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"strconv"

	"github.com/getoutreach/gobox/pkg/log"
	"github.com/getoutreach/gobox/pkg/log/adapters"
	"github.com/getoutreach/gobox/pkg/log/logtest"
)

func printEntries(entries []log.F) {
	for _, entry := range entries {
		entry["@timestamp"] = "2021-12-21T14:19:20.0424249-08:00"
		bytes, err := json.Marshal(entry)
		if err != nil {
			fmt.Println("unexpected", err)
		} else {
			fmt.Println(string(bytes))
		}
	}
}

func main() {
	logs := logtest.NewLogRecorder(nil)
	defer logs.Close()

	logger := adapters.NewLogrLogger(context.Background())

	logger.Info(strconv.FormatBool(logger.Enabled()))

	logger.Info("hello, world", "a", 1)

	childLogger := logger.WithValues("c", 1, "b", "hello, world!")
	childLogger.Info("info!!")
	childLogger.Error(fmt.Errorf("bad thing"), "end of the world!")

	printEntries(logs.Entries())

}
Output:

{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","app.version":"testing","level":"INFO","message":"true"}
{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","a":1,"app.version":"testing","level":"INFO","message":"hello, world"}
{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","app.version":"testing","b":"hello, world!","c":1,"level":"INFO","message":"info!!"}
{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","app.version":"testing","b":"hello, world!","c":1,"error.error":"bad thing","error.kind":"error","error.message":"bad thing","level":"ERROR","message":"end of the world!"}

Types

This section is empty.

Jump to

Keyboard shortcuts

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