Back to godoc.org

Package log

v0.0.0-...-3627568
Latest Go to latest

The highest tagged major version is .

Published: Jul 4, 2020 | License: GPL3 | Module: git.sr.ht/~ewintr/go-kit

Overview

Package log implements a generic interface to log

Example

Code:

package main

import (
	"bytes"
	"fmt"

	"git.sr.ht/~ewintr/go-kit/log"
)

// LoggerTestable represents a data structure for a log context
type LoggerTestable struct {
	TestKey string `json:"key"`
}

func (l LoggerTestable) ContextName() string { return "test" }

func main() {
	var buff bytes.Buffer
	var logger log.Logger

	logger = log.NewLogger(&buff)

	// Please ignore the following line, it was added to allow better
	// assertion of the results when logging.
	logger = logger.AddContext("time", "-")

	logger = log.Add(logger, LoggerTestable{
		TestKey: "value",
	})
	logger.Info("this is an example.")

	fmt.Println(buff.String())
}
{"message":"this is an example.","test":{"key":"value"},"time":"-"}

Index

Examples

func NewWriter

func NewWriter(l Logger) io.Writer

NewWriter returns io.Writer implementation based on a logger

type Caller

type Caller func() string

Caller represents a runtime file:line caller for log context

func NewCaller

func NewCaller(depth int) Caller

NewCaller returns a log context for runtime file caller with full path

func (Caller) ContextName

func (c Caller) ContextName() string

ContextName returns the key for the log context

type Contexter

type Contexter interface {
	ContextName() string
}

Contexter ensures type is intentionally a log context

type GoKit

type GoKit struct {
	// contains filtered or unexported fields
}

func (*GoKit) AddContext

func (gk *GoKit) AddContext(contextKey string, contextValue interface{}) Logger

AddContext attaches a key-value information to the log message

func (*GoKit) Debug

func (gk *GoKit) Debug(message string) error

Debug writes out the log message when debug is enabled

func (*GoKit) DebugEnabled

func (gk *GoKit) DebugEnabled(enable bool)

DebugEnabled sets debug flag to enable or disabled

func (*GoKit) DebugStatus

func (gk *GoKit) DebugStatus() bool

DebugStatus returns whether or not debug is enabled

func (*GoKit) Info

func (gk *GoKit) Info(message string) error

Info writes out the log message

type Logger

type Logger interface {
	AddContext(string, interface{}) Logger
	Info(string) error
	Debug(string) error
	DebugEnabled(bool)
	DebugStatus() bool
}

Logger represents a log implementation

func Add

func Add(l Logger, cc ...Contexter) Logger

Add adds a contexter interface to a Logger

func AttachError

func AttachError(l Logger, e error) Logger

AttachError adds a context called `attached_error` for error message that is relevant to the log entry.

func NewLogger

func NewLogger(logWriter io.Writer) Logger

NewLogger returns a Logger implementation

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier