lion

package module
v0.0.0-...-304b2f4 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2019 License: MIT Imports: 14 Imported by: 15

README

Deprecated: This library has not been maintained for a long time and there are better options available at this time. Please consider using a different logging library. If you must continue to use lion, fork it for your own use and maintenance.

Documentation

Overview

Package lion provides structured logging, configurable to use serialization protocols such as Protocol Buffers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLevel is the default Level.
	DefaultLevel = LevelInfo
	// DefaultIDAllocator is the default IDAllocator.
	DefaultIDAllocator = &idAllocator{instanceID, 0}
	// DefaultTimer is the default Timer.
	DefaultTimer = &timer{}
	// DefaultErrorHandler is the default ErrorHandler.
	DefaultErrorHandler = &errorHandler{}
	// DefaultJSONMarshalFunc is the default JSONMarshalFunc.
	DefaultJSONMarshalFunc = func(writer io.Writer, value interface{}) error {
		data, err := json.Marshal(value)
		if err != nil {
			return err
		}
		_, err = writer.Write(data)
		return err
	}

	// DefaultJSONMarshaller is the default JSON Marshaller.
	DefaultJSONMarshaller = NewJSONMarshaller()

	// DiscardPusher is a Pusher that discards all logs.
	DiscardPusher = discardPusherInstance
	// DiscardLogger is a Logger that discards all logs.
	DiscardLogger = NewLogger(DiscardPusher)

	// DefaultPusher is the default Pusher.
	DefaultPusher = NewTextWritePusher(os.Stderr)
	// DefaultLogger is the default Logger.
	DefaultLogger = NewLogger(DefaultPusher)
)

Functions

func AddGlobalHook

func AddGlobalHook(globalHook GlobalHook)

AddGlobalHook adds a GlobalHook that will be called any time SetLogger or SetLevel is called. It will also be called when added.

func DebugWriter

func DebugWriter() io.Writer

DebugWriter calls DebugWriter on the global Logger.

func Debugf

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

Debugf calls Debugf on the global Logger.

func Debugln

func Debugln(args ...interface{})

Debugln calls Debugln on the global Logger.

func ErrorWriter

func ErrorWriter() io.Writer

ErrorWriter calls ErrorWriter on the global Logger.

func Errorf

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

Errorf calls Errorf on the global Logger.

func Errorln

func Errorln(args ...interface{})

Errorln calls Errorln on the global Logger.

func Fatalf

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

Fatalf calls Fatalf on the global Logger.

func Fatalln

func Fatalln(args ...interface{})

Fatalln calls Fatalln on the global Logger.

func Flush

func Flush() error

Flush calls Flush on the global Logger.

func InfoWriter

func InfoWriter() io.Writer

InfoWriter calls InfoWriter on the global Logger.

func Infof

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

Infof calls Infof on the global Logger.

func Infoln

func Infoln(args ...interface{})

Infoln calls Infoln on the global Logger.

func Panicf

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

Panicf calls Panicf on the global Logger.

func Panicln

func Panicln(args ...interface{})

Panicln calls Panicln on the global Logger.

func Printf

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

Printf calls Printf on the global Logger.

func Println

func Println(args ...interface{})

Println calls Println on the global Logger.

func RedirectStdLogger

func RedirectStdLogger()

RedirectStdLogger will redirect logs to golang's standard logger to the global Logger instance.

func RegisterEncoderDecoder

func RegisterEncoderDecoder(encoding string, encoderDecoder EncoderDecoder) error

RegisterEncoderDecoder registers an EncoderDecoder by encoding.

func SetJSONMarshalFunc

func SetJSONMarshalFunc(jsonMarshalFunc JSONMarshalFunc)

SetJSONMarshalFunc sets the global JSONMarshalFunc to be used by default.

func SetLevel

func SetLevel(level Level)

SetLevel sets the global Logger to to be at the given Level.

func SetLogger

func SetLogger(logger Logger)

SetLogger sets the global Logger instance.

func WarnWriter

