adapters

package
v1.90.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package adapters integrates the logger with 3rd party loggers

Description: This file integrates the logger with go-logr/logr

Description: This file integrates the logger with retryablehttp.LeveledLogger

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","module":"github.com/getoutreach/gobox"}
{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","a":1,"app.version":"testing","level":"INFO","message":"hello, world","module":"github.com/getoutreach/gobox"}
{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","app.version":"testing","b":"hello, world!","c":1,"level":"INFO","message":"info!!","module":"github.com/getoutreach/gobox"}
{"@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!","module":"github.com/getoutreach/gobox"}

func NewRetryableHTTPLogger added in v1.59.0

func NewRetryableHTTPLogger(ctx context.Context) retryablehttp.LeveledLogger

NewRetryableHTTPLogger returns a gobox/pkg/log logger that implements the retryablehttp.LeveledLogger interface.

Example
package main

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

	"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.NewRetryableHTTPLogger(context.Background())

	logger.Info("hello, info", "a", 1)
	logger.Debug("hello, debug", "a", 1)
	logger.Error("hello, error", "a", 1)
	logger.Warn("hello, warn", "a", 1)

	printEntries(logs.Entries())

}
Output:

{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","a":1,"app.version":"testing","level":"INFO","message":"hello, info","module":"github.com/getoutreach/gobox"}
{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","a":1,"app.version":"testing","level":"DEBUG","message":"hello, debug","module":"github.com/getoutreach/gobox"}
{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","a":1,"app.version":"testing","level":"ERROR","message":"hello, error","module":"github.com/getoutreach/gobox"}
{"@timestamp":"2021-12-21T14:19:20.0424249-08:00","a":1,"app.version":"testing","level":"WARN","message":"hello, warn","module":"github.com/getoutreach/gobox"}

Types

This section is empty.

Jump to

Keyboard shortcuts

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