slog

package module
v0.0.0-...-7a9bfb7 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2019 License: Apache-2.0 Imports: 12 Imported by: 14

README

Slog

Build Status GoDoc Go Report Card

Slog (slow log) is a attempt to make a logger with more features than the standard go logger but with similar performance. The logger feature levels, tags, flexibility to implement diferentes formatters and committers. It´s simple and easy to use. Import the package, use the free functions and you will have a logger to the console. If you want to log to a file change the writer.

Performance

You can see bellow a simple comparison of some loggers. First is the baseline logger, the go logger, with is simple but fast. The others loggers with more functionalities have slower performance, as expected, but slog without debug information (Di) have a good set of features with a performance better than Logrus.

Benchmark name N Time
BenchmarkPureGolog-4 300000 3980 ns/op
BenchmarkLogrus-4 200000 7912 ns/op
BenchmarkSlogNullFile-4 200000 9052 ns/op
BenchmarkSlogJSONNullFile-4 200000 8926 ns/op
BenchmarkSlogNullFileNoDi-4 200000 5662 ns/op
BenchmarkSlogJSONNullFileNoDi-4 200000 5716 ns/op

Some optimizations will be needed before slog can be used like a high-performance logger. I need to get deeper into go and learn to do some optimizations to achieve it, mainly for the debug information.

Bottlenecks

Slog have 3 main bottlenecks:

  • Io bottleneck: this occurs when committer send the data to disk or to some db.
  • Log message assemble: the log message is assembled in a byte slice and uses the append function that make things slow. Message formatting is a problem too, mainly the date and time formatting.
  • Debug information (line number and file name) is a trouble. I need to come with a solution that make it more adequate for production. Debug information in a test environment is ok.

For the io bottleneck there's no safe solution besides buy a fast hardware. The in memory approach may be good for some tasks but its not safe if something wrong happen.

TODO

  • Need to check all code for allocations and minimize that.
  • A more flexible way to deal with date and time.

Conclusion

Slog is a logger with more features and have a good performance but I need to make some optimizations to make it faster.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// GID is process group ID
	GID string
	// UID is process user ID
	UID string
	// PID is process group ID
	PID string
	// Hostname is the hostname where the process is running
	Hostname string
)
View Source
var BufferSize = 512

BufferSize is the initial allocated size of the buffers.

View Source
var Pool *sync.Pool

Pool of buffers to be used with formatter and commit functions.

View Source
var RecoverBufferStack = 4096

RecoverBufferStack amount of buffer to store the stack.

Functions

func Colors

func Colors(b bool)

Colors enables or disables log message coloring.

func CommitSd

func CommitSd(sl *Slog)

CommitSd send to systemd journal the log entry.

func DebugInfo

func DebugInfo()

DebugInfo enable debug information for all messages.

func Error

func Error(vals ...interface{})

Error logs an error.

func Errorf

func Errorf(str string, vals ...interface{})

Errorf logs an error formated.

func Errorln

func Errorln(vals ...interface{})

Errorln logs an error.

func Exiter

func Exiter(fn func(int)) error

Exiter configures a function that will be called to exit the app.

func FallbackFormater

func FallbackFormater(sl *Slog) ([]byte, error)

FallbackFormater is called if systemd isn't available. Need to set Writter in Slog struct.

func Fatal

func Fatal(vals ...interface{})

Fatal print a log entry to the destine and exit with 1.

func Fatalf

func Fatalf(s string, vals ...interface{})

Fatalf print a formated log entry to the destine and exit with 1.

func Fatalln

func Fatalln(vals ...interface{})

Fatalln print a log entry to the destine and exit with 1.

func FormatTime

func FormatTime(buf *[]byte, t time.Time)

FormatTime is a sample function for format the data and time.

func GoPanic

func GoPanic(r interface{}, stack []byte, cont bool)

GoPanic logs a panic.

func Itoa

func Itoa(buf *[]byte, i int, wid int)

