charmlogr

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 2 Imported by: 0

README

Go Reference

charmlogr

logr implementation for github.com/charmbracelet/log

Documentation

Overview

Example
package main

import (
	"errors"
	"os"

	"github.com/charmbracelet/log"

	"github.com/aerfio/charmlogr"
)

func main() {
	l := charmlogr.NewLogger(log.NewWithOptions(os.Stdout, log.Options{
		Level: log.DebugLevel,
	}))
	l.Info("info msg")
	l.WithName("loggerName").V(1).Info("debug message")
	l.Error(errors.New("whoops"), "additional msg", "key", "value")
	l.Error(nil, "no error but err level")

}
Output:

INFO info msg v=0
DEBU loggerName: debug message v=1
ERRO additional msg err=whoops key=value
ERRO no error but err level

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogSink

func NewLogSink(l *log.Logger) logr.LogSink

func NewLogSinkWitOptions

func NewLogSinkWitOptions(l *log.Logger, options ...Option) logr.LogSink

func NewLogger

func NewLogger(l *log.Logger) logr.Logger

func NewLoggerWithOptions

func NewLoggerWithOptions(l *log.Logger, options ...Option) logr.Logger
Example
package main

import (
	"errors"
	"os"

	"github.com/charmbracelet/log"

	"github.com/aerfio/charmlogr"
)

func main() {
	l := charmlogr.NewLoggerWithOptions(
		log.New(os.Stdout), // default info lvl
		charmlogr.WithErrorFieldName("error"),
		charmlogr.WithVerbosityFieldName("verbosity"),
	)
	l.V(1).Info("does not get logged")
	l.Info("some message")
	l.Error(errors.New("whoops"), "log line with error")
}
Output:

INFO some message verbosity=0
ERRO log line with error error=whoops
Example (Json_formatter)
package main

import (
	"errors"
	"os"

	"github.com/charmbracelet/log"

	"github.com/aerfio/charmlogr"
)

func main() {
	l := charmlogr.NewLoggerWithOptions(
		log.NewWithOptions(os.Stdout, log.Options{Formatter: log.JSONFormatter}),
	)
	l.WithName("json-logger").Info("some message")
	l.Error(errors.New("whoops"), "log line with error")
}
Output:

{"lvl":"info","msg":"some message","prefix":"json-logger","v":0}
{"err":"whoops","lvl":"error","msg":"log line with error"}
Example (Logfmt_formatter)
package main

import (
	"errors"
	"os"

	"github.com/charmbracelet/log"

	"github.com/aerfio/charmlogr"
)

func main() {
	l := charmlogr.NewLoggerWithOptions(
		log.NewWithOptions(os.Stdout, log.Options{
			Formatter: log.LogfmtFormatter,
		}),
	)
	l.WithName("logfmt-logger").Info("some message")
	l.Error(errors.New("whoops"), "log line with error")
}
Output:

lvl=info prefix=logfmt-logger msg="some message" v=0
lvl=error msg="log line with error" err=whoops
Example (More_options)
package main

import (
	"errors"
	"os"
	"time"

	"github.com/charmbracelet/log"

	"github.com/aerfio/charmlogr"
)

func main() {
	l := charmlogr.NewLoggerWithOptions(
		log.NewWithOptions(os.Stdout, log.Options{
			TimeFunction: func() time.Time {
				return time.Date(1996, time.March, 24, 1, 2, 3, 4, time.UTC)
			},
			Prefix:          "test-prefix",
			ReportTimestamp: true,
			ReportCaller:    true,
			CallerOffset:    0,
			Fields:          []any{"key-pair", 1, "another-key", "value-for-that"},
			Formatter:       log.LogfmtFormatter,
		}),
	)
	l.
		WithName("json-logger").
		Info("some message")
	l.
		Error(errors.New("whoops"), "log line with error")
}
Output:

ts="1996/03/24 01:02:03" lvl=info caller=charmlogr/example_test.go:83 prefix=test-prefix/json-logger msg="some message" key-pair=1 another-key=value-for-that v=0
ts="1996/03/24 01:02:03" lvl=error caller=charmlogr/example_test.go:85 prefix=test-prefix msg="log line with error" key-pair=1 another-key=value-for-that err=whoops

Types

type Option

type Option func(*charmLogger)

Option is additional parameter for NewLoggerWithOptions.

func WithErrorFieldName

func WithErrorFieldName(name string) Option

WithErrorFieldName changes the default field name from "err"

func WithNameSeparator

func WithNameSeparator(separator string) Option

WithNameSeparator changes the default separator of name parts. Default value is "/"

func WithVerbosityFieldName

func WithVerbosityFieldName(name string) Option

WithVerbosityFieldName updates the field key for logr.Info verbosity, which by default is set to "v". If set to "", the verbosity key is added to log line

type Underlier

type Underlier interface {
	GetUnderlying() *log.Logger
}

Underlier exposes access to the underlying logging implementation. Since callers only have a logr.Logger, they have to know which implementation is in use, so this interface is less of an abstraction and more of way to test type conversion.

Jump to

Keyboard shortcuts

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