xlog

package module
v0.0.0-...-131980f Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2017 License: MIT Imports: 20 Imported by: 107

README

Check zerolog, the successor of xlog.

HTTP Handler Logger

godoc license Build Status Coverage

xlog is a logger for net/context aware HTTP applications.

Unlike most loggers, xlog will never block your application because one its outputs is lagging. The log commands are connected to their outputs through a buffered channel and will prefer to discard messages if the buffer get full. All message formatting, serialization and transport happen in a dedicated go routine.

Read more about xlog on Dailymotion engineering blog.

Features

  • Per request log context
  • Per request and/or per message key/value fields
  • Log levels (Debug, Info, Warn, Error)
  • Color output when terminal is detected
  • Custom output (JSON, logfmt, …)
  • Automatic gathering of request context like User-Agent, IP etc.
  • Drops message rather than blocking execution
  • Easy access logging thru github.com/rs/xaccess

Works with both Go 1.7+ (with net/context support) and Go 1.6 if used with github.com/rs/xhandler.

Install

go get github.com/rs/xlog

Usage

c := alice.New()

host, _ := os.Hostname()
conf := xlog.Config{
    // Log info level and higher
    Level: xlog.LevelInfo,
    // Set some global env fields
    Fields: xlog.F{
        "role": "my-service",
        "host": host,
    },
    // Output everything on console
    Output: xlog.NewOutputChannel(xlog.NewConsoleOutput()),
}

// Install the logger handler
c = c.Append(xlog.NewHandler(conf))

// Optionally plug the xlog handler's input to Go's default logger
log.SetFlags(0)
xlogger := xlog.New(conf)
log.SetOutput(xlogger)

// Install some provided extra handler to set some request's context fields.
// Thanks to those handler, all our logs will come with some pre-populated fields.
c = c.Append(xlog.MethodHandler("method"))
c = c.Append(xlog.URLHandler("url"))
c = c.Append(xlog.RemoteAddrHandler("ip"))
c = c.Append(xlog.UserAgentHandler("user_agent"))
c = c.Append(xlog.RefererHandler("referer"))
c = c.Append(xlog.RequestIDHandler("req_id", "Request-Id"))

// Here is your final handler
h := c.Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    // Get the logger from the request's context. You can safely assume it
    // will be always there: if the handler is removed, xlog.FromContext
    // will return a NopLogger
    l := xlog.FromRequest(r)

    // Then log some errors
    if err := errors.New("some error from elsewhere"); err != nil {
        l.Errorf("Here is an error: %v", err)
    }

    // Or some info with fields
    l.Info("Something happend", xlog.F{
        "user":   "current user id",
        "status": "ok",
    })
    // Output:
    // {
    //   "message": "Something happend",
    //   "level": "info",
    //   "file": "main.go:34",
    //   "time": time.Time{...},
    //   "user": "current user id",
    //   "status": "ok",
    //   "ip": "1.2.3.4",
    //   "user-agent": "Mozilla/1.2.3...",
    //   "referer": "http://somewhere.com/path",
    //   "role": "my-service",
    //   "host": "somehost"
    // }
}))
http.Handle("/", h)

if err := http.ListenAndServe(":8080", nil); err != nil {
    xlogger.Fatal(err)
}
Copy Logger

You may want to get a copy of the current logger to pass a modified version to a function without touching the original:

l := xlog.FromContext(ctx)
l2 := xlog.Copy(l)
l2.SetField("foo", "bar")

Make sure you copy a request context logger if you plan to use it in a go routine that may still exist after the end of the current request. Contextual loggers are reused after each requests to lower the pressure on the garbage collector. If you would use such a logger in a go routine, you may end up using a logger from another request/context or worse, a nil pointer:

l := xlog.FromContext(ctx)
l2 := xlog.Copy(l)
go func() {
    // use the safe copy
    l2.Info("something")
}()
Global Logger

You may use the standard Go logger and plug xlog as it's output as xlog implements io.Writer:

xlogger := xlog.New(conf)
log.SetOutput(xlogger)

This has the advantage to make all your existing code or libraries already using Go's standard logger to use xlog with no change. The drawback though, is that you won't have control on the logging level and won't be able to add custom fields (other than ones set on the logger itself via configuration or SetFields()) for those messages.

Another option for code you manage but which is outside of a HTTP request handler is to use the xlog provided default logger:

xlog.Debugf("some message with %s", variable, xlog.F{"and": "field support"})

