gosteno

package module
v0.0.0-...-0c74832 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2016 License: Apache-2.0 Imports: 8 Imported by: 0

README

go-steno

Travis Build

Implementation of ArpNetworking's LogbackSteno for Go. Extends Sirupsen's logrus logging implementation with a Steno compatible Formatter as well as providing named Logger instances and a fluent log builder.

Dependency

First, retrieve the library into your workspace:

go> go get github.com/vjkoskela/gosteno

To use the library in your project(s) simply import it:

import "github.com/vjkoskela/gosteno"

Formatter

Create the formatter as follows:

var formatter *gosteno.Formatter = gosteno.NewFormatter()

The gosteno.Formatter supports a subset of the options available in LogbackSteno:

  • LogEventName - Set the default event name. The default is "log".
  • InjectContextProcess - Add the process identifier to the context block. The default is true.
  • InjectContextHost - Add the host name to the context block. The default is true.
  • InjectContextLogger - Add the logger name to the context block. The default is false. (1)

Note 1: Injecting additional key-value pairs into context is not strictly compliant with the current definition of Steno.

These may be configured after instantiating the Formatter. For example:

formatter.SetInjectContextLogger(true)

Logrus

The underlying logging implementation is logrus. At minimum you must set the formatter; however, you may also want to configure the writer and minimum output level. For example to configure the global logger instance:

logrus.SetOutput(os.Stdout)
logrus.SetFormatter(formatter)
logrus.SetLevel(logrus.DebugLevel)

Alternatively, you may create a new logrus logger and configure that. For example:

var logrusLogger *logrus.Logger = &logrus.Logger{
    Out: os.Stdout,
    Formatter: formatter,
    Level: logrus.ErrorLevel,
}

Logger

Instantiate a named Logger instance for each particular logging context. By default the Logger is bound to the global default logrus logger instance. For example:

var logger *gosteno.Logger = gosteno.GetLogger("http.server")

Alternatively, you may bind a Logger instance to particular logrus logger instance:


var logger *gosteno.Logger = gosteno.GetLoggerForLogger("http.server", logrusLogger)

Log Builder

Aside from providing a full set of logging methods compatible with Go's standard logging and with the logrus logger, gosteno.Logger also provides access to buildable logs:

logger.DebugBuilder().SetMessage("This is a log builder debug message").Log()

logger.InfoBuilder().SetEvent("my_event").SetMessage("This is a log builder info message with event").Log()

logger.WarnBuilder().
        SetEvent("my_event").
        SetMessage("This is a warn builder info message with event and error").
        SetError(errors.New("This is also another error")).
        Log()

logger.ErrorBuilder().
        SetEvent("my_event").
        SetMessage("This is a log builder info message with event, error, data and context").
        SetError(errors.New("This is also another error")).
        AddContext("requestId", uuid.New()).
        AddData("userId", uuid.New()).
        Log()

This produces output like this:

{"time":"2016-01-08T17:45:35.895560313-08:00","name":"log","level":"debug","data":{"message":"This is a log builder debug message"},"context":{"host":"Mac-Pro.local","processId":"16358","logger":"examples.main"},"id":"e4c0f58d-74c1-425e-8f8c-017f03bc0171","version":"0"}
{"time":"2016-01-08T17:45:35.895584789-08:00","name":"my_event","level":"info","data":{"message":"This is a log builder info message with event"},"context":{"host":"Mac-Pro.local","processId":"16358","logger":"examples.main"},"id":"5f7acb89-c498-4b30-b0d4-248de0d8e060","version":"0"}
{"time":"2016-01-08T17:45:35.895611498-08:00","name":"my_event","level":"warn","data":{"message":"This is a warn builder info message with event and error"},"context":{"host":"Mac-Pro.local","processId":"16358","logger":"examples.main"},"exception":{"type":"error","message":"This is also another error","backtrace":[]},"id":"b75fd67c-2831-4aff-8dff-277393da6eed","version":"0"}
{"time":"2016-01-08T17:45:35.895643617-08:00","name":"my_event","level":"crit","data":{"message":"This is a log builder info message with event, error, data and context","userId":"bb486dfd-d7c5-4e3f-8391-c39d9fee6cac"},"context":{"requestId":"3186ea94-bca3-4a75-8ba2-b01151e9935c","host":"Mac-Pro.local","processId":"16358","logger":"examples.main"},"exception":{"type":"error","message":"This is also another error","backtrace":[]},"id":"67c13e4d-12de-4ae4-8606-271d6e4ae13f","version":"0"}