Itoa converts a int to a byte. i is the interger to be converted, buf is a pointer to the buffer that will receive the converted interger and wid is the number of digits, if the digits is less than wid it will be filled with zeros. This function come from std.

func JSON

func JSON(l *Slog) ([]byte, error)

JSON convert a log entry to json.

func NoDebugInfo

func NoDebugInfo()

NoDebugInfo disable debug impormation for all messages.

func Panic

func Panic(vals ...interface{})

Panic print a log entry to the destine and call panic

func Panicf

func Panicf(s string, vals ...interface{})

Panicf print a formated log entry to the destine and call panic.

func Panicln

func Panicln(vals ...interface{})

Panicln print a log entry to the destine and call panic.

func Print

func Print(vals ...interface{})

Print prints a log entry to the destine, this is determined by the commit function.

func Printf

func Printf(str string, vals ...interface{})

Printf prints a formated log entry to the destine.

func Println

func Println(vals ...interface{})

Println prints a log entry to the destine.

func Prior2Sd

func Prior2Sd(level Level) systemd.Priority

Prior2Sd convert the priority from slog to systemd.

func Recover

func Recover(notexit bool)

Recover from panic and log the stack. If notexit is false, call l.Exiter(1), if not continue.

func SdFormater

func SdFormater(sl *Slog) ([]byte, error)

SdFormater format the mensagem for systemd journal

func SetLevel

func SetLevel(level Level) error

SetLevel set the level to filter log entries.

func SetOutput

func SetOutput(domain string, level Level, w io.WriteCloser, commiter func(sl *Slog), formatter func(l *Slog) ([]byte, error), nl int) error

SetOutput sets the commit out put to w.

func Testing

func Testing(t bool)

Testing enable testing in an environment without systemd.

Types

type Level

type Level uint8

Level type represents a log level.

const (
	ProtoPrio Level = iota + 1 //More priority
	DebugPrio
	InfoPrio
	ErrorPrio
	FatalPrio
	PanicPrio
	NoPrio //Less priority
)

This constants defines the levels available to the logger.

func ParseLevel

func ParseLevel(level string) (Level, error)

ParseLevel parses the string form of a level to the type Level.

func (Level) Byte

func (l Level) Byte() []byte

Byte returns the byte representations of a level.

func (Level) Color

func (l Level) Color(au aurora.Aurora) func(interface{}) aurora.Value

Color return a function to color the message based in the log level.

func (Level) String

func (l Level) String() string

String returns a string representation of a level.

type Log

type Log struct {
	Domain    []byte
	Priority  Level
	Timestamp time.Time
	Tags      *tags

	DiLevel int
	DoDi    bool
	// contains filtered or unexported fields
}

Log is a simple log entry.

func (*Log) Coloring

func (l *Log) Coloring(au aurora.Aurora) string

Coloring color the message.

func (*Log) FormatMessage

func (l *Log) FormatMessage() string

FormatMessage simple format the message without color.

func (*Log) Message

func (l *Log) Message(str string)

Message sets the log message.

func (*Log) SetTimeZone

func (l *Log) SetTimeZone()

SetTimeZone sets the zone info from time.Now()

func (*Log) String

func (l *Log) String() string

String print the Log struct contents.

type Logger

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

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

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

Logger defines a interface to the basic logger functions.

type Slog

type Slog struct {
	// Level is the max log level that will filter the entries.
	Level Level
	// Filter filter the log entry. If return true the log entry pass the filter.
	Filter func(l *Slog) bool
	// Format a readable message from the log information
	Formatter func(l *Slog) ([]byte, error)
	// Commit sent entry to somewhere.
	Commit func(l *Slog)
	// Writer can be a destiny in the Commit function.
	Writter io.WriteCloser
	// Log entry.
	Log *Log
	// Exiter is the function called on Fatal and Panic methods.
	Exiter func(int)

	Lck *sync.Mutex
	Cp  bool
	// contains filtered or unexported fields
}

Slog is the logger.

func DebugLevel

func DebugLevel() *Slog

DebugLevel set the log level to debug

func DefaultLogger

func DefaultLogger() *Slog