This way you have access to all the possibilities offered by xlog without having to carry the logger instance around. The default global logger has no fields set and has its output set to the console with no buffering channel. You may want to change that using the xlog.SetLogger() method:

xlog.SetLogger(xlog.New(xlog.Config{
    Level: xlog.LevelInfo,
    Output: xlog.NewConsoleOutput(),
    Fields: xlog.F{
        "role": "my-service",
    },
}))
Configure Output

By default, output is setup to output debug and info message on STDOUT and warning and errors to STDERR. You can easily change this setup.

XLog output can be customized using composable output handlers. Thanks to the LevelOutput, MultiOutput and FilterOutput, it is easy to route messages precisely.

conf := xlog.Config{
    Output: xlog.NewOutputChannel(xlog.MultiOutput{
        // Send all logs with field type=mymodule to a remote syslog
        0: xlog.FilterOutput{
            Cond: func(fields map[string]interface{}) bool {
                return fields["type"] == "mymodule"
            },
            Output: xlog.NewSyslogOutput("tcp", "1.2.3.4:1234", "mymodule"),
        },
        // Setup different output per log level
        1: xlog.LevelOutput{
            // Send errors to the console
            Error: xlog.NewConsoleOutput(),
            // Send syslog output for error level
            Info: xlog.NewSyslogOutput("", "", ""),
        },
    }),
})

h = xlog.NewHandler(conf)
Built-in Output Modules
Name Description
OutputChannel Buffers messages before sending. This output should always be the output directly set to xlog's configuration.
MultiOutput Routes the same message to several outputs. If one or more outputs return error, the last error is returned.
FilterOutput Tests a condition on the message and forward it to the child output if true.
LevelOutput Routes messages per level outputs.
ConsoleOutput Prints messages in a human readable form on the stdout with color when supported. Fallback to logfmt output if the stdout isn't a terminal.
JSONOutput Serialize messages in JSON.
LogfmtOutput Serialize messages using Heroku like logfmt.
LogstashOutput Serialize JSON message using Logstash 2.0 (schema v1) structured format.
SyslogOutput Send messages to syslog.
UIDOutput Append a globally unique id to every message and forward it to the next output.

Third Party Extensions

Project Author Description
gRPClog Hugo González Labrador An adapter to use xlog as the logger for grpclog.
xlog-nsq Olivier Poitrey An xlog to NSQ output.
xlog-sentry trong An xlog to Sentry output.

Licenses

All source code is licensed under the MIT License.

Documentation

Overview

Package xlog is a logger coupled with HTTP net/context aware middleware.

Unlike most loggers, xlog will never block your application because one its outputs is lagging. The log commands are connected to their outputs through a buffered channel and will prefer to discard messages if the buffer get full. All message formatting, serialization and transport happen in a dedicated go routine.

Features:

  • Per request log context
  • Per request and/or per message key/value fields
  • Log levels (Debug, Info, Warn, Error)
  • Color output when terminal is detected
  • Custom output (JSON, logfmt, …)
  • Automatic gathering of request context like User-Agent, IP etc.
  • Drops message rather than blocking execution
  • Easy access logging thru github.com/rs/xaccess

It works best in combination with github.com/rs/xhandler.

Example (CombinedOutputs)
package main

import (
	"github.com/rs/xlog"
)

func main() {
	conf := xlog.Config{
		Output: xlog.NewOutputChannel(xlog.MultiOutput{
			// Output interesting messages to console
			0: xlog.FilterOutput{
				Cond: func(fields map[string]interface{}) bool {
					val, found := fields["type"]
					return found && val == "interesting"
				},
				Output: xlog.NewConsoleOutput(),
			},
			// Also setup by-level loggers
			1: xlog.LevelOutput{
				// Send debug messages to console if they match type
				Debug: xlog.FilterOutput{
					Cond: func(fields map[string]interface{}) bool {
						val, found := fields["type"]
						return found && val == "interesting"
					},
					Output: xlog.NewConsoleOutput(),
				},
			},
			// Also send everything over syslog
			2: xlog.NewSyslogOutput("", "", ""),
		}),
	}

	lh := xlog.NewHandler(conf)
	_ = lh
}
Output:

Example (Handler)
package main

import (
	"errors"
	"log"
	"net/http"
	"os"

	"github.com/justinas/alice"
	"github.com/rs/xlog"
)