For more examples please see performance.go.

Performance

Some very non-scientific relative benchmarking was performed against the Arpnetworking LogbackSteno Java implementation.

Go with error, mind you it's effectively just a string with no stack trace:

Elapsed 2.236446 seconds
Elapsed 2.198458 seconds
Elapsed 2.356763 seconds
Elapsed 2.196019 seconds
Elapsed 2.206734 seconds

Average = 2.238884 seconds

Java with exception for error, which includes the message and a stack trace:

Elapsed 10.815678 seconds
Elapsed 11.896151 seconds
Elapsed 10.839127 seconds
Elapsed 11.803035 seconds
Elapsed 10.903178 seconds

Average = 11.2514338 seconds

Java without exception for error:

Elapsed 2.790097 seconds
Elapsed 1.639426 seconds
Elapsed 1.470144 seconds
Elapsed 1.612253 seconds
Elapsed 1.575005 seconds

Average = 1.817385 seconds

The gosteno implementation is 5.025 times faster when exceptions are logged in Java and 1.232 times slower when they are not. The cost of stack trace generation and encoding in Java is not surprising; however, the scale of the improvement with it disabled is. Finally, although this is neither a complete nor rigorous performance test it does provide an interesting baseline. The next step would be to compare serialization times including complex data structures as that will simulate real world usage more closely.

Details:

  • JDK/JRE version 1.8.0_66
  • GO version 1.5.2
  • 3.5 Ghz 6-Core Intel Xeon E5
  • 32 GB 1866 Mhz DDR ECC
  • Mac OS X with El Capitan
  • Loggers configured to stdout redirected in bash to /dev/null

Development

To build the library locally you must satisfy these prerequisites:

Next, fork the repository, get and build:

Getting and Building:

go> go get github.com/$USER/gosteno
go> go install github.com/$USER/gosteno

Testing:

go> go test -coverprofile=coverage.out github.com/$USER/gosteno
go> go tool cover -html=coverage.out

To use the local forked version in your project simply import it:

import "github.com/$USER/gosteno"

Note: The above assumes $USER is the name of your Github organization containing the fork.

License

Published under Apache Software License 2.0, see LICENSE

© Ville Koskela, 2016

Documentation

Overview

Copyright 2016 Ville Koskela

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	EVENT_DATA_EVENT_KEY   string = "event"
	EVENT_DATA_LOGGER_KEY  string = "logger"
	EVENT_DATA_DATA_KEY    string = "data"
	EVENT_DATA_CONTEXT_KEY string = "context"
	EVENT_DATA_ERROR_KEY   string = "error"
)
View Source
const (
	// The field name containing a descriptor of the field format.
	MarkerKey string = "__gosteno.marker__"
)

Variables

This section is empty.

Functions

func Flags

func Flags() int

Flags from standard Go log library. This is a no-op. Provided for compatibility.

func Output

func Output(calldepth int, s string) error

Output from standard Go log library. This is a no-op. Provided for compatibility.

func Prefix

func Prefix() string

Prefix from standard Go log library. This is a no-op. Provided for compatibility.

func SetFlags

func SetFlags(flag int)

SetFlags from standard Go log library. This is a no-op. Provided for compatibility.

func SetOutput

func SetOutput(w io.Writer)

SetOutput from standard Go log library. This is a no-op. Provided for compatibility.

func SetPrefix

func SetPrefix(prefix string)

SetPrefix from standard Go log library. This is a no-op. Provided for compatibility.

Types

type DefaultLogBuilder

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

DefaultLogBuilder is the default LogBuilder implementation that satisfies the LogBuilder contract.

func NewDefaultLogBuilder

func NewDefaultLogBuilder(l *logrus.Logger, v logrus.Level, n string) *DefaultLogBuilder

func (*DefaultLogBuilder) AddContext

func (dlb *DefaultLogBuilder) AddContext(key string, value interface{}) LogBuilder

func (*DefaultLogBuilder) AddData

func (dlb *DefaultLogBuilder) AddData(key string, value interface{}) LogBuilder

func (*DefaultLogBuilder) Log

func (dlb *DefaultLogBuilder) Log()