DefaultLogger return the default logger. Mainly to be used with Writer interface.

func Di

func Di() *Slog

Di add debug information to the log message.

func ErrorLevel

func ErrorLevel() *Slog

ErrorLevel set the log level to error

func InfoLevel

func InfoLevel() *Slog

InfoLevel set the log level to info

func NoDi

func NoDi() *Slog

NoDi disable debug information for this log entry.

func ProtoLevel

func ProtoLevel() *Slog

ProtoLevel set the log level to protocol

func Tag

func Tag(tags ...string) *Slog

Tag attach tags to the log entry

func (*Slog) Close

func (l *Slog) Close() error

Close the logger.

func (*Slog) Colors

func (l *Slog) Colors(b bool) *Slog

Colors enable or disable coloring of messages in log.

func (*Slog) DebugLevel

func (l *Slog) DebugLevel() *Slog

DebugLevel set the log level to debug

func (*Slog) Di

func (l *Slog) Di() *Slog

Di add debug information to the log entry.

func (*Slog) Error

func (l *Slog) Error(v ...interface{})

Error logs an error.

func (*Slog) ErrorLevel

func (l *Slog) ErrorLevel() *Slog

ErrorLevel set the log level to error

func (*Slog) Errorf

func (l *Slog) Errorf(s string, v ...interface{})

Errorf logs an error with format.

func (*Slog) Errorln

func (l *Slog) Errorln(v ...interface{})

Errorln logs an error.

func (*Slog) Fatal

func (l *Slog) Fatal(v ...interface{})

Fatal print a log entry to the destine and exit with 1.

func (*Slog) Fatalf

func (l *Slog) Fatalf(s string, v ...interface{})

Fatalf print a formated log entry to the destine and exit with 1.

func (*Slog) Fatalln

func (l *Slog) Fatalln(v ...interface{})

Fatalln print a log entry to the destine and exit with 1.

func (*Slog) GoPanic

func (l *Slog) GoPanic(r interface{}, stack []byte, cont bool)

GoPanic is use when recover from a panic and the panic must be logged

func (*Slog) InfoLevel

func (l *Slog) InfoLevel() *Slog

InfoLevel set the log level to info

func (*Slog) Init

func (l *Slog) Init(domain string, nl int) error

Init initializes the logger with domain and numLogs. numLogs is the number of Slog struct in the pool.

func (*Slog) MakeDefault

func (l *Slog) MakeDefault() *Slog

MakeDefault turn the behavior of actual chain of functions into default to be used in the next chain.

func (*Slog) NoDi

func (l *Slog) NoDi() *Slog

NoDi disable debug info.

func (*Slog) Panic

func (l *Slog) Panic(v ...interface{})

Panic print a log entry to the destine and call panic.

func (*Slog) Panicf

func (l *Slog) Panicf(s string, v ...interface{})

Panicf print a formated log entry to the destine and call panic.

func (*Slog) Panicln

func (l *Slog) Panicln(v ...interface{})

Panicln print a log entry to the destine and call panic.

func (*Slog) Print

func (l *Slog) Print(v ...interface{})

Print prints a log entry to the destine, this is determined by the commit function.

func (*Slog) Printf

func (l *Slog) Printf(s string, v ...interface{})

Printf prints a formated log entry to the destine.

func (*Slog) Println

func (l *Slog) Println(v ...interface{})

Println prints a log entry to the destine.

func (*Slog) ProtoLevel

func (l *Slog) ProtoLevel() *Slog

ProtoLevel set the log level to protocol

func (*Slog) SetLevel

func (l *Slog) SetLevel(level Level) *Slog

SetLevel set the level to filter log entries.

func (*Slog) Tag

func (l *Slog) Tag(tags ...string) *Slog

Tag add tags to the log entry.

func (*Slog) Write

func (l *Slog) Write(p []byte) (n int, err error)

Directories

Path Synopsis
Package journal provides write bindings to the local systemd journal.
Package journal provides write bindings to the local systemd journal.

Jump to

Keyboard shortcuts

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