golog

package module
v0.0.0-...-2cb9a8b Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2018 License: MIT Imports: 12 Imported by: 0

README

Golog

Build Status Coverage Status Go Report Card

Install

go get github.com/chapsuk/golog

Usage

package main

import (
    "github.com/chapsuk/golog"
)

func main() {
    // golog.SetFormatter(&golog.JSONFormatter)
    // golog.SetFormatter(&golog.LogstashFormatter{})
    golog.SetFormatter(&golog.TextFormatter{}) // not needed, TextFormatter by default
    golog.Info("Hallo")

    logger := golog.WithContext(golog.Context{
        "host":  "localhost",
        "place": "example",
    })
    logger.Warn("why so serious?!")

    logger.ErrorCtx(golog.Context{
        "uid": 666,
    }, "Omen detected")

    logger.Fatalf("The %s!", "end")
}

Output:

// JSONFormatter
{"_t":"2016-06-10T12:26:20+03:00", "_l":"INFO", "_m":"Hallo"}
{"_t":"2016-06-10T12:26:20+03:00", "_l":"WARN", "host":"localhost", "place":"example", "_m":"why so serious?!"}
{"_t":"2016-06-10T12:26:20+03:00", "_l":"ERROR", "host":"localhost", "place":"example", "uid":666, "_m":"Omen detected"}
{"_t":"2016-06-10T12:26:20+03:00", "_l":"FATAL", "host":"localhost", "place":"example", "uid":666, "_m":"The end!"}

// LogstashFormatter
{"@timestamp":"2016-06-10T12:26:35+03:00","@version":1,"level":"INFO","message":"Hallo"}
{"@timestamp":"2016-06-10T12:26:35+03:00","@version":1,"level":"WARN","host":"localhost","place":"example","message":"why so serious?!"}
{"@timestamp":"2016-06-10T12:26:35+03:00","@version":1,"level":"ERROR","host":"localhost","place":"example","uid":666,"message":"Omen detected"}
{"@timestamp":"2016-06-10T12:26:35+03:00","@version":1,"level":"FATAL","host":"localhost","place":"example","uid":666,"message":"The end!"}

// TextFormatter
2016-06-10T12:22:01+03:00  INFO  []  Hallo
2016-06-10T12:22:01+03:00  WARN  [place: example, host: localhost]  why so serious?!
2016-06-10T12:22:01+03:00  ERROR  [uid: 666, place: example, host: localhost]  Omen detected
2016-06-10T12:22:01+03:00  FATAL  [place: example, host: localhost, uid: 666]  The end!
exit status 1

Tests

  1. Install goconvey
  2. Run $GOPATH/bin/goconvey for watch test result in browser, or go test -v ./....

Benchmark

Benchmark source.

≻ go test -v -bench=. -benchmem ./bench/bench_test.go 2>/dev/null
BenchmarkLog-4                    100000             31437 ns/op            5345 B/op        112 allocs/op
BenchmarkLogComplex-4              20000             50919 ns/op           10915 B/op        224 allocs/op
BenchmarkLogxi-4                  100000             17629 ns/op            2321 B/op         58 allocs/op
BenchmarkLogxiComplex-4            30000             40875 ns/op            7379 B/op        178 allocs/op
BenchmarkLogrus-4                  30000             49576 ns/op            8253 B/op        139 allocs/op
BenchmarkLogrusComplex-4           20000             50315 ns/op           11544 B/op        229 allocs/op
BenchmarkLog15-4                   30000             48401 ns/op            9634 B/op        204 allocs/op
BenchmarkLog15Complex-4            20000             96301 ns/op           12751 B/op        254 allocs/op
BenchmarkGolog-4                   50000             22442 ns/op            3060 B/op         52 allocs/op
BenchmarkGologComplex-4            30000             39815 ns/op            8438 B/op        184 allocs/op
BenchmarkZapSugar-4              1000000              2200 ns/op             350 B/op         24 allocs/op
BenchmarkZapSugarComplex-4        100000             17372 ns/op            2386 B/op         73 allocs/op
BenchmarkZap-4                   1000000              1884 ns/op             785 B/op          4 allocs/op
BenchmarkZapComplex-4             500000              2050 ns/op             576 B/op          5 allocs/op
PASS
ok      command-line-arguments  28.506s

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug write debug log message

func DebugCtx

func DebugCtx(ctx Context, m string)

DebugCtx write debug log message with context

func Debugf

func Debugf(f string, args ...interface{})

Debugf write debug log message, fmt.Sprtinf style

func DebugfCtx

func DebugfCtx(ctx Context, f string, args ...interface{})