func (*DefaultLogBuilder) SetError

func (dlb *DefaultLogBuilder) SetError(err error) LogBuilder

func (*DefaultLogBuilder) SetEvent

func (dlb *DefaultLogBuilder) SetEvent(event string) LogBuilder

func (*DefaultLogBuilder) SetMessage

func (dlb *DefaultLogBuilder) SetMessage(message string) LogBuilder

type Formatter

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

func NewFormatter

func NewFormatter() *Formatter

func (*Formatter) Format

func (sf *Formatter) Format(e *logrus.Entry) (result []byte, err error)

func (*Formatter) InjectContextHost

func (sf *Formatter) InjectContextHost() bool

func (*Formatter) InjectContextLogger

func (sf *Formatter) InjectContextLogger() bool

func (*Formatter) InjectContextProcess

func (sf *Formatter) InjectContextProcess() bool

func (*Formatter) LogEventName

func (sf *Formatter) LogEventName() string

func (*Formatter) SetInjectContextHost

func (sf *Formatter) SetInjectContextHost(v bool)

func (*Formatter) SetInjectContextLogger

func (sf *Formatter) SetInjectContextLogger(v bool)

func (*Formatter) SetInjectContextProcess

func (sf *Formatter) SetInjectContextProcess(v bool)

func (*Formatter) SetLogEventName

func (sf *Formatter) SetLogEventName(v string)

type LogBuilder

type LogBuilder interface {

	// Event setter.
	SetEvent(string) LogBuilder

	// Message setter.
	SetMessage(string) LogBuilder

	// Error setter.
	SetError(error) LogBuilder

	// Data adder.
	AddData(string, interface{}) LogBuilder

	// Context adder.
	AddContext(string, interface{}) LogBuilder

	// Log message.
	Log()
}

LogBuilder interface for assembling log messages.

type Logger

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

Logger implementation. Interface compatible with standard Go log and github.com/Sirupsen/logrus. However, the power of the implementation is in the methods returning LogBuilder instances. The "Print" methods are mapped to the info level. As with other implementations fatal will exit and panic will halt.

func GetLogger

func GetLogger(loggerName string) *Logger

func GetLoggerForLogger

func GetLoggerForLogger(loggerName string, logger *logrus.Logger) *Logger

func NewLogger

func NewLogger(n string, l *logrus.Logger) *Logger

func (*Logger) Debug

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

Debug from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) DebugBuilder

func (l *Logger) DebugBuilder() LogBuilder

Debug with LogBuilder. Recommended.

func (*Logger) Debugf

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

Debugf from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) Debugln

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

Debugln from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) Error

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

Error from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) ErrorBuilder

func (l *Logger) ErrorBuilder() LogBuilder

Error with LogBuilder. Recommended.

func (*Logger) Errorf

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

Errorf from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) Errorln

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

Errorln from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) Fatal

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

Fatal from standard Go log library. This implementation like the standard library causes the program to exit. Provided for compatibility.

func (*Logger) FatalBuilder

func (l *Logger) FatalBuilder() LogBuilder

Fatal with LogBuilder. Recommended. This implementation like the standard library causes the program to exit.

func (*Logger) Fatalf

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

Fatalf from standard Go log library. This implementation like the standard library causes the program to exit. Provided for compatibility.

func (*Logger) Fatalln

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

Fatalln from standard Go log library. This implementation like the standard library causes the program to exit. Provided for compatibility.

func (*Logger) Flags

func (l *Logger) Flags() int

Flags from standard Go log library. This is a no-op. Provided for compatibility.

func (*Logger) Info

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

Info from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) InfoBuilder

func (l *Logger) InfoBuilder() LogBuilder

Info with LogBuilder. Recommended.

func (*Logger) Infof

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

Infof from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) Infoln

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

Infoln from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) Output

func (l *Logger) Output(calldepth int, s string) error

Output from standard Go log library. This is mapped to Info without the stack trace. Provided for compatibility.

func (*Logger) Panic

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

Panic from standard Go log library. This implementation like the standard library causes the program to panic. Provided for compatibility.

func (*Logger) PanicBuilder

func (l *Logger) PanicBuilder() LogBuilder

Panic with LogBuilder. Recommended. This implementation like the standard library causes the program to panic.

func (*Logger) Panicf

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

