logger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2019 License: MIT Imports: 3 Imported by: 3

README

Build Status codecov

logger - package provides log.Logger adapters

Quick start

go get github.com/gregoryv/logger

Use in your code

type Car struct {
    logger.Logger
}

car := &Car{logger.New()}
car.Log("brakes are failing")
car.Logf("reached speed limit %s", 100)

or when testing the logger can be replaced by testing.T

func Test_car_speed(t *testing.T) {
    car := &Car{t}
    // asserts here
}

Documentation

Overview

Package logger adapts log.Logger with Log and Logf funcs.

There are two benefits of this, the smaller Logger interface limits logging to output only and can easily be replaced by the testing.T object during testing.

type Car struct {
	logger.Logger
}

car := &Car{logger.New()}
car.Log("brakes are failing")
car.Logf("reached speed limit %s", 100)
Example (Silent)
package main

import (
	"github.com/gregoryv/logger"
)

func main() {
	l := logger.Silent
	l.Log("nada")
	l.Logf("%s", "nada")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Log(args ...interface{})
	Logf(format string, args ...interface{})
}
var Silent Logger = silent(0)

Logger that outputs nothing

type Prefixed

type Prefixed struct {
	*Wrapped
	// contains filtered or unexported fields
}

func Prefix

func Prefix(wrap *Wrapped, prefix string) *Prefixed

func (*Prefixed) Log

func (p *Prefixed) Log(args ...interface{})

func (*Prefixed) Logf

func (p *Prefixed) Logf(format string, args ...interface{})

type Wrapped

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

func New

func New() *Wrapped

Returns a logger with log.LstdFlags|log.Lshortfile that writes to stderr

Example
package main

import (
	"github.com/gregoryv/logger"
)

func main() {
	type Car struct {
		logger.Logger
	}

	car := &Car{logger.New()}
	car.Log("brakes are failing")
	car.Logf("reached speed limit %v", 100)
}
Output:

func NewProgress

func NewProgress() *Wrapped

Returns a logger with empty flags that writes to stdout

func Wrap

func Wrap(l *log.Logger) *Wrapped

Wrap creates and adapter from Logger to log.Logger

func (*Wrapped) Log

func (wrap *Wrapped) Log(args ...interface{})

func (*Wrapped) Logf

func (wrap *Wrapped) Logf(format string, args ...interface{})

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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