DebugfCtx write debug log message with context, fmt.Sprtinf style

func Debugln

func Debugln(args ...interface{})

Debugln func

func Error

func Error(args ...interface{})

Error write error log message

func ErrorCtx

func ErrorCtx(ctx Context, m string)

ErrorCtx write error log message with context

func Errorf

func Errorf(f string, args ...interface{})

Errorf write error log message fmt.Printf style

func ErrorfCtx

func ErrorfCtx(ctx Context, f string, args ...interface{})

ErrorfCtx write error log message with context, fmt.Printf style

func Errorln

func Errorln(args ...interface{})

Errorln func

func Fatal

func Fatal(args ...interface{})

Fatal write fatal log message and call os.Exit

func FatalCtx

func FatalCtx(ctx Context, m string)

FatalCtx write fatal log message with context and call os.Exit

func Fatalf

func Fatalf(f string, args ...interface{})

Fatalf write fatal log message and call os.Exit, fmt.Sprtinf style

func FatalfCtx

func FatalfCtx(ctx Context, f string, args ...interface{})

FatalfCtx write fatal log message with context and call os.Exit, fmt.Sprtinf style

func Fatalln

func Fatalln(args ...interface{})

Fatalln not supported, only for standart logger interface compatibility call os.Exit anyway

func Info

func Info(args ...interface{})

Info write info log message

func InfoCtx

func InfoCtx(ctx Context, m string)

InfoCtx write info log message with context

func Infof

func Infof(f string, args ...interface{})

Infof write info log message, fmt.Sprtinf style

func InfofCtx

func InfofCtx(ctx Context, f string, args ...interface{})

InfofCtx write info log message with context, fmt.Sprtinf style

func Infoln

func Infoln(args ...interface{})

Infoln func

func LevelToString

func LevelToString(level Level) string

LevelToString convert LogLevel to a string

func Panic

func Panic(args ...interface{})

Panic write panice log message and throw panic

func PanicCtx

func PanicCtx(ctx Context, m string)

PanicCtx write panice log message with context and throw panic

func Panicf

func Panicf(f string, args ...interface{})

Panicf write panice log message and throw panic, fmt.Sprintf style

func PanicfCtx

func PanicfCtx(ctx Context, f string, args ...interface{})

PanicfCtx write panice log message with context and throw panic, fmt.Sprintf style

func Panicln

func Panicln(args ...interface{})

Panicln not supported, only for standart logger interface compatibility throw panic anyway

func Print

func Print(args ...interface{})

Print info log message

func Printf

func Printf(f string, args ...interface{})

Printf info log message, fmt.Sprintf style

func Println

func Println(args ...interface{})

Println not supported, only for standart logger interface compatibility

func SetFormatter

func SetFormatter(f Formatter)

SetFormatter set log message formatter

func SetLevel

func SetLevel(lvl Level)

SetLevel set max log level

func SetOutput

func SetOutput(o io.Writer)

SetOutput for logger messages

func Warn

func Warn(args ...interface{})

Warn write warning log message

func WarnCtx

func WarnCtx(ctx Context, m string)

WarnCtx write warning log message with context

func Warnf

func Warnf(f string, args ...interface{})

Warnf write warning log message, fmt.Sprintf style

func WarnfCtx

func WarnfCtx(ctx Context, f string, args ...interface{})

WarnfCtx write warning log message with context, fmt.Sprintf style

func Warnln

func Warnln(args ...interface{})

Warnln func

Types

type BufferPool

type BufferPool struct {
	Pool sync.Pool
}

BufferPool is pool of buffers

func NewBufferPool

func NewBufferPool() *BufferPool

NewBufferPool yield new buffer poll instance

func (*BufferPool) Get

func (p *BufferPool) Get() *bytes.Buffer

Get buffer from pool

func (*BufferPool) Put

func (p *BufferPool) Put(b *bytes.Buffer)

Put clear buffer and put to pool

type ConcurrentWriter

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

ConcurrentWriter write log message to output with output lock

func NewConcurrentWriter

func NewConcurrentWriter(w io.Writer) *ConcurrentWriter

NewConcurrentWriter yield new concurrent writer instance

func (*ConcurrentWriter) Write

func (w *ConcurrentWriter) Write(p []byte) (int, error)

Write bytes to output with lock

type Context

type Context map[string]interface{}

Context is log message context

func GetContext

func GetContext() Context

GetContext return current global log context

type Formatter

type Formatter interface {
	Format(out *bytes.Buffer, lvl Level, ctx Context, msg string, trace []byte) *bytes.Buffer
}

Formatter format log message to needed output

type ILogger

