gsl

package
v0.0.0-...-898cb84 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package gsl provides a concurrency-safe logger.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CreateApplicationLoggerInput

type CreateApplicationLoggerInput struct {
	ErrorDestination string
	ErrorCompression string
	ErrorFormat      string
	InfoDestination  string
	InfoCompression  string
	InfoFormat       string
	Verbose          bool
}

CreateApplicationLoggerInput holds the input for the CreateApplicationLogger function..

type ErrUnknownLevel

type ErrUnknownLevel struct {
	Level string
}

func (*ErrUnknownLevel) Error

func (e *ErrUnknownLevel) Error() string

type Logger

type Logger struct {
	LevelField      string // the key for the level field
	TimeStampField  string // the key for the timestamp field
	TimeStampFormat string // the format for the timestamp field
	MessageField    string // the key for the message field
	AutoFlush       bool   // flush after every message
	// contains filtered or unexported fields
}

Logger contains a slice of writers, a slice of matching formats, and a mapping of levels to writers.

func CreateApplicationLogger

func CreateApplicationLogger(input *CreateApplicationLoggerInput) *Logger

CreateApplicationLogger creates a new *Logger given the fields in *CreateApplicationLoggerInput. This function creates a logger that intuitively works as you would expect an application logger to work. The logger shares a single grw.ByteWriteCloser if error and info messages are going to the same location. If verbose mode is on, warn messages are sent to the error log and debug messages are sent to the info log. If there is an error during creation then the program prints the error and exits with exit code 1.

func NewLogger

func NewLogger(levels map[string]int, writers []Writer, formats []string, autoFlush bool) *Logger

NewLogger returns a new logger with the given configuration and default field keys. Set autoFlush to true to flush the buffer to the underlying writer after every message.

func (*Logger) Close

func (l *Logger) Close()

Close locks all the writers, flushes them, closes them, and then unlocks them.

func (*Logger) Debug

func (l *Logger) Debug(obj interface{}) error

Debug writes the provided object to the `debug` writer. If no `debug` writer exists, then return an ErrUnknownLevel error.

func (*Logger) DebugF

func (l *Logger) DebugF(format string, values ...interface{}) error

DebugF writes the provided message to the `debug` writer. The message is generated using `fmt.Sprintf(format, values...)`. If no `debug` writer exists, then return an ErrUnknownLevel error.

func (*Logger) Error

func (l *Logger) Error(obj interface{}) error

Error writes the provided object to the `error` writer. If no `error` writer exists, then return an ErrUnknownLevel error.

func (*Logger) ErrorF

func (l *Logger) ErrorF(format string, values ...interface{}) error

ErrorF writes the provided message to the `error` writer. The message is generated using `fmt.Sprintf(format, values...)`. If no `error` writer exists, then return an ErrUnknownLevel error.

func (*Logger) Fatal

func (l *Logger) Fatal(obj interface{})

Fatal locks all the writers, flushes them, writes the given message to the fatal writer, flushes the writers again, closes the writers, unlocks the writers, and finally exits with code 1.

func (*Logger) FatalF

func (l *Logger) FatalF(format string, values ...interface{})

FatalF writes a message to the fatal writer using the provide format and values. The message is generated using `fmt.Sprintf(format, values...)`.

func (*Logger) Flush

func (l *Logger) Flush() error

FlushSafe flushes all the writers using concurrency-safe methods.

func (*Logger) FormatObject

func (l *Logger) FormatObject(level string, obj interface{}, format string) ([]byte, error)

FormatObject formats a given object using a given level and format and returns the formatted bytes and error, if any.

func (*Logger) Info

func (l *Logger) Info(obj interface{}) error

Info writes the provided object to the `info` writer. If no `info` writer exists, then return an ErrUnknownLevel error.

func (*Logger) InfoF

func (l *Logger) InfoF(format string, values ...interface{}) error

InfoF writes the provided message to the `info` writer. The message is generated using `fmt.Sprintf(format, values...)`. If no `info` writer exists, then return an ErrUnknownLevel error.

func (*Logger) ListenError

func (l *Logger) ListenError(messages chan interface{}, wg *sync.WaitGroup)

ListenError listens for message on a `chan interface{}` channel and writes them to the error writer. If a *sync.WaitGroup is not nil, then it is marked as done once the channel is closed.

func (*Logger) ListenFatal

func (l *Logger) ListenFatal(messages chan interface{})

ListenFatal listens for a message on a `chan interface{}` channel. Once a message is received, the logger immediately calls l.Fatal(), which writes the fatal error, flushes the logs, closes the logs, and finally exits with code 1.

func (*Logger) ListenInfo

func (l *Logger) ListenInfo(messages chan interface{}, wg *sync.WaitGroup)

ListenError listens for message on a `chan interface{}` channel and writes them to the info writer. If a *sync.WaitGroup is not nil, then it is marked as done once the channel is closed.

func (*Logger) Warn

func (l *Logger) Warn(obj interface{}) error

Warn writes the provided object to the `warn` writer. If no `warn` writer exists, then return an ErrUnknownLevel error.

func (*Logger) WriteLine

func (l *Logger) WriteLine(level string, obj interface{}, writer Writer, format string) (n int, err error)

WriteLine formats the given object using FormatObject then writes the formatted string with a trailing newline to the matching grw.ByteWriteCloser and returns an error, if any. WriteLine calls the writer's WriteLine method, which does not lock the underlying writer. The writer can already be locked.

func (*Logger) WriteLineSafe

func (l *Logger) WriteLineSafe(level string, obj interface{}, writer Writer, format string) (n int, err error)

WriteLineSafe formats the given object using FormatObject then writes the formatted string with a trailing newline to the matching grw.ByteWriteCloser and returns an error, if any. WriteLineSafe calls the writer's WriteLineSafe method, which locks the underlying writer for the duration of writing using a sync.Mutex.

type Writer

type Writer interface {
	WriteLine(str string) (int, error)     // write line to underlying writer
	WriteLineSafe(str string) (int, error) // lock underlying writer, write line, and then unlock
	Lock()                                 // lock underlying writer
	Unlock()                               // unlock underlying writer
	Flush() error                          // flush buffer to underlying writer
	FlushSafe() error                      // lock underlying writer, flush buffer, and then unlock
	Close() error                          // lock all the underlying writers, flush their buffers, close all the writers, and then unlock.
}

Interface containing the methods required for underlying writers. This interface is implemented by go-reader-writer.

Jump to

Keyboard shortcuts

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