func main() {
	c := alice.New()

	host, _ := os.Hostname()
	conf := xlog.Config{
		// Set some global env fields
		Fields: xlog.F{
			"role": "my-service",
			"host": host,
		},
	}

	// Install the logger handler with default output on the console
	c = c.Append(xlog.NewHandler(conf))

	// Plug the xlog handler's input to Go's default logger
	log.SetFlags(0)
	log.SetOutput(xlog.New(conf))

	// Install some provided extra handler to set some request's context fields.
	// Thanks to those handler, all our logs will come with some pre-populated fields.
	c = c.Append(xlog.RemoteAddrHandler("ip"))
	c = c.Append(xlog.UserAgentHandler("user_agent"))
	c = c.Append(xlog.RefererHandler("referer"))
	c = c.Append(xlog.RequestIDHandler("req_id", "Request-Id"))

	// Here is your final handler
	h := c.Then(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Get the logger from the request's context. You can safely assume it
		// will be always there: if the handler is removed, xlog.FromContext
		// will return a NopLogger
		l := xlog.FromRequest(r)

		// Then log some errors
		if err := errors.New("some error from elsewhere"); err != nil {
			l.Errorf("Here is an error: %v", err)
		}

		// Or some info with fields
		l.Info("Something happend", xlog.F{
			"user":   "current user id",
			"status": "ok",
		})
	}))
	http.Handle("/", h)

	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.SetOutput(os.Stderr) // make sure we print to console
		log.Fatal(err)
	}
}
Output:

Example (Log)
package main

import (
	"context"
	"errors"

	"github.com/rs/xlog"
)

func main() {
	ctx := context.TODO()
	l := xlog.FromContext(ctx)

	// Log a simple message
	l.Debug("message")

	if err := errors.New("some error"); err != nil {
		l.Errorf("Some error happened: %v", err)
	}

	// With optional fields
	l.Debugf("foo %s", "bar", xlog.F{
		"field": "value",
	})
}
Output:

Example (Stdlog)
package main

import (
	"log"

	"github.com/rs/xlog"
)

func main() {
	// Define logger conf
	conf := xlog.Config{
		Output: xlog.NewConsoleOutput(),
	}

	// Remove timestamp and other decorations of the std logger
	log.SetFlags(0)

	// Plug a xlog instance to Go's std logger
	log.SetOutput(xlog.New(conf))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	KeyTime    = "time"
	KeyMessage = "message"
	KeyLevel   = "level"
	KeyFile    = "file"
)

Common field names for log messages.

View Source
var Discard = OutputFunc(func(fields map[string]interface{}) error {
	return nil
})

Discard is an Output that discards all log message going thru it.

View Source
var ErrBufferFull = errors.New("buffer full")

ErrBufferFull is returned when the output channel buffer is full and messages are discarded.

View Source
var NopLogger = &nop{}

NopLogger is an no-op implementation of xlog.Logger

Functions

func Debug

func Debug(v ...interface{})

Debug calls the Debug() method on the default logger

func Debugf

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

Debugf calls the Debugf() method on the default logger

func Error

func Error(v ...interface{})

Error calls the Error() method on the default logger

func Errorf

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

Errorf calls the Errorf() method on the default logger

Go vet users: you may append %v at the end of you format when using xlog.F{} as a last argument to workaround go vet false alarm.

func Fatal

func Fatal(v ...interface{})

Fatal calls the Fatal() method on the default logger

func Fatalf

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

Fatalf calls the Fatalf() method on the default logger

Go vet users: you may append %v at the end of you format when using xlog.F{} as a last argument to workaround go vet false alarm.

func IDFromContext

func IDFromContext(ctx context.Context) (xid.ID, bool)

IDFromContext returns the unique id associated to the request if any.

func IDFromRequest

func IDFromRequest(r *http.Request) (xid.ID, bool)

IDFromRequest returns the unique id accociated to the request if any.

func Info

func Info(v ...interface{})

Info calls the Info() method on the default logger

func Infof

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

Infof calls the Infof() method on the default logger

func MethodHandler

func MethodHandler(name string) func(next http.Handler) http.Handler

MethodHandler returns a handler setting the request's method as a field to the current context's logger using the passed name as field name.

func NewContext

func NewContext(ctx context.Context, l Logger) context.Context

NewContext returns a copy of the parent context and associates it with the provided logger.

func NewHandler

func NewHandler(c Config) func(http.Handler) http.Handler

NewHandler instanciates a new xlog HTTP handler.