func WarnWriter() io.Writer

WarnWriter calls WarnWriter on the global Logger.

func Warnf

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

Warnf calls Warnf on the global Logger.

func Warnln

func Warnln(args ...interface{})

Warnln calls Warnln on the global Logger.

func Writer

func Writer() io.Writer

Writer calls Writer on the global Logger.

Types

type BaseLevelLogger

type BaseLevelLogger interface {
	Printf(format string, args ...interface{})
	Println(args ...interface{})
}

BaseLevelLogger is a LevelLogger without the methods that are self-returning.

This is so sub-packages can implement these.

type BaseLogger

type BaseLogger interface {
	Flusher
	Level() Level
	DebugWriter() io.Writer
	InfoWriter() io.Writer
	WarnWriter() io.Writer
	ErrorWriter() io.Writer
	Writer() io.Writer
	Debugf(format string, args ...interface{})
	Debugln(args ...interface{})
	Infof(format string, args ...interface{})
	Infoln(args ...interface{})
	Warnf(format string, args ...interface{})
	Warnln(args ...interface{})
	Errorf(format string, args ...interface{})
	Errorln(args ...interface{})
	Fatalf(format string, args ...interface{})
	Fatalln(args ...interface{})
	Panicf(format string, args ...interface{})
	Panicln(args ...interface{})
	Printf(format string, args ...interface{})
	Println(args ...interface{})
}

BaseLogger is a Logger without the methods that are self-returning.

This is so sub-packages can implement these.

type Decoder

type Decoder interface {
	Decode(encodedEntryMessage *EncodedEntryMessage) (*EntryMessage, error)
}

Decoder decodes EntryMessages.

type EncodedEntry

type EncodedEntry struct {
	// ID may not be set depending on LoggerOptions.
	// it is up to the user to determine if ID is required.
	ID    string    `json:"id,omitempty"`
	Level Level     `json:"level,omitempty"`
	Time  time.Time `json:"time,omitempty"`
	// both Contexts and Fields can be set
	Contexts []*EncodedEntryMessage `json:"contexts,omitempty"`
	Fields   map[string]string      `json:"fields,omitempty"`
	// zero or one of Event, Message, WriterOutput will be set
	Event        *EncodedEntryMessage `json:"event,omitempty"`
	Message      string               `json:"message,omitempty"`
	WriterOutput []byte               `json:"writer_output,omitempty"`
}

EncodedEntry is an encoded log entry.

func (*EncodedEntry) Decode

func (e *EncodedEntry) Decode() (*Entry, error)

Decode decodes an encoded Entry.

type EncodedEntryMessage

type EncodedEntryMessage struct {
	// Encoding specifies the encoding, such as "json", "protobuf", "thrift".
	Encoding string `json:"encoding,omitempty"`
	// Name specifies the globally-unique name of the message type, such as "google.protobuf.Timestamp".
	Name string `json:"name,omitempty"`
	// Value is the encoded value.
	Value []byte `json:"value,omitempty"`
}

EncodedEntryMessage is an encoded EntryMessage.

func (*EncodedEntryMessage) Decode

func (e *EncodedEntryMessage) Decode() (*EntryMessage, error)

Decode decodes an encoded EntryMessage.

type EncodedPusher

type EncodedPusher interface {
	Flusher
	Push(encodedEntry *EncodedEntry) error
}

EncodedPusher pushes EncodedEntry objects.

type Encoder

type Encoder interface {
	Encode(entryMessage *EntryMessage) (*EncodedEntryMessage, error)
	// Name just gets the globally-unique name for an EntryMessage.
	Name(entryMessage *EntryMessage) (string, error)
}

Encoder encodes EntryMessages.

type EncoderDecoder

type EncoderDecoder interface {
	Encoder
	Decoder
}

EncoderDecoder is an Encoder and Decoarder.

type Entry

