log

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2019 License: ISC Imports: 5 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":"main.go:15","environment":"staging","error":"too late!","level":"error","message":"Hello, World!","region":"us-east-1","timestamp":"2019-07-31T05:11:42.19237Z"}

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{
    Format:      log.JSON,
    Level:       "debug",
    Name:        "hello-world",
    Environment: "production",
    Region:      "us-east-1",
    Component:   "main-function",
  })

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

Output:

{"caller":"main.go:15","component":"main-function","context":{"retries":4},"environment":"production","level":"debug","logger":"hello-world","message":"Hello, World!","region":"us-east-1","timestamp":"2019-07-31T05:12:38.585191Z"}

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

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 (
	// 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

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