logger

package
v1.6.5-rc7 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var InvalidFormatType = errors.New("invalid FormatType type")
View Source
var InvalidOutputType = errors.New("invalid OutputType type")

Functions

func CustomReplacer

func CustomReplacer(groups []string, a slog.Attr) slog.Attr

func ErrIgnoreExporter

func ErrIgnoreExporter() trace.SpanExporter

func KVsToAttr

func KVsToAttr(kvs ...any) (atts []attribute.KeyValue)

func KVsToSlogAttr added in v1.5.6

func KVsToSlogAttr(kvs ...interface{}) (attr []slog.Attr)

func NewJsonHandler

func NewJsonHandler(skip int) *customJsonHandler

func NewSpanContext

func NewSpanContext(ctx context.Context, name string) (context.Context, logr.Logger)

func NewTextHandler

func NewTextHandler(skip int) *customTextHandler

func SpanLogger

func SpanLogger(span trace.Span) logr.Logger
Example
package main

import (
	"context"
	"time"

	"github.com/google/uuid"
	"github.com/pkg/errors"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/trace"

	"github.com/machinefi/w3bstream/pkg/depends/base/types"
	"github.com/machinefi/w3bstream/pkg/depends/conf/logger"
	"github.com/machinefi/w3bstream/pkg/depends/conf/tracer"
	"github.com/machinefi/w3bstream/pkg/depends/kit/logr"
	"github.com/machinefi/w3bstream/pkg/depends/kit/metax"
	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
)

func main() {
	{
		var c = logger.Config{
			Level:  logr.DebugLevel,
			Output: logger.OUTPUT_TYPE__NEVER,
			Format: logger.FORMAT_TYPE__TEXT,
		}

		c.SetDefault()
		if c.Init() != nil {
			return
		}

		ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}})
		doLog(ctx, "OutputNever")
	}

	{
		var c = logger.Config{
			Level:  logr.InfoLevel,
			Output: logger.OUTPUT_TYPE__ON_FAILURE,
			Format: logger.FORMAT_TYPE__JSON,
		}

		c.SetDefault()
		if c.Init() != nil {
			return
		}

		ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}})
		doLog(ctx, "OutputOnFailureText")
	}

	{
		var c = logger.Config{
			Output: logger.OUTPUT_TYPE__ALWAYS,
			Format: logger.FORMAT_TYPE__TEXT,
			Level:  logr.DebugLevel,
		}

		c.SetDefault()
		if c.Init() != nil {
			return
		}

		ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}})
		doLog(ctx, "OutputAlwaysText")
	}

	{
		var c = logger.Config{
			Output: logger.OUTPUT_TYPE__ALWAYS,
			Format: logger.FORMAT_TYPE__JSON,
			Level:  logr.DebugLevel,
		}

		c.SetDefault()
		if c.Init() != nil {
			return
		}

		ctx := metax.ContextWithMeta(context.Background(), metax.Meta{"_id": {"from context"}, "operator": {"GetByID"}})
		doLog(ctx, "OutputAlwaysJson")
	}

	_ = tr.Shutdown(context.Background())

}

var tr *tracer.Config

func init() {
	tr = &tracer.Config{
		GrpcEndpoint: types.Endpoint{
			Scheme:   "http",
			Hostname: "localhost",
			Port:     4317,
		},
		ServiceName:    "test_log",
		ServiceVersion: "1.0.0",
		InstanceID:     uuid.NewString(),
		DebugMode:      datatypes.TRUE,
	}
	tr.SetDefault()
	if err := tr.Init(); err != nil {
		panic(err)
	}
}

func doLog(ctx context.Context, name string) {
	println(name)
	defer println()

	t := otel.Tracer(name)

	ctx, span := t.Start(ctx, "op", trace.WithTimestamp(time.Now()))
	defer func() {
		span.End(trace.WithTimestamp(time.Now()))
	}()

	ctx = logr.WithLogger(ctx, logger.SpanLogger(span))

	someActionWithSpanAndLog(ctx)

	otherActionsLogOnly(ctx)
}

