log

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2019 License: ISC Imports: 6 Imported by: 0

README

log

This package provides structured logging for Go applications (it is a wrapper for go-kit/kit/log).

Default output format is log.JSON and default log level is log.InfoLevel.

Quick Start

You can use the global/singelton logger as follows:

package main

import (
  "errors"

  "github.com/moorara/observe/log"
)

func main() {
  log.SetOptions(log.Options{
    Environment: "staging",
    Region:      "us-east-1",
  })

  log.Error(
    "message", "Hello, World!",
    "error", errors.New("too late!"),
  )
}

Output:

{"caller":"log.go:228","environment":"staging","error":"too late!","level":"error","message":"Hello, World!","region":"us-east-1","timestamp":"2019-09-02T04:44:29.74648Z"}

Or you can create a new instance logger as follows:

package main

import "github.com/moorara/observe/log"

func main() {
  logger := log.NewLogger(log.Options{
    Name:        "hello-world",
    Environment: "production",
    Region:      "us-east-1",
    Level:       "debug",
    Format:      log.JSON,
  })

  logger.Debug(
    "message", "Hello, World!",
    "context", map[string]interface{}{
      "retries": 4,
    },
  )
}

Output:

{"caller":"log.go:180","context":{"retries":4},"environment":"production","level":"debug","logger":"hello-world","message":"Hello, World!","region":"us-east-1","timestamp":"2019-09-02T04:45:18.426484Z"}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithLogger added in v0.2.3

func ContextWithLogger(ctx context.Context, logger *Logger) context.Context

ContextWithLogger returns a new context that holds a reference to the logger.

func Debug

func Debug(kv ...interface{}) error

Debug logs a debug-level event using singleton logger.

func Error

func Error(kv ...interface{}) error

Error logs an error-level event using singleton logger.

func Info

func Info(kv ...interface{}) error

Info logs an info-level event using singleton logger.

func SetOptions

func SetOptions(opts Options)

SetOptions set optional options for singleton logger.

func Warn

func Warn(kv ...interface{}) error

Warn logs a warn-level event using singleton logger.

Types

type Format

type Format int

Format is the type for output format.

const (
	// JSON represents a json logger
	JSON Format = iota
	// Logfmt represents logfmt logger
	Logfmt
)

type Level

type Level int

Level is the type for logging level.

const (
	// NoneLevel log
	NoneLevel Level = iota
	// ErrorLevel log
	ErrorLevel
	// WarnLevel log
	WarnLevel
	// InfoLevel log
	InfoLevel
	// DebugLevel log
	DebugLevel
)

type Logger

type Logger struct {
	Level  Level
	Logger *kitLog.SwapLogger
}

Logger wraps a go-kit Logger.

func LoggerFromContext added in v0.2.3

func LoggerFromContext(ctx context.Context) *Logger

LoggerFromContext returns a logger set on a context. If no logger found on the context, the singleton logger will be returned.

func NewLogger

func NewLogger(opts Options) *Logger

NewLogger creates a new logger.

func NewVoidLogger added in v0.2.0

func NewVoidLogger() *Logger

NewVoidLogger creates a void logger for testing purposes.

func (*Logger) Debug

func (l *Logger) Debug(kv ...interface{}) error

Debug logs in debug level.

func (*Logger) Error

func (l *Logger) Error(kv ...interface{}) error

Error logs in debug level.

func (*Logger) Info

func (l *Logger) Info(kv ...interface{}) error

Info logs in debug level.

func (*Logger) SetOptions added in v0.2.0

func (l *Logger) SetOptions(opts Options)

SetOptions resets a logger with new options.

func (*Logger) Warn

func (l *Logger) Warn(kv ...interface{}) error

Warn logs in debug level.

func (*Logger) With

func (l *Logger) With(kv ...interface{}) *Logger

With returns a new logger that always logs a set of key-value pairs (context).

type Options

type Options struct {
	Name        string
	Environment string
	Region      string
	Level       string
	Format      Format
	Writer      io.Writer
	// contains filtered or unexported fields
}

Options contains optional options for Logger.

Jump to

Keyboard shortcuts

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