dalog

package module
v0.0.0-...-7d51247 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2018 License: Apache-2.0 Imports: 11 Imported by: 0

README

dalog

Logger abstraction allowing JSON via Zap logger and string via Go's logger. Also supports a key value context. Features

  • Both JSON and column std output logger
  • Context based loggers
  • Support for pkg errors stack traces
  • DebugContext allows debug out put to be related and annotated
  • Environment variable for configuration

Options

  • DALOG_LOGGER=[ZAP|GO] - Define which logger to use
  • DALOG_DEBUG=[TRUE|FALSE] - include debug and stack log statements
  • DALOG_STACK=[TRUE|FALSE] - include stack traces in the error log statements

See it in action

Running make will compile, lint, vet and run tests dalog contains a lame test that is really just sample usage code (log_test.go).

JSON via Zap
os.Setenv("DALOG_LOGGER", "ZAP")
dalog.WithContext(dalog.WithID("A123"), dalog.WithHostname()).Infof("%s %s", "hello", "world")
{"level":"info","ts":1512254914.971346,"msg":"hello world","ID":"A123","Hostname":"MacBook-Pro.local"}
String via go log
os.Setenv("DALOG_LOGGER", "GO")
dalog.WithContext(dalog.WithID("A123"), dalog.WithHostname()).Infof("%s %s", "hello", "world")
2017/12/02 00:43:28 INFO hello world, ID=A123, Hostname=MacBook-Pro.local

Stack Traces

Stack traces are supported using pkg errors and error log message.

	e := errors.New("This is an error using pkg error")
	dalog.NoContext().Error(e)
JSON output
{"level":"error","ts":1514921204.09196,"msg":"This is an error using pkg error","stack":"\ngithub.com/craigivy/dalog_test.TestStack\n\t/Users/civerson/dev/go/src/github.com/craigivy/dalog/log_test.go:61\ntesting.tRunner\n\t/usr/local/Cellar/go/1.9.2/libexec/src/testing/testing.go:746\nruntime.goexit\n\t/usr/local/Cellar/go/1.9.2/libexec/src/runtime/asm_amd64.s:2337"}
Standard output
2018/01/02 12:26:44 ERROR This is an error using pkg error, stack=
github.com/craigivy/dalog_test.TestStack
        /Users/civerson/dev/go/src/github.com/craigivy/dalog/log_test.go:61
testing.tRunner
        /usr/local/Cellar/go/1.9.2/libexec/src/testing/testing.go:746
runtime.goexit
        /usr/local/Cellar/go/1.9.2/libexec/src/runtime/asm_amd64.s:2337

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Key   string
	Value string
}

Context to associate to a logger

func WithDebugContext

func WithDebugContext(context string) Context

WithDebugContext is a specialized context to allow debug lines to be related and annotated

func WithHostname

func WithHostname() Context

WithHostname creates and hostname context

func WithID

func WithID(id string) Context

WithID creates an ID context

func WithKey

func WithKey(key, value string) Context

WithKey creates a new context with the provided key and value

type Frame

type Frame uintptr

Frame represents a program counter inside a stack frame.

func (Frame) Format

func (f Frame) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags that alter the printing of some verbs, as follows:

%+s   path of source file relative to the compile time GOPATH
%+v   equivalent to %+s:%d

type Log

type Log interface {
	Info(a ...interface{})
	Warn(a ...interface{})
	Error(err error)
	Debug(a ...interface{})
	Infof(format string, a ...interface{})
	Warnf(format string, a ...interface{})
	Debugf(format string, a ...interface{})
	// logs the current stack and message at the DEBUG level
	Stackf(format string, a ...interface{})
	WithContext(contexts ...Context) Log
}

Log is the logger

func NoContext

func NoContext() Log

NoContext creates a logger without any configured context.

func WithContext

func WithContext(contexts ...Context) Log

WithContext creates a logger with a context

type StackTrace

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

func (StackTrace) Format

func (st StackTrace) Format(s fmt.State, verb rune)

Jump to

Keyboard shortcuts

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