logger

package module
v0.0.0-...-4d4057f Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2017 License: MIT Imports: 8 Imported by: 0

README

Simple structured logger for Go

Build Status

A simple logger package that provides levels, a number of output formats, and named sub-logs. Output formats include plain text, key/value, JSON and AMQP/RabbitMQ

Installation

Install using go get github.com/felix/logger.

Documentation is available at http://godoc.org/github.com/felix/logger

Usage

Create a normal logger
log := logger.New(&logger.Options{
	Name:  "app",
	Level: logger.Debug,
})
log.Error("unable to do anything")
... [INFO ] app: unable to do anything
Create a key/value logger
import "github.com/felix/logger/outputs/keyvalue"

log := logger.New(&logger.Options{
	Name:      "app",
	Level:     logger.Debug,
    Formatter: keyvalue.New(),
})
log.Warn("invalid something", "id", 344, "error", "generally broken")
... [WARN ] app: invalid something id=344 error="generally broken"
... [WARN ] app: invalid something id=344 error="generally broken"
Create a sub-logger
sublog := log.Named("database")
sublog.Info("connection initialised")
... [INFO ] app.database: connection initialised
Create a new Logger with pre-defined values

For major sub-systems there is no need to repeat values for each log call:

reqID := "555"
msgLog := sublog.WithFields("request", reqID)
msgLog.Error("failed to process message")
... [INFO ] app.database: failed to process message request=555

Credits

Solidly based on all the other loggers around, particularly Hashicorp's simple hclog with additions and modifications as required.

Documentation

Index

Constants

View Source
const DefaultTimeFormat = "2006-01-02T15:04:05.000Z0700"

DefaultTimeFormat unless specified by options

Variables

This section is empty.

Functions

func ToString

func ToString(v interface{}) string

ToString converts interface to string

Types

type DefaultWriter

type DefaultWriter struct{}

DefaultWriter implementation

func NewDefaultWriter

func NewDefaultWriter() *DefaultWriter

New creates a new writer

func (DefaultWriter) Write

func (kv DefaultWriter) Write(w io.Writer, m Message)

Write implements the logger.MessageWriter interface

type Level

type Level int

Level defines the logger output level

const (
	// NoLevel is prior to being defined
	NoLevel Level = 0
	// Debug is for development
	Debug Level = 1
	// Info are for interesting runtime events
	Info Level = 2
	// Warn is for almost errors
	Warn Level = 3
	// Error is a runtime problem
	Error Level = 4
)

func (Level) String

func (lvl Level) String() string

type Logger

type Logger interface {
	Log(level Level, args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Debug(args ...interface{})
	Error(args ...interface{})

	WithFields(args ...interface{}) Logger
	Named(name string) Logger
	IsDebug() bool
	IsInfo() bool
	IsWarn() bool
	IsError() bool
}

Logger defines our methods

func New

func New(opts *Options) Logger

New creates a new logger

type Message

type Message struct {
	Name   string
	Time   string
	Level  Level
	Fields []interface{}
}

Message type for writers

type MessageWriter

type MessageWriter interface {
	Write(io.Writer, Message)
}

MessageWriter interface for writing messages

type Options

type Options struct {
	Name       string
	Level      Level
	Fields     []interface{}
	Output     io.Writer
	TimeFormat string
	Formatter  MessageWriter
}

Options to configure the logger

Directories

Path Synopsis
outputs

Jump to

Keyboard shortcuts

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