If not configured, the output is set to NewConsoleOutput() by default.

func NewSyslogWriter

func NewSyslogWriter(network, address string, prio syslog.Priority, tag string) io.Writer

NewSyslogWriter returns a writer ready to be used with output modules. If network and address are empty, Dial will connect to the local syslog server.

Example
package main

import (
	"log/syslog"

	"github.com/rs/xlog"
)

func main() {
	conf := xlog.Config{
		Output: xlog.NewOutputChannel(xlog.LevelOutput{
			Debug: xlog.NewLogstashOutput(xlog.NewSyslogWriter("", "", syslog.LOG_LOCAL0|syslog.LOG_DEBUG, "")),
			Info:  xlog.NewLogstashOutput(xlog.NewSyslogWriter("", "", syslog.LOG_LOCAL0|syslog.LOG_INFO, "")),
			Warn:  xlog.NewLogstashOutput(xlog.NewSyslogWriter("", "", syslog.LOG_LOCAL0|syslog.LOG_WARNING, "")),
			Error: xlog.NewLogstashOutput(xlog.NewSyslogWriter("", "", syslog.LOG_LOCAL0|syslog.LOG_ERR, "")),
		}),
	}

	lh := xlog.NewHandler(conf)
	_ = lh
}
Output:

func RefererHandler

func RefererHandler(name string) func(next http.Handler) http.Handler

RefererHandler returns a handler setting the request's referer header as a field to the current context's logger using the passed name as field name.

func RemoteAddrHandler

func RemoteAddrHandler(name string) func(next http.Handler) http.Handler

RemoteAddrHandler returns a handler setting the request's remote address as a field to the current context's logger using the passed name as field name.

func RequestHandler

func RequestHandler(name string) func(next http.Handler) http.Handler

RequestHandler returns a handler setting the request's method and URL as a field to the current context's logger using the passed name as field name.

func RequestIDHandler

func RequestIDHandler(name, headerName string) func(next http.Handler) http.Handler

RequestIDHandler returns a handler setting a unique id to the request which can be gathered using IDFromContext(ctx). This generated id is added as a field to the logger using the passed name as field name. The id is also added as a response header if the headerName is not empty.

The generated id is a URL safe base64 encoded mongo object-id-like unique id. Mongo unique id generation algorithm has been selected as a trade-off between size and ease of use: UUID is less space efficient and snowflake requires machine configuration.

func SetLogger

func SetLogger(logger Logger)

SetLogger changes the global logger instance

Example
package main

import (
	"github.com/rs/xlog"
)

func main() {
	xlog.SetLogger(xlog.New(xlog.Config{
		Level:  xlog.LevelInfo,
		Output: xlog.NewConsoleOutput(),
		Fields: xlog.F{
			"role": "my-service",
		},
	}))
}
Output:

func URLHandler

func URLHandler(name string) func(next http.Handler) http.Handler

URLHandler returns a handler setting the request's URL as a field to the current context's logger using the passed name as field name.

func UserAgentHandler

func UserAgentHandler(name string) func(next http.Handler) http.Handler

UserAgentHandler returns a handler setting the request's client's user-agent as a field to the current context's logger using the passed name as field name.

func Warn

func Warn(v ...interface{})

Warn calls the Warn() method on the default logger

func Warnf

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

Warnf calls the Warnf() method on the default logger

Types

type Config

type Config struct {
	// Level is the maximum level to output, logs with lower level are discarded.
	Level Level
	// Fields defines default fields to use with all messages.
	Fields map[string]interface{}
	// Output to use to write log messages to.
	//
	// You should always wrap your output with an OutputChannel otherwise your
	// logger will be connected to its output synchronously.
	Output Output
	// DisablePooling removes the use of a sync.Pool for cases where logger
	// instances are needed beyond the scope of a request handler. This option
	// puts a greater pressure on GC and increases the amount of memory allocated
	// and freed. Use only if persistent loggers are a requirement.
	DisablePooling bool
}

Config defines logger's configuration

type F

type F map[string]interface{}

F represents a set of log message fields

type FilterOutput

type FilterOutput struct {
	Cond   func(fields map[string]interface{}) bool
	Output Output
}

FilterOutput test a condition on the message and forward it to the child output if it returns true.

Example
package main

import (
	"github.com/rs/xlog"
)