type ILogger interface {
	WithContext(Context) ILogger
	GetContext() Context

	SetFormatter(Formatter)
	SetOutput(io.Writer)
	SetLevel(Level)
	SetTraceLevel(Level)

	Print(...interface{})
	Debug(...interface{})
	Info(...interface{})
	Warn(...interface{})
	Error(...interface{})
	Fatal(...interface{})
	Panic(...interface{})

	Printf(string, ...interface{})
	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Warnf(string, ...interface{})
	Errorf(string, ...interface{})
	Fatalf(string, ...interface{})
	Panicf(string, ...interface{})

	Println(...interface{})
	Debugln(...interface{})
	Infoln(...interface{})
	Warnln(...interface{})
	Errorln(...interface{})
	Fatalln(...interface{})
	Panicln(...interface{})

	DebugCtx(Context, ...interface{})
	InfoCtx(Context, ...interface{})
	WarnCtx(Context, ...interface{})
	ErrorCtx(Context, ...interface{})
	FatalCtx(Context, ...interface{})
	PanicCtx(Context, ...interface{})

	DebugfCtx(Context, string, ...interface{})
	InfofCtx(Context, string, ...interface{})
	WarnfCtx(Context, string, ...interface{})
	ErrorfCtx(Context, string, ...interface{})
	FatalfCtx(Context, string, ...interface{})
	PanicfCtx(Context, string, ...interface{})
}

ILogger interface

func WithContext

func WithContext(ctx Context) ILogger

WithContext yield new logger instance with context new context append to current context

type JSONFormatter

type JSONFormatter struct {
	DateFormat string
}

JSONFormatter structure

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(
	b *bytes.Buffer,
	lvl Level,
	ctx Context,
	msg string,
	trace []byte,
) *bytes.Buffer

Format log message to json format

type Level

type Level uint8

Level logging

const (
	PanicLevel Level = iota
	FatalLevel
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
)

Levels constants

func GetLevel

func GetLevel() Level

GetLevel return current max log level

func StringToLevel

func StringToLevel(lvl string) Level

StringToLevel return Level by level name

type Logger

type Logger struct {
	Level      Level
	TraceLevel Level
	Writer     io.Writer
	Formatter  Formatter

	Pool    *BufferPool
	Context Context
	// contains filtered or unexported fields
}

Logger struct

func New

func New() *Logger

New return default logger

func NewLogger

func NewLogger(out io.Writer, f Formatter, ctx Context) *Logger

NewLogger yield new logger instance

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

Debug write debug log message

func (*Logger) DebugCtx

func (l *Logger) DebugCtx(ctx Context, args ...interface{})

DebugCtx write debug log message with context

func (*Logger) Debugf

func (l *Logger) Debugf(f string, args ...interface{})

Debugf write debug log message, fmt.Sprtinf style

func (*Logger) DebugfCtx

func (l *Logger) DebugfCtx(ctx Context, f string, args ...interface{})

DebugfCtx write debug log message with context, fmt.Sprtinf style

func (*Logger) Debugln

func (l *Logger) Debugln(args ...interface{})

Debugln print debug log msg

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

Error write error log message

func (*Logger) ErrorCtx

func (l *Logger) ErrorCtx(ctx Context, args ...interface{})

ErrorCtx write error log message with context

func (*Logger) Errorf

func (l *Logger) Errorf(f string, args ...interface{})

Errorf write error log message fmt.Printf style

func (*Logger) ErrorfCtx

func (l *Logger) ErrorfCtx(ctx Context, f string, args ...interface{})

ErrorfCtx write error log message with context, fmt.Printf style

func (*Logger) Errorln

func (l *Logger) Errorln(args ...interface{})

Errorln write error log message

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

Fatal write fatal log message

func (*Logger) FatalCtx

func (l *Logger) FatalCtx(ctx Context, args ...interface{})

FatalCtx write fatal log message with context

func (*Logger) Fatalf

func (l *Logger) Fatalf(f string, args ...interface{})

Fatalf write fatal log message

func (*Logger) FatalfCtx

func (l *Logger) FatalfCtx(ctx Context, f string, args ...interface{})

FatalfCtx write fatal log message

func (*Logger) Fatalln

func (l *Logger) Fatalln(args ...interface{})

Fatalln not supported, only for standart logger interface compatibility

func (*Logger) GetContext

func (l *Logger) GetContext() Context

GetContext return current global log context

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

GetLevel return current max log level

func (*Logger) GetTraceLevel

func (l *Logger) GetTraceLevel() Level

GetTraceLevel return max trace level

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

Info write info log message

func (*Logger) InfoCtx

func (l *Logger) InfoCtx(ctx Context, args ...interface{})

