minlog

package module
v0.0.0-...-4238205 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 9 Imported by: 1

README

minlog build codecov go report card go reference

Features

  • Time stamps
  • Callers
  • Simple leveled logging, argument type based
  • Contextual logging, context.Context based
  • Format customization including structured logging
  • Extremely simple one-method interface

Usage and docs

Docs and examples are available in GoDoc

Documentation

Overview

Minimalist, simple logger

Example (PkgErrorsCompatMultilineJSONEncoding)
package main

import (
	"context"
	"encoding/json"
	"errors"
	"time"

	pkgErrors "github.com/pkg/errors"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(
		withReproducibleTime,
		minlog.WithLineFormatter(func(tm, level, label, caller, msg string) string {
			b, _ := json.Marshal(map[string]string{"time": tm, "level": level, "label": label, "caller": caller, "msg": msg})
			return string(b)
		}))
	ctx := context.Background()
	err := errors.New("diagnostics")
	err = pkgErrors.WithMessage(err, "additional message")
	err = pkgErrors.WithMessage(err, "more details")
	l.Log(ctx, "Error:", err)
}
Output:

{"caller":"example_test.go:58","label":"","level":"error","msg":"Error: diagnostics\nadditional message\nmore details","time":"1975-12-02 18:42:57"}

Index

Examples

Constants

View Source
const (
	DefaultInfoLabel  = "info"
	DefaultErrorLabel = "error"
)

Variables

This section is empty.

Functions

func Label

func Label(ctx context.Context, label string) context.Context

Label sets/adds label. You are still able to use custom context key and custom ctx setter, consider SetDefaultLabelKey and WithLabelFormatter.

Example (Context)
package main

import (
	"context"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime)
	ctx := minlog.Label(context.Background(), "scope")
	l.Log(ctx, "ok")
}
Output:

1975-12-02 18:42:57 info scope example_test.go:66 ok
Example (NestedContext)
package main

import (
	"context"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime)
	ctx := context.Background()
	ctx = minlog.Label(ctx, "scope")
	ctx = minlog.Label(ctx, "subscope")
	l.Log(ctx, "ok")
}
Output:

1975-12-02 18:42:57 info scope:subscope example_test.go:76 ok

func Log

func Log(ctx context.Context, message ...interface{})
Example (LevelError)
package main

import (
	"context"
	"errors"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime)
	ctx := context.Background()
	l.Log(ctx, "Error:", errors.New("diagnostics"))
}
Output:

1975-12-02 18:42:57 error example_test.go:35 Error: diagnostics
Example (LevelInfoAndLabels)
package main

import (
	"context"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime)
	ctx := context.Background()
	ctx = minlog.Label(ctx, "component-a")
	ctx = minlog.Label(ctx, "request-75")
	l.Log(ctx, "just string", true, []byte("valid utf8"), []byte{0xff})
}
Output:

1975-12-02 18:42:57 info component-a:request-75 example_test.go:27 just string true valid utf8 [255]
Example (NilContext)
package main

import (
	"errors"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime)
	l.Log(nil, "Error:", errors.New("diagnostics")) //nolint:staticcheck // disable nil context warning
}
Output:

1975-12-02 18:42:57 error example_test.go:42 Error: diagnostics

func SetDefaultLabelKey

func SetDefaultLabelKey(l interface{})

SetDefaultLabelKey touches global variable, it is not thread safe. And it obviously affects Label function. You have to set default label key before first Label usage.

func SetDefaultLogger

func SetDefaultLogger(l Interface)

SetDefaultLogger touches global variable, it is not thread safe.

Example
package main

import (
	"context"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	minlog.SetDefaultLogger(minlog.New(withReproducibleTime))
	minlog.Log(context.Background(), "ok")
}
Output:

1975-12-02 18:42:57 info example_test.go:162 ok

Types

type Interface

type Interface interface {
	Log(ctx context.Context, message ...interface{})
}

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func New