type Entry struct {
	// ID may not be set depending on LoggerOptions.
	// it is up to the user to determine if ID is required.
	ID    string    `json:"id,omitempty"`
	Level Level     `json:"level,omitempty"`
	Time  time.Time `json:"time,omitempty"`
	// both Contexts and Fields can be set
	Contexts []*EntryMessage   `json:"contexts,omitempty"`
	Fields   map[string]string `json:"fields,omitempty"`
	// zero or one of Event, Message, WriterOutput will be set
	Event        *EntryMessage `json:"event,omitempty"`
	Message      string        `json:"message,omitempty"`
	WriterOutput []byte        `json:"writer_output,omitempty"`
}

Entry is a log entry.

func (*Entry) Encode

func (e *Entry) Encode() (*EncodedEntry, error)

Encode encodes an Entry.

type EntryMessage

type EntryMessage struct {
	// Encoding specifies the encoding to use, if converting to an EncodedEntryMessage, such as "json", "protobuf", "thrift".
	Encoding string `json:"encoding,omitempty"`
	// Value is the unmarshalled value to use. It is expected that Value can be marshalled to JSON.
	Value interface{} `json:"value,omitempty"`
}

EntryMessage is a context or event in an Entry. It has different meanings depending on what state it is in.

func (*EntryMessage) Encode

func (e *EntryMessage) Encode() (*EncodedEntryMessage, error)

Encode encodes an EntryMessage.

func (*EntryMessage) Name

func (e *EntryMessage) Name() (string, error)

Name gets the globally-unique name for an EntryMessge.

type ErrorHandler

type ErrorHandler interface {
	Handle(err error)
}

ErrorHandler handles errors when logging. The default behavior is to panic.

type Flusher

type Flusher interface {
	Flush() error
}

Flusher is an object that can be flushed to a persistent store.

type GlobalHook

type GlobalHook func(Logger)

GlobalHook is a function that handles a change in the global Logger instance.

type IDAllocator

type IDAllocator interface {
	Allocate() string
}

IDAllocator allocates unique IDs for Entry objects. The default behavior is to allocate a new UUID for the process, then add an incremented integer to the end.

type JSONMarshalFunc

type JSONMarshalFunc func(writer io.Writer, value interface{}) error

JSONMarshalFunc marshals JSON for a TextMarshaller.

It is used internally in a TextMarshaller, and is not a Marshaller itself.

func GlobalJSONMarshalFunc

func GlobalJSONMarshalFunc() JSONMarshalFunc

GlobalJSONMarshalFunc returns the global JSONMarshalFunc instance.

type JSONMarshallerOption

type JSONMarshallerOption func(*jsonMarshaller)

JSONMarshallerOption is an option for creating Marshallers.

func JSONMarshallerDisableNewlines

func JSONMarshallerDisableNewlines() JSONMarshallerOption

JSONMarshallerDisableNewlines disables newlines after each marshalled Entry.

type Level

type Level int32

Level is a logging level.

const (
	// LevelDebug is the debug Level.
	LevelDebug Level = 0
	// LevelInfo is the info Level.
	LevelInfo Level = 1
	// LevelWarn is the warn Level.
	LevelWarn Level = 2
	// LevelError is the error Level.
	LevelError Level = 3
	// LevelFatal is the fatal Level.
	LevelFatal Level = 4
	// LevelPanic is the panic Level.
	LevelPanic Level = 5
	// LevelNone represents no Level.
	// It is always logged.
	LevelNone Level = 6
)

func NameToLevel

func NameToLevel(name string) (Level, error)

NameToLevel returns the Level for the given name.

func (Level) String

func (l Level) String() string

String returns the name of a Level or the numerical value if the Level is unknown.

type LevelLogger

type LevelLogger interface {
	BaseLevelLogger

	WithField(key string, value interface{}) LevelLogger
	WithFields(fields map[string]interface{}) LevelLogger
	WithKeyValues(keyvalues ...interface{}) LevelLogger

	// This generally should only be used internally or by sub-loggers such as the protobuf Logger.
	WithEntryMessageContext(context *EntryMessage) LevelLogger
	// This generally should only be used internally or by sub-loggers such as the protobuf Logger.
	LogEntryMessage(level Level, event *EntryMessage)
}

