structlog

package module
v0.0.0-...-ab5e019 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2020 License: MIT Imports: 3 Imported by: 0

README

structlog GoDoc Build Status

Structured logger for Go using the message templates format. Inspired by Serilog.

Motivation

Structured logging makes it easier to search and extract useful information from your logs. In Go this usually means passing key-value pairs to your logger in addition to a string message. This library takes a more compact approach by embeddeding key names in the format string itself.

Example

import (
    "github.com/danstiner/structlog"
    "github.com/danstiner/structlog/sink"
)

type Coordinates struct {
    Latitude, Longitude float64
}

log := structlog.New(sink.Json{os.Stdout})

log.Info("Processed {@position} in {elapsed} ms", Coordinates{25, 132}, 34)

Formatted output:

{
  "$level":"INFO",
  "$message":"Processed {\"Latitude\":25,\"Longitude\":132} in 34 ms",
  "$template":"Processed {@position} in {elapsed} ms",
  "$timestamp":"2228-03-22T12:34:56Z",
  "elapsed":34,
  "position":{"Latitude":25,"Longitude":132}
}

Some benefits of this approach:

  • Simple syntax, just surround key names in the format with braces and put the values in the same order
  • @ can be used to serialize structs and other complex values as JSON
  • The format string can be logged as is, this makes it easy to use grep to find which line of code a log came from

Interface

type Logger struct {
    Trace(template string, values ...interface{})
    Info(template string, values ...interface{})
    Error(template string, values ...interface{})
    Panic(template string, values ...interface{})

    With(key string, value interface{}) Logger
    WithFields(fields Fields) Logger
    WithError(err error) Logger
}

type Fields map[string]interface{}

Documentation

Index

Constants

View Source
const ErrorKey = "error"

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Fields    Fields
	Level     Level
	Message   string
	Template  string
	Timestamp time.Time
}

type Fields

type Fields map[string]interface{}

type Level

type Level int
const (
	TraceLevel Level = iota
	InfoLevel
	ErrorLevel
	PanicLevel
)

func (Level) String

func (l Level) String() string

type Logger

type Logger struct {
	Fields Fields
	Sink   Sink
}

func New

func New(sink Sink) Logger

func (*Logger) Error

func (l *Logger) Error(template string, values ...interface{})

func (*Logger) Info

func (l *Logger) Info(template string, values ...interface{})

func (*Logger) Panic

func (l *Logger) Panic(template string, values ...interface{})

func (*Logger) Trace

func (l *Logger) Trace(template string, values ...interface{})

func (*Logger) With

func (l *Logger) With(key string, value interface{}) Logger

func (*Logger) WithError

func (l *Logger) WithError(err error) Logger

func (*Logger) WithFields

func (l *Logger) WithFields(fields Fields) Logger

type Sink

type Sink interface {
	Log(event Event)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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