log

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2019 License: ISC Imports: 5 Imported by: 0

README

log

This package provides a logger for your go applications. 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 "github.com/moorara/goto/log"

func main() {
  log.SetOptions(log.Options{
    Environment: "prod",
    Region:      "us-east-1",
    Component:   "auth-service",
  })

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

Output:

{"caller":"main.go:12","component":"auth-service","environment":"prod","error":"too late!","level":"error","message":"Hello, World!","region":"us-east-1","timestamp":"2019-02-12T17:59:33.973456Z"}

Or you can create a new instance logger as follows:

package main

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

func main() {
  logger := log.NewLogger(log.Options{
    Format:      log.JSON,
    Level:       "debug",
    Name:        "handler",
    Environment: "stage",
    Region:      "us-east-1",
    Component:   "auth-service",
  })

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

Output:

{"caller":"main.go:15","component":"auth-service","context":{"retries":4},"environment":"prod","level":"debug","logger":"instance","message":"Hello, World!","region":"us-east-1","timestamp":"2019-02-12T17:59:33.973595Z"}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 added in v0.2.4

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 added in v0.2.4

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 (
	// DebugLevel log
	DebugLevel Level = iota
	// InfoLevel log
	InfoLevel
	// WarnLevel log
	WarnLevel
	// ErrorLevel log
	ErrorLevel
	// NoneLevel log
	NoneLevel
)

type Logger

type Logger struct {
	Level  Level
	Logger kitLog.Logger
}

Logger wraps a go-kit Logger

func NewLogger

func NewLogger(opts Options) *Logger

NewLogger creates a new logger

func NewNopLogger

func NewNopLogger() *Logger

NewNopLogger creates a new logger for testing purposes

func (*Logger) Debug

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

Debug logs a debug-level event

func (*Logger) Error

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

Error logs an error-level event

func (*Logger) Info

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

Info logs an info-level event

func (*Logger) Warn

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

Warn logs a warn-level event

func (*Logger) With

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

With returns a new logger which always logs a set of key-value pairs

type Options added in v0.2.2

type Options struct {
	Writer      io.Writer
	Format      Format
	Level       string
	Name        string
	Environment string
	Region      string
	Component   string
	// 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