LevelLogger is a logger tied to a specific Level.

It is returned from a Logger only.

If the requested Level is less than the Logger's level when LevelLogger is constructed, a discard logger will be returned. This is to help with performance concerns of doing lots of WithField/WithFields/etc calls, and then at the end finding the level is discarded.

If log calls are ignored, this has better performance than the standard Logger. If log calls are not ignored, this has slightly worse performance than the standard logger.

Main use of this is for debug calls.

func LogDebug

func LogDebug() LevelLogger

LogDebug calls LogDebug on the global Logger.

func LogInfo

func LogInfo() LevelLogger

LogInfo calls LogInfo on the global Logger.

type Logger

type Logger interface {
	BaseLogger

	AtLevel(level Level) Logger
	WithField(key string, value interface{}) Logger
	WithFields(fields map[string]interface{}) Logger
	WithKeyValues(keyvalues ...interface{}) Logger

	// This generally should only be used internally or by sub-loggers such as the protobuf Logger.
	WithEntryMessageContext(context *EntryMessage) Logger
	// This generally should only be used internally or by sub-loggers such as the protobuf Logger.
	LogEntryMessage(level Level, event *EntryMessage)

	// NOTE: this function name may change, this is experimental
	LogDebug() LevelLogger
	// NOTE: this function name may change, this is experimental
	LogInfo() LevelLogger
}

Logger is the main logging interface. All methods are also replicated on the package and attached to a global Logger.

func AtLevel

func AtLevel(level Level) Logger

AtLevel calls AtLevel on the global Logger.

func GlobalLogger

func GlobalLogger() Logger

GlobalLogger returns the global Logger instance.

func NewLogger

func NewLogger(pusher Pusher, options ...LoggerOption) Logger

NewLogger constructs a new Logger using the given Pusher.

func WithField

func WithField(key string, value interface{}) Logger

WithField calls WithField on the global Logger.

func WithFields

func WithFields(fields map[string]interface{}) Logger

WithFields calls WithFields on the global Logger.

func WithKeyValues

func WithKeyValues(keyValues ...interface{}) Logger

WithKeyValues calls WithKeyValues on the global Logger.

type LoggerOption

type LoggerOption func(*logger)

LoggerOption is an option for the Logger constructor.

func LoggerEnableID

func LoggerEnableID() LoggerOption

LoggerEnableID enables IDs for the Logger.

func LoggerWithErrorHandler

func LoggerWithErrorHandler(errorHandler ErrorHandler) LoggerOption

LoggerWithErrorHandler uses the ErrorHandler for the Logger.

func LoggerWithIDAllocator

func LoggerWithIDAllocator(idAllocator IDAllocator) LoggerOption

LoggerWithIDAllocator uses the IDAllocator for the Logger.

func LoggerWithTimer

func LoggerWithTimer(timer Timer) LoggerOption

LoggerWithTimer uses the Timer for the Logger.

type Marshaller

type Marshaller interface {
	Marshal(entry *Entry) ([]byte, error)
}

Marshaller marshals Entry objects to be written.

func NewJSONMarshaller

func NewJSONMarshaller(options ...JSONMarshallerOption) Marshaller

NewJSONMarshaller constructs a new Marshaller for JSON.

type Puller

type Puller interface {
	Pull() (*EncodedEntry, error)
}

Puller pulls EncodedEntry objects from a persistent store.

func NewReadPuller

func NewReadPuller(reader io.Reader, unmarshaller Unmarshaller) Puller

NewReadPuller constructs a new Puller that reads from the given Reader.

type Pusher

type Pusher interface {
	Flusher
	Push(entry *Entry) error
}

Pusher is the interface used to push Entry objects to a persistent store.

func EncodedPusherToPusher

func EncodedPusherToPusher(encodedPusher EncodedPusher) Pusher

EncodedPusherToPusher returns a Pusher for the EncodedPusher