InfoCtx write info log message with context

func (*Logger) Infof

func (l *Logger) Infof(f string, args ...interface{})

Infof write info log message, fmt.Sprtinf style

func (*Logger) InfofCtx

func (l *Logger) InfofCtx(ctx Context, f string, args ...interface{})

InfofCtx write info log message with context, fmt.Sprtinf style

func (*Logger) Infoln

func (l *Logger) Infoln(args ...interface{})

Infoln write info log message

func (*Logger) Panic

func (l *Logger) Panic(args ...interface{})

Panic write panice log message and throw panic

func (*Logger) PanicCtx

func (l *Logger) PanicCtx(ctx Context, args ...interface{})

PanicCtx write panice log message with context and throw panic

func (*Logger) Panicf

func (l *Logger) Panicf(f string, args ...interface{})

Panicf write panice log message and throw panic, fmt.Sprintf style

func (*Logger) PanicfCtx

func (l *Logger) PanicfCtx(ctx Context, f string, args ...interface{})

PanicfCtx write panice log message with context and throw panic, fmt.Sprintf style

func (*Logger) Panicln

func (l *Logger) Panicln(args ...interface{})

Panicln not supported, only for standart logger interface compatibility throw panic anyway

func (*Logger) Print

func (l *Logger) Print(args ...interface{})

Print info log message

func (*Logger) Printf

func (l *Logger) Printf(f string, args ...interface{})

Printf info log message, fmt.Sprintf style

func (*Logger) Println

func (l *Logger) Println(args ...interface{})

Println not supported, only for standart logger interface compatibility

func (*Logger) SetFormatter

func (l *Logger) SetFormatter(f Formatter)

SetFormatter set log message formatter

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl Level)

SetLevel set max log level

func (*Logger) SetOutput

func (l *Logger) SetOutput(o io.Writer)

SetOutput for logger messages

func (*Logger) SetTraceLevel

func (l *Logger) SetTraceLevel(lvl Level)

SetTraceLevel set max log level for adding trace

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

Warn write warning log message

func (*Logger) WarnCtx

func (l *Logger) WarnCtx(ctx Context, args ...interface{})

WarnCtx write warning log message with context

func (*Logger) Warnf

func (l *Logger) Warnf(f string, args ...interface{})

Warnf write warning log message, fmt.Sprintf style

func (*Logger) WarnfCtx

func (l *Logger) WarnfCtx(ctx Context, f string, args ...interface{})

WarnfCtx write warning log message with context, fmt.Sprintf style

func (*Logger) Warnln

func (l *Logger) Warnln(args ...interface{})

Warnln write warning log message

func (*Logger) WithContext

func (l *Logger) WithContext(ctx Context) ILogger

WithContext yield new logger instance with context new context append to current context

type LogstashFormatter

type LogstashFormatter struct{}

LogstashFormatter is log message formatter to logstash format

func (*LogstashFormatter) Format

func (f *LogstashFormatter) Format(
	b *bytes.Buffer,
	lvl Level,
	ctx Context,
	msg string,
	trace []byte,
) *bytes.Buffer

Format log message to logstash format

type Pool

type Pool interface {
	Get() *bytes.Buffer
	Put(*bytes.Buffer)
}

Pool of buffers for help gc and reduce memory allocate

type StandardLogger

type StandardLogger interface {
	Print(...interface{})
	Printf(string, ...interface{})
	Println(...interface{})

	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Fatalln(...interface{})

	Panic(...interface{})
	Panicf(string, ...interface{})
	Panicln(...interface{})
}

StandardLogger is log package interface

type SyslogWriter

type SyslogWriter struct {
	Writer io.WriteCloser
	// contains filtered or unexported fields
}

SyslogWriter write logs to syslog

func NewSyslogWriter

func NewSyslogWriter(network, addr, tag string, timeout time.Duration) *SyslogWriter

NewSyslogWriter return new SyslogWriter instance with concurrent writer to syslog

func (*SyslogWriter) Close

func (w *SyslogWriter) Close()

Close close connection

func (*SyslogWriter) Write

func (w *SyslogWriter) Write(p []byte) (int, error)

Write writes data to syslog

type TextFormatter

type TextFormatter struct {
	DateFormat string
}

TextFormatter formatter for console output

func (*TextFormatter) Format

func (f *TextFormatter) Format(
	b *bytes.Buffer,
	lvl Level,
	ctx Context,
	msg string,
	trace []byte,
) *bytes.Buffer

Format log message

Directories

Path Synopsis
Package syslog provides a simple interface to the system log service.
Package syslog provides a simple interface to the system log service.

Jump to

Keyboard shortcuts

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