func New(opt ...Option) *Logger

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, message ...interface{})

type Option

type Option func(*Logger)

func WithCallerCutter

func WithCallerCutter(ctr func(p string) string) Option
Example
package main

import (
	"context"
	"path"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime, minlog.WithCallerCutter(path.Base))
	l.Log(context.Background(), "ok")
}
Output:

1975-12-02 18:42:57 info example_test.go:155 ok

func WithCommonLabel

func WithCommonLabel(s string) Option
Example
package main

import (
	"context"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime, minlog.WithCommonLabel("logger_label"))
	ctx := context.Background()
	l.Log(ctx, "ok")
	l.Log(minlog.Label(ctx, "from_ctx"), "ok")
}
Output:

1975-12-02 18:42:57 info logger_label example_test.go:136 ok
1975-12-02 18:42:57 info logger_label:from_ctx example_test.go:137 ok

func WithLabelFormatter

func WithLabelFormatter(fmtr func(interface{}) string) Option
Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(
		withReproducibleTime,
		minlog.WithLabelFormatter(func(v interface{}) string {
			return fmt.Sprintf("[%s]", v)
		}),
	)
	ctx := minlog.Label(context.Background(), "component-a")
	l.Log(ctx, "ok")
}
Output:

1975-12-02 18:42:57 info [component-a] example_test.go:102 ok

func WithLabelPlaceholder

func WithLabelPlaceholder(s string) Option
Example
package main

import (
	"context"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime, minlog.WithLabelPlaceholder("<nolabel>"))
	ctx := context.Background()
	l.Log(ctx, "ok")
}
Output:

1975-12-02 18:42:57 info <nolabel> example_test.go:128 ok

func WithLevelLabels

func WithLevelLabels(info, err string) Option
Example
package main

import (
	"context"
	"errors"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime, minlog.WithLevelLabels("[INFO_]", "[ERROR]"))
	ctx := context.Background()
	l.Log(ctx, "ok")
	l.Log(ctx, errors.New("error details"))
}
Output:

1975-12-02 18:42:57 [INFO_] example_test.go:146 ok
1975-12-02 18:42:57 [ERROR] example_test.go:147 error details

func WithLineFormatter

func WithLineFormatter(fmtr func(tm, level, label, caller, msg string) string) Option
Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(
		withReproducibleTime,
		minlog.WithLineFormatter(func(tm, level, label, caller, msg string) string {
			return fmt.Sprintf("%[1]s [%[2]s] %[4]s [%[3]s] %[5]q", tm, level, label, caller, msg)
		}),
	)
	ctx := minlog.Label(context.Background(), "component-a")
	l.Log(ctx, "ok")
}
Output:

1975-12-02 18:42:57 [info] example_test.go:89 [component-a] "ok"

func WithNower

func WithNower(nwr func() time.Time) Option

func WithTimeFormat

func WithTimeFormat(fmt string) Option
Example
package main

import (
	"context"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	l := minlog.New(withReproducibleTime, minlog.WithTimeFormat(time.RFC3339Nano))
	ctx := context.Background()
	l.Log(ctx, "ok")
}
Output:

1975-12-02T18:42:57.777Z info example_test.go:110 ok

func WithWriter

func WithWriter(w io.Writer) Option
Example
package main

import (
	"bytes"
	"context"
	"fmt"
	"time"

	"github.com/michurin/minlog"
)

var (
	constTime            = time.Unix(186777777, 777000000).UTC()
	withReproducibleTime = minlog.WithNower(func() time.Time { return constTime })
)

func main() {
	output := new(bytes.Buffer)
	l := minlog.New(withReproducibleTime, minlog.WithWriter(output))
	ctx := context.Background()
	l.Log(ctx, "ok")
	fmt.Printf("%q\n", output.String())
}
Output:

"1975-12-02 18:42:57 info example_test.go:119 ok\n"

Jump to

Keyboard shortcuts

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