Documentation
¶
Overview ¶
Package gsl provides a concurrency-safe logger.
Index ¶
- type CreateApplicationLoggerInput
- type ErrUnknownLevel
- type Logger
- func (l *Logger) Close()
- func (l *Logger) Debug(obj interface{}) error
- func (l *Logger) DebugF(format string, values ...interface{}) error
- func (l *Logger) Error(obj interface{}) error
- func (l *Logger) ErrorF(format string, values ...interface{}) error
- func (l *Logger) Fatal(obj interface{})
- func (l *Logger) FatalF(format string, values ...interface{})
- func (l *Logger) Flush() error
- func (l *Logger) FormatObject(level string, obj interface{}, format string) ([]byte, error)
- func (l *Logger) Info(obj interface{}) error
- func (l *Logger) InfoF(format string, values ...interface{}) error
- func (l *Logger) ListenError(messages chan interface{}, wg *sync.WaitGroup)
- func (l *Logger) ListenFatal(messages chan interface{})
- func (l *Logger) ListenInfo(messages chan interface{}, wg *sync.WaitGroup)
- func (l *Logger) Warn(obj interface{}) error
- func (l *Logger) WriteLine(level string, obj interface{}, writer Writer, format string) (n int, err error)
- func (l *Logger) WriteLineSafe(level string, obj interface{}, writer Writer, format string) (n int, err error)
- type Writer
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 ¶
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 ¶
Debug writes the provided object to the `debug` writer. If no `debug` writer exists, then return an ErrUnknownLevel error.
func (*Logger) DebugF ¶
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 ¶
Error writes the provided object to the `error` writer. If no `error` writer exists, then return an ErrUnknownLevel error.
func (*Logger) ErrorF ¶
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 ¶
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) FormatObject ¶
FormatObject formats a given object using a given level and format and returns the formatted bytes and error, if any.
func (*Logger) Info ¶
Info writes the provided object to the `info` writer. If no `info` writer exists, then return an ErrUnknownLevel error.
func (*Logger) InfoF ¶
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 ¶
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 ¶
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 ¶
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.