func someActionWithSpanAndLog(ctx context.Context) {
	_, l := logr.Start(ctx, "SomeActionWithSpan")
	defer l.End()

	l.Info("msg")
	l.Debug("msg")
	l.Warn(errors.New("err"))
	l.Error(errors.New("err"))
}

func otherActionsLogOnly(ctx context.Context) {
	l := logr.FromContext(ctx)

	l.WithValues("test_key", 2).Info("test")
	l.Error(errors.New("any"))
}
Output:

func SpanMapExporter

func SpanMapExporter() trace.SpanExporter

func Std added in v1.5.6

func Std() logr.Logger

func StdContext added in v1.5.6

func StdContext(ctx context.Context) (context.Context, logr.Logger)

func StdoutSpanExporter

func StdoutSpanExporter() trace.SpanExporter

func WithErrIgnoreExporter

func WithErrIgnoreExporter() func(trace.SpanExporter) trace.SpanExporter

func WithSpanMapExporter

func WithSpanMapExporter(mappers ...SpanMapper) func(trace.SpanExporter) trace.SpanExporter

Types

type Config

type Config struct {
	Service string
	Version string
	Level   logr.Level `env:""`
	Output  OutputType `env:""`
	Format  FormatType `env:""`
	// contains filtered or unexported fields
}

func (*Config) Init

func (c *Config) Init() error

func (*Config) SetDefault

func (c *Config) SetDefault()

type FormatType

type FormatType uint8
const (
	FORMAT_TYPE_UNKNOWN FormatType = iota
	FORMAT_TYPE__JSON
	FORMAT_TYPE__TEXT
)

func ParseFormatTypeFromLabel

func ParseFormatTypeFromLabel(s string) (FormatType, error)

func ParseFormatTypeFromString

func ParseFormatTypeFromString(s string) (FormatType, error)

func (FormatType) ConstValues

func (v FormatType) ConstValues() []enum.IntStringerEnum

func (FormatType) Int

func (v FormatType) Int() int

func (FormatType) Label

func (v FormatType) Label() string

func (FormatType) MarshalText

func (v FormatType) MarshalText() ([]byte, error)

func (*FormatType) Scan

func (v *FormatType) Scan(src interface{}) error

func (FormatType) String

func (v FormatType) String() string

func (FormatType) TypeName

func (v FormatType) TypeName() string

func (*FormatType) UnmarshalText

func (v *FormatType) UnmarshalText(data []byte) error

func (FormatType) Value

func (v FormatType) Value() (driver.Value, error)

type OutputType

type OutputType uint8
const (
	OUTPUT_TYPE_UNKNOWN OutputType = iota
	OUTPUT_TYPE__ALWAYS
	OUTPUT_TYPE__ON_FAILURE
	OUTPUT_TYPE__NEVER
)

func ParseOutputTypeFromLabel

func ParseOutputTypeFromLabel(s string) (OutputType, error)

func ParseOutputTypeFromString

func ParseOutputTypeFromString(s string) (OutputType, error)

func (OutputType) ConstValues

func (v OutputType) ConstValues() []enum.IntStringerEnum

func (OutputType) Int

func (v OutputType) Int() int

func (OutputType) Label

func (v OutputType) Label() string

func (OutputType) MarshalText

func (v OutputType) MarshalText() ([]byte, error)

func (*OutputType) Scan

func (v *OutputType) Scan(src interface{}) error

func (OutputType) String

func (v OutputType) String() string

func (OutputType) TypeName

func (v OutputType) TypeName() string

func (*OutputType) UnmarshalText

func (v *OutputType) UnmarshalText(data []byte) error

func (OutputType) Value

func (v OutputType) Value() (driver.Value, error)

type SpanMapper

type SpanMapper func(trace.ReadOnlySpan) trace.ReadOnlySpan

func OutputFilter

func OutputFilter() SpanMapper

Jump to

Keyboard shortcuts

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