levelog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: Unlicense Imports: 6 Imported by: 0

README

levelog

Log levels for golang's built in log library

Usage

import (
    "github.com/matansh/levelog"
    "github.com/matansh/levelog/loglevel"
)

func main() {
    ctx := context.Background()

    logger := levelog.NewLogger(
        loglevel.Info,
        log.New(os.Stdout, "", log.LstdFlags),
        log.New(os.Stderr, "", log.LstdFlags),
    )
    // from this line onward panics will be logged as errors
    defer logger.PanicRecovery()
    // embedding the logger instance into the applications context
    ctx = levellogger.ContextWithLogger(ctx, logger)

    logger.Info("tada")
}
Usage in your applications tests
import "github.com/matansh/levelog"

func Test(t *testing.T) {
    logger := levelog.NewTestLogger(t)
    ctx := levelog.ContextWithLogger(context.Background(), logger)

    logger.Debug("I am a log statement that will show up as part of the tests output")
    // log statements made as part of the tested code will also show up as part of the tests output
    TestedCode(ctx)
}
Testing against logs

Outputted log statements can be asserted in tests by passing a bytes buffer as the loggers output

import "github.com/matansh/levelog"

func Test(t *testing.T) {
    stdout := bytes.Buffer{}
    stderr := bytes.Buffer{}

    logger := levelog.NewLogger(
        loglevel.Debug,
        log.New(&stdout, "", 0),
        log.New(&stderr, "", 0),
    )
    ctx := levelog.ContextWithLogger(context.Background(), logger)

    TestedCode(ctx)
    // assertions regarding what is or is not in the buffers

As an example, see the tests implemented for the lib itself

Adapting pre exiting usages of the built in log lib

In order to avoid a painful refactoring the github.com/matansh/levelog/log package provides interface parity with the defacto interface exposed by the log lib.

Existing code as such

import "log"

log.Print("foo")
log.Fatalf("%s", "bar")
log.Panicln("spam")

Can simply replace the import statement to import "github.com/matansh/levelog/log" without any breakage or further refactoring.

Background

While there are plenty of logging libraries in the golang sphere I failed to find one that simply wrapped the languages pre existing logging capabilities in a level based interface.

This library does exactly that, it is a nicer packaging for something we already know and love.

footnote

This library is intentionally dependency-less in order to minimize the dependency trees of its importers, you are welcome ;)

Documentation

Overview

providing the option to safely persist and retrieve the logger instance from our context. while its best not to treat our context as a dumping ground for singletons, the logger is so commonly used that passing it around will pollute our method signatures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithLogger

func ContextWithLogger(ctx context.Context, logger Logger) context.Context

Types

type Logger

type Logger interface {
	Log(loglevel.Level, string)
	Debug(...any)
	Debugf(string, ...any)
	Info(...any)
	Infof(string, ...any)
	Warn(...any)
	Warnf(string, ...any)
	Error(error)
	PanicRecovery()
}

func FromContext

func FromContext(ctx context.Context) Logger

func NewLogger

func NewLogger(level loglevel.Level, stdout, stderr *log.Logger) Logger

func NewTestLogger

func NewTestLogger(t *testing.T) Logger

TestLogger is an implementation if the level logger interface that pipes logs into the test output.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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