logger

package module
v0.0.0-...-b7a9896 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2017 License: MIT Imports: 12 Imported by: 0

README

logger

Simple go logger which can write logs to multiple backends.

Advantages:

  1. Multiple backends with individual levels
  2. Simple
  3. Fast
  4. Prints file, line
  5. fmt.Print, fmt.Printf, fmt.Println like API
  6. Slack integration (you can choose separate levels for different Slack channels)
  7. Sentry integration

Format

[level] time_rfc3339 file:line > message

Example

package main

import "github.com/gofort/logger"

func main() {
	logger.SetLevel(logger.DebugLevel)
	logger.Debug("Debug information")
	logger.Infof("Info: %s", "some info here")
	logger.Warn("Warning here!")
	logger.Errorln("Error", "info!")
	logger.Fatal("Fatal ...")
}

Result:

[DEBUG] 2017-08-27T13:16:13+03:00 main.go:37 main > Debug information
[INFO] 2017-08-27T13:16:13+03:00 main.go:38 main > Info: some info here
[WARN] 2017-08-27T13:16:13+03:00 main.go:39 main > Warning here!
[ERROR] 2017-08-27T13:16:13+03:00 main.go:40 main > Error info!
[FATAL] 2017-08-27T13:16:13+03:00 main.go:41 main > Fatal ...

Multiple backends

package main

import (
	"fmt"
	"os"

	"github.com/gofort/logger"
)

func main() {

	f, err := os.OpenFile("./logs", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer f.Close()

	stderrBackend := logger.NewWriterBackend(os.Stderr, logger.DebugLevel, true, false, "") // colored output enabled via third param
	fileBackend := logger.NewWriterBackend(f, logger.InfoLevel, false, false, "") // no debug logs

	l := logger.NewLogger(stderrBackend, fileBackend)

	l.Debug("Debug information")
	l.Infof("Info: %s", "some info here")
	l.Warn("Warning here!")
	l.Errorln("Error", "info!")
}

Result:

$ go run example/main.go
[DEBUG] 2017-08-27T13:17:48+03:00 main.go:24 main > Debug information
[INFO] 2017-08-27T13:17:48+03:00 main.go:25 main > Info: some info here
[WARN] 2017-08-27T13:17:48+03:00 main.go:26 main > Warning here!
[ERROR] 2017-08-27T13:17:48+03:00 main.go:27 main > Error info!
$ cat logs
[INFO] 2017-08-27T13:17:48+03:00 main.go:25 main > Info: some info here
[WARN] 2017-08-27T13:17:48+03:00 main.go:26 main > Warning here!
[ERROR] 2017-08-27T13:17:48+03:00 main.go:27 main > Error info!

Levels

Currently implemented: Fatal, Error, Warn, Info, Debug.

If you set FatalLevel => only Fatal logs would appear.

If you set ErrorLevel => only Fatal and Error logs would appear.

If you set WarnLevel => only Fatal, Error and Warn logs would appear.

If you set InfoLevel => only Fatal, Error, Warn and Info logs would appear.

If you set DebugLevel => all logs would appear.

Fatal

Fatal works as in standart golang log package, it means that after log, os.Exit(1) called

Documentation

Index

Constants

View Source
const (
	FatalLevel = Level(iota)
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
)

Variables

This section is empty.

Functions

func Caller

func Caller(skip int) (string, int)

func Debug

func Debug(v ...interface{})

func Debugf

func Debugf(format string, v ...interface{})

func Debugln

func Debugln(v ...interface{})

func Error

func Error(v ...interface{})

func Errorf

func Errorf(format string, v ...interface{})

func Errorln

func Errorln(v ...interface{})

func Fatal

func Fatal(v ...interface{})

func Fatalf

func Fatalf(format string, v ...interface{})

func Fatalln

func Fatalln(v ...interface{})

func Info

func Info(v ...interface{})

func Infof

func Infof(format string, v ...interface{})

func Infoln

func Infoln(v ...interface{})

func SetLevel

func SetLevel(level Level)

func SetOutput

func SetOutput(writer io.Writer)

func Warn

func Warn(v ...interface{})

func Warnf

func Warnf(format string, v ...interface{})

func Warnln

func Warnln(v ...interface{})

Types

type Level

type Level uint8

type LogBackend

type LogBackend interface {
	Write(entry LogEntry) error
	GetLevel() Level
}

type LogEntry

type LogEntry struct {
	Caller struct {
		LineNumber int
		File       string
	}

	Time    time.Time
	Level   Level
	Message string
}

func NewLogEntry

func NewLogEntry(level Level, message string) LogEntry

type Logger

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

func NewLogger

func NewLogger(backends ...LogBackend) *Logger

func (*Logger) AddBackend

func (s *Logger) AddBackend(backend LogBackend)

func (*Logger) Debug

func (s *Logger) Debug(v ...interface{})

func (*Logger) Debugf

func (s *Logger) Debugf(format string, v ...interface{})

func (*Logger) Debugln

func (s *Logger) Debugln(v ...interface{})

func (*Logger) Error

func (s *Logger) Error(v ...interface{})

func (*Logger) Errorf

func (s *Logger) Errorf(format string, v ...interface{})

func (*Logger) Errorln

func (s *Logger) Errorln(v ...interface{})

func (*Logger) Fatal

func (s *Logger) Fatal(v ...interface{})

func (*Logger) Fatalf

func (s *Logger) Fatalf(format string, v ...interface{})

func (*Logger) Fatalln

func (s *Logger) Fatalln(v ...interface{})

func (*Logger) Info

func (s *Logger) Info(v ...interface{})

func (*Logger) Infof

func (s *Logger) Infof(format string, v ...interface{})

func (*Logger) Infoln

func (s *Logger) Infoln(v ...interface{})

func (*Logger) Warn

func (s *Logger) Warn(v ...interface{})

func (*Logger) Warnf

func (s *Logger) Warnf(format string, v ...interface{})

func (*Logger) Warnln

func (s *Logger) Warnln(v ...interface{})

func (*Logger) Write

func (s *Logger) Write(level Level, str string) error

type SentryBackend

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

func NewSentryBackend

func NewSentryBackend(dsn string, componentName, componentVersion string) (*SentryBackend, error)

func (*SentryBackend) GetLevel

func (s *SentryBackend) GetLevel() Level

func (*SentryBackend) Write

func (s *SentryBackend) Write(entry LogEntry) error

type SlackBackend

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

func NewSlackBackend

func NewSlackBackend(urlStr string, level Level, fixedLevel, async bool, componentName, componentVersion, hostname string) (*SlackBackend, error)

func (*SlackBackend) GetLevel

func (s *SlackBackend) GetLevel() Level

func (*SlackBackend) Write

func (s *SlackBackend) Write(entry LogEntry) error

type WriterBackend

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

func NewWriterBackend

func NewWriterBackend(output io.Writer, level Level, colored, printHostname bool, hostname string) *WriterBackend

func (*WriterBackend) GetLevel

func (s *WriterBackend) GetLevel() Level

func (*WriterBackend) Write

func (s *WriterBackend) Write(entry LogEntry) error

Jump to

Keyboard shortcuts

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