Skip to content
Snippets Groups Projects
Unverified Commit 9acc1a62 authored by Silas Davis's avatar Silas Davis
Browse files

Make non blocking logger configurable and move wrapping to lifecycle


Signed-off-by: default avatarSilas Davis <silas@monax.io>
parent 5bf3a2cf
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,7 @@ import (
type LoggingConfig struct {
RootSink *SinkConfig `toml:",omitempty"`
ExcludeTrace bool
NonBlocking bool
}
// For encoding a top-level '[logging]' TOML table
......
......@@ -38,64 +38,56 @@ import (
// Obtain a logger from a LoggingConfig
func NewLoggerFromLoggingConfig(loggingConfig *config.LoggingConfig) (*logging.Logger, error) {
var logger *logging.Logger
var errCh channels.Channel
var err error
if loggingConfig == nil {
logger, errCh, err = NewStdErrLogger()
if err != nil {
return nil, err
}
return NewStdErrLogger()
} else {
outputLogger, err := loggerFromLoggingConfig(loggingConfig)
outputLogger, errCh, err := loggerFromLoggingConfig(loggingConfig)
if err != nil {
return nil, err
}
logger, errCh = NewLogger(outputLogger)
logger := NewLogger(outputLogger)
if loggingConfig.ExcludeTrace {
logger.Trace = kitlog.NewNopLogger()
}
go func() {
err := <-errCh.Out()
if err != nil {
fmt.Printf("Logging error: %v", err)
}
}()
return logger, nil
}
go func() {
err := <-errCh.Out()
if err != nil {
fmt.Printf("Logging error: %v", err)
}
}()
return logger, nil
}
// Hot swap logging config by replacing output loggers of passed InfoTraceLogger
// with those built from loggingConfig
func SwapOutputLoggersFromLoggingConfig(logger *logging.Logger, loggingConfig *config.LoggingConfig) error {
outputLogger, err := loggerFromLoggingConfig(loggingConfig)
func SwapOutputLoggersFromLoggingConfig(logger *logging.Logger, loggingConfig *config.LoggingConfig) (error, channels.Channel) {
outputLogger, errCh, err := loggerFromLoggingConfig(loggingConfig)
if err != nil {
return err
return err, channels.NewDeadChannel()
}
logger.SwapOutput(outputLogger)
return nil
return nil, errCh
}
func NewStdErrLogger() (*logging.Logger, channels.Channel, error) {
func NewStdErrLogger() (*logging.Logger, error) {
outputLogger, err := loggers.NewStreamLogger(os.Stderr, loggers.TerminalFormat)
if err != nil {
return nil, nil, err
return nil, err
}
logger, errCh := NewLogger(outputLogger)
return logger, errCh, nil
return NewLogger(outputLogger), nil
}
// Provided a standard logger that outputs to the supplied underlying outputLogger
func NewLogger(outputLogger kitlog.Logger) (*logging.Logger, channels.Channel) {
logger, errCh := logging.NewLogger(outputLogger)
func NewLogger(outputLogger kitlog.Logger) *logging.Logger {
logger := logging.NewLogger(outputLogger)
// Create a random ID based on start time
uuid, _ := simpleuuid.NewTime(time.Now())
var runId string
if uuid != nil {
runId = uuid.String()
}
return logger.With(structure.RunId, runId), errCh
return logger.With(structure.RunId, runId)
}
func JustLogger(logger *logging.Logger, _ channels.Channel) *logging.Logger {
......@@ -103,15 +95,19 @@ func JustLogger(logger *logging.Logger, _ channels.Channel) *logging.Logger {
}
func CaptureStdlibLogOutput(infoTraceLogger *logging.Logger) {
stdlib.CaptureRootLogger(infoTraceLogger.
With(structure.CapturedLoggingSourceKey, "stdlib_log"))
stdlib.CaptureRootLogger(infoTraceLogger.With(structure.CapturedLoggingSourceKey, "stdlib_log"))
}
// Helpers
func loggerFromLoggingConfig(loggingConfig *config.LoggingConfig) (kitlog.Logger, error) {
func loggerFromLoggingConfig(loggingConfig *config.LoggingConfig) (kitlog.Logger, channels.Channel, error) {
outputLogger, _, err := loggingConfig.RootSink.BuildLogger()
if err != nil {
return nil, err
return nil, nil, err
}
var errCh channels.Channel = channels.NewDeadChannel()
var logger kitlog.Logger = loggers.BurrowFormatLogger(outputLogger)
if loggingConfig.NonBlocking {
logger, errCh = loggers.NonBlockingLogger(logger)
}
return outputLogger, nil
return logger, errCh, err
}
......@@ -15,9 +15,7 @@
package logging
import (
"github.com/eapache/channels"
kitlog "github.com/go-kit/kit/log"
"github.com/hyperledger/burrow/logging/loggers"
"github.com/hyperledger/burrow/logging/structure"
)
......@@ -43,22 +41,21 @@ type Logger struct {
}
// Create an InfoTraceLogger by passing the initial outputLogger.
func NewLogger(outputLogger kitlog.Logger) (*Logger, channels.Channel) {
func NewLogger(outputLogger kitlog.Logger) *Logger {
// We will never halt the progress of a log emitter. If log output takes too
// long will start dropping log lines by using a ring buffer.
swapLogger := new(kitlog.SwapLogger)
swapLogger.Swap(outputLogger)
wrappedOutputLogger, errCh := wrapOutputLogger(swapLogger)
return &Logger{
Output: swapLogger,
// logging contexts
Info: kitlog.With(wrappedOutputLogger,
Info: kitlog.With(swapLogger,
structure.ChannelKey, structure.InfoChannelName,
),
Trace: kitlog.With(wrappedOutputLogger,
Trace: kitlog.With(swapLogger,
structure.ChannelKey, structure.TraceChannelName,
),
}, errCh
}
}
func NewNoopLogger() *Logger {
......@@ -140,10 +137,3 @@ func Msg(logger kitlog.Logger, message string, keyvals ...interface{}) error {
prepended := structure.CopyPrepend(keyvals, structure.MessageKey, message)
return logger.Log(prepended...)
}
// Wrap the output loggers with a a set of standard transforms, a non-blocking
// ChannelLogger and an outer context
func wrapOutputLogger(outputLogger kitlog.Logger) (kitlog.Logger, channels.Channel) {
return loggers.NonBlockingLogger(loggers.BurrowFormatLogger(outputLogger))
//return loggers.BurrowFormatLogger(outputLogger), channels.NewDeadChannel()
}
......@@ -23,7 +23,7 @@ import (
func TestLogger(t *testing.T) {
stderrLogger := kitlog.NewLogfmtLogger(os.Stderr)
logger, _ := NewLogger(stderrLogger)
logger := NewLogger(stderrLogger)
logger.Trace.Log("hello", "barry")
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment