fox

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2020 License: MIT Imports: 3 Imported by: 3

README

fox - package provides an alternate Logger design

fox logo

Quick start

go get github.com/gregoryv/fox

Log := fox.NewSyncLog(os.Stdout).Log
Log("something")

func Test_thing(t *testing.T) {
   thingWith.Logger := t
   ...
}

Design

Based on the principle that interfaces should be kept small this package provides a Logger interface with one func only

Log(v ...interface{})

design overview

The design focuses on separation between writing and formating

  • SyncLog only writes messages to the output ensuring each one ends with a new line

Documentation

Overview

Package fox provides loggers implementing the simple fox.Logger interface. The fox.Logger interface matches that of testing.T.Log method which makes it very easy to inject during testing.

Example usage

Use the Log method as a first class citizen

Log := fox.NewSyncLog(os.Stdout).Log
Log("some", "nice", "message")

and warnings are simplified by filtering out empty values

warn := fox.NewSyncLog(os.Stdout).FilterEmpty().Log
warn("") // will not be logged

// Log errors only if there are any
warn(nil) // nothing, it's nil
warn(io.EOF)

Wrap the standard log package and it's default logger

Log := fox.LoggerFunc(log.Println)
Log("hello", "standard", "logger")

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FilterEmpty

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

func NewFilterEmpty

func NewFilterEmpty(l Logger) *FilterEmpty

NewFilterEmpty wraps the given logger with a filter for empty values.

func (*FilterEmpty) Log

func (l *FilterEmpty) Log(v ...interface{})

Log calls the underlying logger only if v is non empty

Example
Log := NewSyncLog(os.Stdout).FilterEmpty().Log
Log("hey")
Log(nil)
Log("")
Log("there")
Output:

hey
there

type Logger

type Logger interface {
	Log(...interface{})
}
var NoLogger Logger = LoggerFunc(func(v ...interface{}) {})

type LoggerFunc

type LoggerFunc func(...interface{})

func (LoggerFunc) Log

func (me LoggerFunc) Log(args ...interface{})
Example
log.SetFlags(0)
log.SetOutput(os.Stdout)
std := LoggerFunc(log.Println)
std.Log("hello", "fox")
Output:

hello fox

type Logging added in v0.6.0

type Logging struct {
	Logger
}

Logging implements github.com/gregoryv/ant Setting interface

func (Logging) Set added in v0.6.0

func (me Logging) Set(v interface{}) error

type SyncLog

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

func NewSyncLog

func NewSyncLog(w io.Writer) *SyncLog

func (*SyncLog) FilterEmpty

func (l *SyncLog) FilterEmpty() *FilterEmpty

FilterEmpty returns a wrapper filtering out empty and nil values

func (*SyncLog) Log

func (l *SyncLog) Log(v ...interface{})

Log synchronizes calls to the underlying writer and makes sure each message ends with one new line

Example
Log := NewSyncLog(os.Stdout).Log
Log("hey")
Output:

hey

func (*SyncLog) SetOutput

func (l *SyncLog) SetOutput(w io.Writer)

Directories

Path Synopsis
Package format generates strings for logging.
Package format generates strings for logging.

Jump to

Keyboard shortcuts

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