loki

package module
v0.0.0-...-3d24d56 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

README

Loki

/lo‧ki/ Finnish for log; to record something.

Go Reference

Getting started

Basic logging

func main() {
    l := "loki"
    log := loki.New()
    log.Info("hello from %s", l)
}
go run main.go
2021/01/01 18:00:00 [INFO] hello from loki

Setting different log levels

func main() {
    log := loki.New()
    log.Set(loki.ERROR)
    log.Debug("this won't appear")
    log.Error("error message")
}
go run main.go
2021/01/01 18:00:00 [ERROR] error message

Writing to a file

func main() {
    log := loki.New()
    log.WriteFile("/var/log/loki.log")
    log.Info("hello /var/log")
}

Logging in JSON format

Loki supports logging directly out as a json string.

func main() {
    log = loki.NewJsonLogger()
    log.Info("some information from your application")
}
go run main.go
{"timestamp":"2021-01-01T18:00:00.000000Z","message":"some information from your application","level":"INFO","caller":{"function":"main.main","line":3,"file":"/path/to/main.go"}}

Documentation

Index

Constants

View Source
const (
	//TRACE level logging
	TRACE int = 1
	//DEBUG level logging
	DEBUG int = 2
	//INFO level logging
	INFO int = 3
	//WARN level logging
	WARN int = 4
	//ERROR level logging
	ERROR int = 5
	//FATAL level logging
	FATAL int = 6
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Caller

type Caller struct {
	Function string `json:"function"`
	Line     int    `json:"line"`
	File     string `json:"file"`
}

Caller defines json structure to store data on where the log message orginated

type JsonLog

type JsonLog struct {
	Timestamp time.Time          `json:"timestamp"`
	Message   string             `json:"message"`
	Error     error              `json:"error,omitempty"`
	Level     string             `json:"level"`
	Caller    Caller             `json:"caller"`
	Context   []*json.RawMessage `json:"context,omitempty"`
}

JsonLog struct defines the structure of a log message

type Logger

type Logger interface {
	Set(int)
	WriteFile(string) error
	Debug(string, ...interface{})
	Info(string, ...interface{})
	Warn(string, ...interface{})
	Error(string, ...interface{})
	Fatal(string, ...interface{})
}

Logger is base interface for different loggers

type Loki

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

Loki basic logger

func New

func New() *Loki

New return a new instance of loki logger. By default writes to stdout

func (*Loki) Debug

func (l *Loki) Debug(mesg string, args ...interface{})

Debug write a log with level DEBUG

func (*Loki) Error

func (l *Loki) Error(mesg string, args ...interface{})

Error write a log with level ERROR

func (*Loki) Fatal

func (l *Loki) Fatal(mesg string, args ...interface{})

Fatal with a log with level FATAL

func (*Loki) Info

func (l *Loki) Info(mesg string, args ...interface{})

Info write a log with level INFO

func (*Loki) Set

func (l *Loki) Set(level int)

Set the logger level defaults to INFO

func (*Loki) Warn

func (l *Loki) Warn(mesg string, args ...interface{})

Warn write a log with level WARN

func (*Loki) WriteFile

func (l *Loki) WriteFile(path string) error

WriteFile tell loki where to write log files too

type LokiJson

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

LokiJson logs in json format

func NewJsonLogger

func NewJsonLogger() *LokiJson

NewJsonLogger return a new loki logger that logs in json format. By default writes to stdout

func (*LokiJson) Debug

func (l *LokiJson) Debug(mesg string, args ...interface{})

Debug write a log with level DEBUG

func (*LokiJson) Error

func (l *LokiJson) Error(mesg string, args ...interface{})

Error write a log with level ERROR

func (*LokiJson) Fatal

func (l *LokiJson) Fatal(mesg string, args ...interface{})

Fatal write a log with level FATAL

func (*LokiJson) Info

func (l *LokiJson) Info(mesg string, args ...interface{})

Info write a log with level INFO

func (*LokiJson) Set

func (l *LokiJson) Set(level int)

Set the logging level

func (*LokiJson) Warn

func (l *LokiJson) Warn(mesg string, args ...interface{})

Warn write a log with level WARN

func (*LokiJson) WriteFile

func (l *LokiJson) WriteFile(path string) error

Jump to

Keyboard shortcuts

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