func main() {
	conf := xlog.Config{
		Output: xlog.NewOutputChannel(xlog.FilterOutput{
			// Match messages containing a field type = interesting
			Cond: func(fields map[string]interface{}) bool {
				val, found := fields["type"]
				return found && val == "interesting"
			},
			// Output matching messages to the console
			Output: xlog.NewConsoleOutput(),
		}),
	}

	lh := xlog.NewHandler(conf)
	_ = lh
}
Output:

func (FilterOutput) Write

func (f FilterOutput) Write(fields map[string]interface{}) (err error)

type Level

type Level int

Level defines log levels

const (
	LevelDebug Level = iota
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
)

Log levels

func LevelFromString

func LevelFromString(t string) (Level, error)

LevelFromString returns the level based on its string representation

func (Level) MarshalText

func (l Level) MarshalText() ([]byte, error)

MarshalText lets Level implements the TextMarshaler interface used by encoding packages

func (Level) String

func (l Level) String() string

String returns the string representation of the level.

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(text []byte) (err error)

UnmarshalText lets Level implements the TextUnmarshaler interface used by encoding packages

type LevelOutput

type LevelOutput struct {
	Debug Output
	Info  Output
	Warn  Output
	Error Output
	Fatal Output
}

LevelOutput routes messages to different output based on the message's level.

Example
package main

import (
	"github.com/rs/xlog"
)

func main() {
	conf := xlog.Config{
		Output: xlog.NewOutputChannel(xlog.LevelOutput{
			// Send debug message to console
			Debug: xlog.NewConsoleOutput(),
			// and error messages to syslog
			Error: xlog.NewSyslogOutput("", "", ""),
			// other levels are discarded
		}),
	}

	lh := xlog.NewHandler(conf)
	_ = lh
}
Output:

func (LevelOutput) Write

func (l LevelOutput) Write(fields map[string]interface{}) error

type Logger

type Logger interface {
	// Implements io.Writer so it can be set a output of log.Logger
	io.Writer

	// SetField sets a field on the logger's context. All future messages on this logger
	// will have this field set.
	SetField(name string, value interface{})
	// GetFields returns all the fields set on the logger
	GetFields() F
	// Debug logs a debug message. If last parameter is a map[string]string, it's content
	// is added as fields to the message.
	Debug(v ...interface{})
	// Debug logs a debug message with format. If last parameter is a map[string]string,
	// it's content is added as fields to the message.
	Debugf(format string, v ...interface{})
	// Info logs a info message. If last parameter is a map[string]string, it's content
	// is added as fields to the message.
	Info(v ...interface{})
	// Info logs a info message with format. If last parameter is a map[string]string,
	// it's content is added as fields to the message.
	Infof(format string, v ...interface{})
	// Warn logs a warning message. If last parameter is a map[string]string, it's content
	// is added as fields to the message.
	Warn(v ...interface{})
	// Warn logs a warning message with format. If last parameter is a map[string]string,
	// it's content is added as fields to the message.
	Warnf(format string, v ...interface{})
	// Error logs an error message. If last parameter is a map[string]string, it's content
	// is added as fields to the message.
	Error(v ...interface{})
	// Error logs an error message with format. If last parameter is a map[string]string,
	// it's content is added as fields to the message.
	Errorf(format string, v ...interface{})
	// Fatal logs an error message followed by a call to os.Exit(1). If last parameter is a
	// map[string]string, it's content is added as fields to the message.
	Fatal(v ...interface{})
	// Fatalf logs an error message with format followed by a call to ox.Exit(1). If last
	// parameter is a map[string]string, it's content is added as fields to the message.
	Fatalf(format string, v ...interface{})
	// Output mimics std logger interface
	Output(calldepth int, s string) error
	// OutputF outputs message with fields.
	OutputF(level Level, calldepth int, msg string, fields map[string]interface{})
}

Logger defines the interface for a xlog compatible logger

func Copy

func Copy(l Logger) Logger

Copy returns a copy of the passed logger if the logger implements LoggerCopier or the NopLogger otherwise.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext gets the logger out of the context. If not logger is stored in the context, a NopLogger is returned.

func FromRequest

func FromRequest(r *http.Request) Logger

FromRequest gets the logger in the request's context. This is a shortcut for xlog.FromContext(r.Context())

func New

func New(c Config) Logger

New manually creates a logger.

This function should only be used out of a request. Use FromContext in request.

type LoggerCopier

type LoggerCopier interface {
	// Copy returns a copy of the logger
	Copy() Logger
}

LoggerCopier defines a logger with copy support

type MultiOutput