func NewJSONWritePusher

func NewJSONWritePusher(writer io.Writer, jsonMarshallerOptions ...JSONMarshallerOption) Pusher

NewJSONWritePusher constructs a new Pusher using a JSON Marshaller.

func NewMultiPusher

func NewMultiPusher(pushers ...Pusher) Pusher

NewMultiPusher constructs a new Pusher that calls all the given Pushers.

func NewTextWritePusher

func NewTextWritePusher(writer io.Writer, textMarshallerOptions ...TextMarshallerOption) Pusher

NewTextWritePusher constructs a new Pusher using a TextMarshaller.

func NewWritePusher

func NewWritePusher(writer io.Writer, marshaller Marshaller) Pusher

NewWritePusher constructs a new Pusher that writes to the given io.Writer.

type TextMarshaller

type TextMarshaller interface {
	Marshaller
	WithColors() TextMarshaller
	WithoutColors() TextMarshaller
}

TextMarshaller is a Marshaller used for text.

func NewTextMarshaller

func NewTextMarshaller(options ...TextMarshallerOption) TextMarshaller

NewTextMarshaller constructs a new Marshaller that produces human-readable marshalled Entry objects. This Marshaller is currently inefficient.

type TextMarshallerOption

type TextMarshallerOption func(*textMarshaller)

TextMarshallerOption is an option for creating Marshallers.

func TextMarshallerDisableContexts

func TextMarshallerDisableContexts() TextMarshallerOption

TextMarshallerDisableContexts will suppress the printing of Entry contexts.

func TextMarshallerDisableLevel

func TextMarshallerDisableLevel() TextMarshallerOption

TextMarshallerDisableLevel will suppress the printing of Entry Levels.

func TextMarshallerDisableNewlines

func TextMarshallerDisableNewlines() TextMarshallerOption

TextMarshallerDisableNewlines disables newlines after each marshalled Entry.

func TextMarshallerDisableTime

func TextMarshallerDisableTime() TextMarshallerOption

TextMarshallerDisableTime will suppress the printing of Entry Timestamps.

type Timer

type Timer interface {
	Now() time.Time
}

Timer returns the current time. The default behavior is to call time.Now().UTC().

type Unmarshaller

type Unmarshaller interface {
	Unmarshal(reader io.Reader, encodedtry *EncodedEntry) error
}

Unmarshaller unmarshalls a marshalled EncodedEntry object. At the end of a stream, Unmarshaller will return io.EOF.

Directories

Path Synopsis
Package currentlion implements basic integration with Current using plaintext syslog.
Package currentlion implements basic integration with Current using plaintext syslog.
Package envlion provides simple utilities to setup lion from the environment.
Package envlion provides simple utilities to setup lion from the environment.
example
current
Package main implements an example integration with Current.
Package main implements an example integration with Current.
Package gcloudlion integrates lion with the Google Cloud Logging service.
Package gcloudlion integrates lion with the Google Cloud Logging service.
Package ginlion defines functionality to integrate lion with gin.
Package ginlion defines functionality to integrate lion with gin.
Package grpclion provides a logger for grpclog.
Package grpclion provides a logger for grpclog.
Package kitlion defines functionality to integrate lion with go-kit.
Package kitlion defines functionality to integrate lion with go-kit.
Package protolion defines the Protocol Buffers functionality for lion.
Package protolion defines the Protocol Buffers functionality for lion.
testing
Package protoliontesting defines testing for protolion.
Package protoliontesting defines testing for protolion.
Package sysloglion defines functionality for integration with syslog.
Package sysloglion defines functionality for integration with syslog.
Package taillion implements tailing of lion log files.
Package taillion implements tailing of lion log files.
Package thriftlion defines the Thrift functionality for lion.
Package thriftlion defines the Thrift functionality for lion.
testing
Package thriftliontesting defines testing for thriftlion.
Package thriftliontesting defines testing for thriftlion.

Jump to

Keyboard shortcuts

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