loki

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2019 License: MIT Imports: 8 Imported by: 3

README

Loki

GitHub release Go Report Card codecov CircleCI

Inspired by a popular NodeJS logger library: debug.

Feature

  • Out of the box
  • Colorful console output
  • Enable logger by environment easily

Difference with other logger library

Sometimes, it's not enough to split loggers by different log level.

With Loki, you can inject more detailed logs in your code, and enable them by environment variable whenever you want, which makes debug more conveniently.

Install

go get github.com/joway/loki@latest

API

Go Doc

Usage

Use root logger
// default level is INFO
loki.SetLevel(loki.DEBUG)

loki.Info("x: %s", "hi")
loki.Debug("x: %s", "hi")
loki.Error("x: %s", "hi")
Create your logger
logger := loki.New("app:moduleName")
logger.Info("x: %s", "hi")
logger.Debug("x: %s", "hi")
logger.Error("x: %s", "hi")

To enable this logger, add env LOKI_ENV=app:moduleName in your startup command, like: LOKI_ENV=app:moduleName ./main.

You can also use LOKI_ENV=app:a,app:b,model:a to enable multiple loggers.

LOKI_ENV=* can enable all loggers.

Use file handler
// "02 Jan 06 15:04 MST"
l := loki.New("app:xxx")
fp, err := os.OpenFile("test.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
defer fp.Close()
flushIntervalMs := 1000
l.SetHandler(loki.NewFileHandler(fp, flushIntervalMs))

l.Info("x: %s", "hi")
Change time format
// "02 Jan 06 15:04 MST"
loki.SetTimeFormatter(time.RFC822)

// disable time output
loki.SetTimeFormatter("")
Use your custom logger formatter
type ErrFormatter struct {
	loki.Formatter
}

func (f ErrFormatter) format(a ...interface{}) string {
	err := a[0].(error)
	return fmt.Sprintf("Error %v", err)
}

logger := loki.New("app:xxx")
f := ErrFormatter{}
logger.SetFormatter(f)
logger.Debug(errors.New("test error"))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DEBUG level of DEBUG
	DEBUG = 1
	// INFO level of DEBUG
	INFO = 2
	// WARN level of DEBUG
	WARN = 3
	// ERROR level of DEBUG
	ERROR = 4
)

Functions

func Debug

func Debug(a ...interface{})

Debug output level DEBUG log

func Error

func Error(a ...interface{})

Error output level ERROR log

func Fatal

func Fatal(a ...interface{})

Fatal output level ERROR log and exit with code 1

func Info

func Info(a ...interface{})

Info output level INFO log

func SetFormatter

func SetFormatter(formatter Formatter)

SetFormatter set the formatter of logger

func SetHandler

func SetHandler(handler Handler)

SetHandler set the handler of logger

func SetLevel

func SetLevel(level int)

SetLevel set the level of logger

func SetTimeFormatter added in v0.2.2

func SetTimeFormatter(format string)

SetTimeFormatter set the time format string of logger

func Warn

func Warn(a ...interface{})

Warn output level WARN log

Types

type ConsoleHandler

type ConsoleHandler struct {
	Handler
}

ConsoleHandler output logs to console

type FileHandler

type FileHandler struct {
	Handler
	// contains filtered or unexported fields
}

FileHandler output logs to console

type Formatter

type Formatter interface {
	// contains filtered or unexported methods
}

Formatter format the message

func NewStandardFormatter

func NewStandardFormatter() Formatter

NewStandardFormatter ...

type Handler

type Handler interface {
	// contains filtered or unexported methods
}

Handler handle the output process

func NewConsoleHandler

func NewConsoleHandler() Handler

NewConsoleHandler return ConsoleHandler instance

func NewFileHandler

func NewFileHandler(fp *os.File, flushIntervalMs int) Handler

NewFileHandler return FileHandler instance

type Logger

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

Logger is a instance to handle logs

func New

func New(name string) Logger

New create a Logger instance with with its name

func (Logger) Check

func (l Logger) Check() bool

Check if logger's name match the LOKI_ENV setting

func (Logger) Compile

func (l Logger) Compile(a ...interface{}) string

Compile return final compiled log string

func (Logger) Debug

func (l Logger) Debug(a ...interface{})

Debug output level DEBUG log

func (Logger) Error

func (l Logger) Error(a ...interface{})

Error output level ERROR log

func (Logger) Fatal

func (l Logger) Fatal(a ...interface{})

Fatal output level ERROR log and exit with code 1

func (Logger) Info

func (l Logger) Info(a ...interface{})

Info output level INFO log

func (*Logger) SetFormatter

func (l *Logger) SetFormatter(formatter Formatter)

SetFormatter set the formatter of logger

func (*Logger) SetHandler

func (l *Logger) SetHandler(handler Handler)

SetHandler set the handler of logger

func (*Logger) SetLevel

func (l *Logger) SetLevel(level int)

SetLevel set the level of logger

func (*Logger) SetLogEnv added in v0.2.3

func (l *Logger) SetLogEnv(env string)

SetLogEnv set the LOKI_ENV of logger

func (*Logger) SetTimeFormatter

func (l *Logger) SetTimeFormatter(format string)

SetTimeFormatter set the time format string of logger

func (Logger) Warn

func (l Logger) Warn(a ...interface{})

Warn output level WARN log

type StandardFormatter

type StandardFormatter struct {
	Formatter
}

StandardFormatter default formatter

Jump to

Keyboard shortcuts

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