type MultiOutput []Output

MultiOutput routes the same message to serveral outputs. If one or more outputs return an error, the last error is returned.

Example
package main

import (
	"github.com/rs/xlog"
)

func main() {
	conf := xlog.Config{
		Output: xlog.NewOutputChannel(xlog.MultiOutput{
			// Output everything to console
			0: xlog.NewConsoleOutput(),
			// and also to local syslog
			1: xlog.NewSyslogOutput("", "", ""),
		}),
	}
	lh := xlog.NewHandler(conf)
	_ = lh
}
Output:

func (MultiOutput) Write

func (m MultiOutput) Write(fields map[string]interface{}) (err error)

type Output

type Output interface {
	Write(fields map[string]interface{}) error
}

Output sends a log message fields to a destination.

func NewConsoleOutput

func NewConsoleOutput() Output

NewConsoleOutput returns a Output printing message in a colored human readable form on the stderr. If the stderr is not on a terminal, a LogfmtOutput is returned instead.

func NewConsoleOutputW

func NewConsoleOutputW(w io.Writer, noTerm Output) Output

NewConsoleOutputW returns a Output printing message in a colored human readable form with the provided writer. If the writer is not on a terminal, the noTerm output is returned.

func NewJSONOutput

func NewJSONOutput(w io.Writer) Output

NewJSONOutput returns a new JSON output with the given writer.

func NewLogfmtOutput

func NewLogfmtOutput(w io.Writer) Output

NewLogfmtOutput returns a new output using logstash JSON schema v1

func NewLogstashOutput

func NewLogstashOutput(w io.Writer) Output

NewLogstashOutput returns an output to generate logstash friendly JSON format.

func NewSyslogOutput

func NewSyslogOutput(network, address, tag string) Output

NewSyslogOutput returns JSONOutputs in a LevelOutput with writers set to syslog with the proper priority added to a LOG_USER facility. If network and address are empty, Dial will connect to the local syslog server.

func NewSyslogOutputFacility

func NewSyslogOutputFacility(network, address, tag string, facility syslog.Priority) Output

NewSyslogOutputFacility returns JSONOutputs in a LevelOutput with writers set to syslog with the proper priority added to the passed facility. If network and address are empty, Dial will connect to the local syslog server.

func NewTrimFieldsOutput

func NewTrimFieldsOutput(trimFields []string, maxLen int, o Output) Output

NewTrimFieldsOutput trims listed field fields of type string with a value length greater than maxLen to maxLen.

func NewTrimOutput

func NewTrimOutput(maxLen int, o Output) Output

NewTrimOutput trims any field of type string with a value length greater than maxLen to maxLen.

func NewUIDOutput

func NewUIDOutput(field string, o Output) Output

NewUIDOutput returns an output filter adding a globally unique id (using github.com/rs/xid) to all message going thru this output. The o parameter defines the next output to pass data to.

type OutputChannel

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

OutputChannel is a send buffered channel between xlog and an Output.

func NewOutputChannel

func NewOutputChannel(o Output) *OutputChannel

NewOutputChannel creates a consumer buffered channel for the given output with a default buffer of 100 messages.

func NewOutputChannelBuffer

func NewOutputChannelBuffer(o Output, bufSize int) *OutputChannel

NewOutputChannelBuffer creates a consumer buffered channel for the given output with a customizable buffer size.

func (*OutputChannel) Close

func (oc *OutputChannel) Close()

Close closes the output channel and release the consumer's go routine.

func (*OutputChannel) Flush

func (oc *OutputChannel) Flush()

Flush flushes all the buffered message to the output

func (*OutputChannel) Write

func (oc *OutputChannel) Write(fields map[string]interface{}) (err error)

Write implements the Output interface

type OutputFunc

type OutputFunc func(fields map[string]interface{}) error

OutputFunc is an adapter to allow the use of ordinary functions as Output handlers. If it is a function with the appropriate signature, OutputFunc(f) is a Output object that calls f on Write().

func (OutputFunc) Write

func (of OutputFunc) Write(fields map[string]interface{}) error

type RecorderOutput

type RecorderOutput struct {
	Messages []F
}

RecorderOutput stores the raw messages in it's Messages field. This output is useful for testing.

func (*RecorderOutput) Reset

func (l *RecorderOutput) Reset()

Reset empty the output from stored messages

func (*RecorderOutput) Write

func (l *RecorderOutput) Write(fields map[string]interface{}) error

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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