Panicf from standard Go log library. This implementation like the standard library causes the program to panic. Provided for compatibility.

func (*Logger) Panicln

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

Panicln from standard Go log library. This implementation like the standard library causes the program to panic. Provided for compatibility.

func (*Logger) Prefix

func (l *Logger) Prefix() string

Prefix from standard Go log library. This is a no-op. Provided for compatibility.

func (*Logger) Print

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

Print from standard Go log library. Provided for compatibility.

func (*Logger) Printf

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

Printf from standard Go log library. Provided for compatibility.

func (*Logger) Println

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

Println from standard Go log library. Provided for compatibility.

func (*Logger) SetFlags

func (l *Logger) SetFlags(flag int)

SetFlags from standard Go log library. This is a no-op. Provided for compatibility.

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

SetPrefix from standard Go log library. This is a no-op. Provided for compatibility.

func (*Logger) Warn

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

Warn from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) WarnBuilder

func (l *Logger) WarnBuilder() LogBuilder

Warn with LogBuilder. Recommended.

func (*Logger) Warnf

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

Warnf from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) Warning

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

Warning from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) WarningBuilder

func (l *Logger) WarningBuilder() LogBuilder

Warning with LogBuilder. Recommended.

func (*Logger) Warningf

func (l *Logger) Warningf(format string, args ...interface{})

Warningf from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) Warningln

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

Warningln from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) Warnln

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

Warnln from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) WithError

func (l *Logger) WithError(err error) *logrus.Entry

WithError from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) *logrus.Entry

WithField from github.com/Sirupsen/logrus library. Provided for compatibility.

func (*Logger) WithFields

func (l *Logger) WithFields(fields logrus.Fields) *logrus.Entry

WithFields from github.com/Sirupsen/logrus library. Provided for compatibility.

type MapsMarker

type MapsMarker struct{}

Maps marker implementation.

var (
	// The marker for steno as maps format descriptor.
	MarkerMaps *MapsMarker = new(MapsMarker)
)

func (*MapsMarker) Encode

func (smm *MapsMarker) Encode(
	logger *logrus.Logger,
	event string,
	loggerName string,
	data map[string]interface{},
	context map[string]interface{},
	err error) *logrus.Entry

Encode.

func (*MapsMarker) ParseContext

func (smm *MapsMarker) ParseContext(e *logrus.Entry) map[string]interface{}

Parse context from event.

func (*MapsMarker) ParseData

func (smm *MapsMarker) ParseData(e *logrus.Entry) map[string]interface{}

Parse data from event.

func (*MapsMarker) ParseError

func (smm *MapsMarker) ParseError(e *logrus.Entry) error

Parse data from event.

func (*MapsMarker) ParseEvent

func (smm *MapsMarker) ParseEvent(e *logrus.Entry) string

Parse event name from event.

func (*MapsMarker) ParseLoggerName

func (smm *MapsMarker) ParseLoggerName(e *logrus.Entry) string

Parse logger name from event.

type Marker

type Marker interface {

	// Parse event name from event.
	ParseEvent(e *logrus.Entry) string

	// Parse logger from event.
	ParseLoggerName(e *logrus.Entry) string

	// Parse data from event
	ParseData(e *logrus.Entry) map[string]interface{}

	// Parse context from event
	ParseContext(e *logrus.Entry) map[string]interface{}

	// Parse error from event
	ParseError(e *logrus.Entry) error
}

Marker interface.

type NoOpLogBuilder

type NoOpLogBuilder struct {
}

NoOpLogBuilder is a LogBuilder implementation that satisfies the LogBuilder contract without empty implementations.

func (*NoOpLogBuilder) AddContext

func (nolb *NoOpLogBuilder) AddContext(key string, value interface{}) LogBuilder

func (*NoOpLogBuilder) AddData

func (nolb *NoOpLogBuilder) AddData(key string, value interface{}) LogBuilder

func (*NoOpLogBuilder) Log

func (nolb *NoOpLogBuilder) Log()

func (*NoOpLogBuilder) SetError

func (nolb *NoOpLogBuilder) SetError(err error) LogBuilder

func (*NoOpLogBuilder) SetEvent

func (nolb *NoOpLogBuilder) SetEvent(event string) LogBuilder

func (*NoOpLogBuilder) SetMessage

func (nolb *NoOpLogBuilder) SetMessage(message string) LogBuilder

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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