protolog

package module
v0.0.0-...-146e201 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2016 License: MIT Imports: 19 Imported by: 0

README

CircleCI Go Report Card GoDoc MIT License

go get go.pedge.io/protolog

Initial beta release coming soon - I need to do one more pass on this and then document all the functionality.

  • Structured logging with Protocol buffers
  • Child of https://github.com/peter-edge/ledge-go
  • Some compatibility with existing libraries (specifically logrus and glog)
  • Two-way serialization - write logs somewhere, read them back, language independent

Where to poke around

  • protolog.go: all the public definiions
  • protolog.proto: the protos that are serialized over the wire
  • testing*: test compilation of proto definitions
  • benchmark*: more compilation of proto definitions, and benchmarks
  • make test: will print out some logs with the default text marshaller
  • make bench: some basic benchmarks

TODO

  • journal writer?
  • colors in terminals
  • better text formatting/options
  • third-party logs integration
  • performance improvements/testing
  • documentation

Documentation

Overview

Package protolog defines the main protolog functionality.

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{}

	// DelimitedMarshaller is a Marshaller that uses the protocol buffers write delimited scheme.
	DelimitedMarshaller = &delimitedMarshaller{}
	// DelimitedUnmarshaller is an Unmarshaller that uses the protocol buffers write delimited scheme.
	DelimitedUnmarshaller = &delimitedUnmarshaller{}

	// 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 Debug

func Debug(Event proto.Message)

Debug calls Debug on the global Logger.

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 Error

func Error(Event proto.Message)

Error calls Error 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 Fatal

func Fatal(Event proto.Message)

Fatal calls Fatal 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 Info

func Info(Event proto.Message)

Info calls Info 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 Panic

func Panic(Event proto.Message)

Panic calls Panic 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 Print

func Print(Event proto.Message)

Print calls Print 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 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 Warn

func Warn(Event proto.Message)

Warn calls Warn on the global Logger.

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 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 may be set
	Contexts []proto.Message   `json:"contexts,omitempty"`
	Fields   map[string]string `json:"fields,omitempty"`
	// one of Event, Message, WriterOutput will be set
	Event        proto.Message `json:"event,omitempty"`
	Message      string        `json:"message,omitempty"`
	WriterOutput []byte        `json:"writer_output,omitempty"`
}

Entry is the go equivalent of an Entry.

func (*Entry) String

func (g *Entry) String() string

String defaults a string representation of the Entry.

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 Level

type Level int32

Level is a logging level.

const (
	// LevelNone represents no Level.
	LevelNone Level = 0
	// LevelDebug is the debug Level.
	LevelDebug Level = 1
	// LevelInfo is the info Level.
	LevelInfo Level = 2
	// LevelWarn is the warn Level.
	LevelWarn Level = 3
	// LevelError is the error Level.
	LevelError Level = 4
	// LevelFatal is the fatal Level.
	LevelFatal Level = 5
	// LevelPanic is the panic Level.
	LevelPanic 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 Logger

type Logger interface {
	Flusher

	AtLevel(level Level) Logger

	WithContext(context proto.Message) Logger
	Debug(event proto.Message)
	Info(event proto.Message)
	Warn(event proto.Message)
	Error(event proto.Message)
	Fatal(event proto.Message)
	Panic(event proto.Message)
	Print(event proto.Message)

	DebugWriter() io.Writer
	InfoWriter() io.Writer
	WarnWriter() io.Writer
	ErrorWriter() io.Writer
	Writer() io.Writer

	WithField(key string, value interface{}) Logger
	WithFields(fields map[string]interface{}) Logger
	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{})
}

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 WithContext

func WithContext(context proto.Message) Logger

WithContext calls WithContext on the global Logger.

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.

type LoggerOption

type LoggerOption func(*logger)

LoggerOption is an option for the Logger constructor.

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 LoggerWithIDEnabled

func LoggerWithIDEnabled() LoggerOption

LoggerWithIDEnabled enables IDs 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.

type Puller

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

Puller pulls Entry objects from a persistent store.

func NewReadPuller

func NewReadPuller(reader io.Reader, options ...ReadPullerOption) 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 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 and newlines.

func NewWritePusher

func NewWritePusher(writer io.Writer, options ...WritePusherOption) Pusher

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

type ReadPullerOption

type ReadPullerOption func(*readPuller)

ReadPullerOption is an option for a read Puller.

func ReadPullerWithUnmarshaller

func ReadPullerWithUnmarshaller(unmarshaller Unmarshaller) ReadPullerOption

ReadPullerWithUnmarshaller uses the Unmarshaller for the read Puller.

By default, DelimitedUnmarshaller is used.

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, entry *Entry) error
}

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

type WritePusherOption

type WritePusherOption func(*writePusher)

WritePusherOption is an option for constructing a new write Pusher.

func WritePusherWithMarshaller

func WritePusherWithMarshaller(marshaller Marshaller) WritePusherOption

WritePusherWithMarshaller uses the Marshaller for the write Pusher.

By default, DelimitedMarshaller is used.

Directories

Path Synopsis
Package protolog_glog defines functionality for integration with glog.
Package protolog_glog defines functionality for integration with glog.
Package protolog_logrus defines functionality for integration with Logrus.
Package protolog_logrus defines functionality for integration with Logrus.
Package protologpb is a generated protocol buffer package.
Package protologpb is a generated protocol buffer package.
Package protolog_syslog defines functionality for integration with syslog.
Package protolog_syslog defines functionality for integration with syslog.

Jump to

Keyboard shortcuts

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