logger

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2020 License: MIT Imports: 7 Imported by: 2

README

Logo

Go Report Card Build Status Coverage Status GoDoc GitHub release GitHub license

logger is a fast Go logging package made to be simple but effective.

Overview

Install with:

go get github.com/hamba/logger
Formatters
  • JSON
  • Logfmt
  • Console
Handlers
  • StreamHandler Write directly to a Writer, usually os.Stdout
  • BufferedStreamHandler A buffered version of StreamHandler
  • FilterHandler Filter log line using a function
  • LevelFilterHandler Filter log line by level
  • DiscardHandler Discard everything

Examples

// Composable handlers
h := logger.LevelFilterHandler(
    logger.Info,
    logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
)

// The logger can have an initial context
l := logger.New(h, "env", "prod")

// All messages can have a context
l.Warn("connection error", "redis", conn.Name(), "timeout", conn.Timeout())

Will log the message

lvl=warn msg="connection error" redis=dsn_1 timeout=0.500

More examples can be found in the godocs.

Documentation

Overview

Package logger implements a logging package.

logger implements github.com/hamba/pkg Logger interface.

Example usage:

// Composable handlers
h := logger.LevelFilterHandler(
	logger.Info,
	logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
)

// The logger can have an initial context
l := logger.New(h, "env", "prod")

// All messages can have a context
l.Error("connection error", "redis", conn.Name(), "timeout", conn.Timeout())

Index

Examples

Constants

View Source
const (
	// LevelKey is the key used for message levels.
	LevelKey = "lvl"
	// MessageKey is the key used for message descriptions.
	MessageKey = "msg"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Event added in v1.1.0

type Event struct {
	Time    int64
	Msg     string
	Lvl     Level
	BaseCtx []interface{}
	Ctx     []interface{}
}

Event is a log event.

type FilterFunc

type FilterFunc func(e *Event) bool

FilterFunc represents a function that can filter messages.

type Formatter

type Formatter interface {
	// Format formats a log message.
	Format(e *Event) []byte
}

Formatter represents a log message formatter.

func ConsoleFormat added in v1.1.0

func ConsoleFormat() Formatter

ConsoleFormat formats a log line in a console format.

func JSONFormat

func JSONFormat() Formatter

JSONFormat formats a log line in json format.

func LogfmtFormat

func LogfmtFormat() Formatter

LogfmtFormat formats a log line in logfmt format.

type FormatterFunc

type FormatterFunc func(e *Event) []byte

FormatterFunc is a function formatter.

func (FormatterFunc) Format

func (f FormatterFunc) Format(e *Event) []byte

Format formats a log message.

type Handler

type Handler interface {
	// Log write the log message.
	Log(e *Event)
}

Handler represents a log handler.

func BufferedStreamHandler

func BufferedStreamHandler(w io.Writer, flushBytes int, flushInterval time.Duration, fmtr Formatter) Handler

BufferedStreamHandler writes buffered log messages to an io.Writer with the given format.

Example
package main

import (
	"os"
	"time"

	"github.com/hamba/logger"
)

func main() {
	h := logger.BufferedStreamHandler(os.Stdout, 2000, 1*time.Second, logger.LogfmtFormat())

	l := logger.New(h, "env", "prod")

	l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}
Output:

func DiscardHandler

func DiscardHandler() Handler

DiscardHandler does nothing, discarding all log messages.

func FilterHandler

func FilterHandler(fn FilterFunc, h Handler) Handler

FilterHandler returns a handler that only writes messages to the wrapped handler if the given function evaluates true.

Example
package main

import (
	"os"

	"github.com/hamba/logger"
)

func main() {
	h := logger.FilterHandler(
		func(e *logger.Event) bool {
			return e.Msg == "some condition"
		},
		logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
	)

	l := logger.New(h, "env", "prod")

	l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}
Output:

func LevelFilterHandler

func LevelFilterHandler(maxLvl Level, h Handler) Handler

LevelFilterHandler returns a handler that filters by log level.

Example
package main

import (
	"os"

	"github.com/hamba/logger"
)

func main() {
	h := logger.LevelFilterHandler(
		logger.Info,
		logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
	)

	l := logger.New(h, "env", "prod")

	l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}
Output:

func StreamHandler

func StreamHandler(w io.Writer, fmtr Formatter) Handler

StreamHandler writes log messages to an io.Writer with the given format.

Example
package main

import (
	"os"

	"github.com/hamba/logger"
)

func main() {
	h := logger.StreamHandler(os.Stdout, logger.LogfmtFormat())

	l := logger.New(h, "env", "prod")

	l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}
Output:

type HandlerFunc

type HandlerFunc func(e *Event)

HandlerFunc is a function handler.

func (HandlerFunc) Log

func (h HandlerFunc) Log(e *Event)

Log write the log message.

type Level

type Level int

Level represents the predefined log level.

const (
	Crit Level = iota
	Error
	Warn
	Info
	Debug
)

List of predefined log Levels.

func LevelFromString

func LevelFromString(lvl string) (Level, error)

LevelFromString converts a string to Level.

func (Level) String

func (l Level) String() string

String returns the string representation of the level.

type Logger

type Logger interface {
	io.Closer

	// Debug logs a debug message.
	Debug(msg string, ctx ...interface{})
	// Info logs an informational message.
	Info(msg string, ctx ...interface{})
	// Warn logs a warning message.
	Warn(msg string, ctx ...interface{})
	// Error logs an error message.
	Error(msg string, ctx ...interface{})
	// Crit logs a critical message.
	Crit(msg string, ctx ...interface{})
}

Logger represents a log writer.

func New

func New(h Handler, ctx ...interface{}) Logger

New creates a new Logger.

Example
package main

import (
	"os"

	"github.com/hamba/logger"
)

func main() {
	h := logger.LevelFilterHandler(
		logger.Info,
		logger.StreamHandler(os.Stdout, logger.LogfmtFormat()),
	)

	l := logger.New(h, "env", "prod") // The logger can have an initial context

	l.Info("redis connection", "redis", "some redis name", "timeout", 10)
}
Output:

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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