...

Package logger

import "gitlab.com/tymonx/go-logger/logger"
Overview
Index
Examples

Overview ▾

Package logger implements logging package. It defines a type, Logger, with methods for formatting output. Each logging operations creates and sends lightweight not formatted log message to separate worker thread. It offloads main code from unnecessary resource consuming formatting and I/O operations. On default it supports many different log handlers like logging to standard output, error output, file, stream or syslog.

Example

Code:

// To make testing this example more consistent, date must be constant
for _, handler := range logger.GetHandlers() {
    handler.GetFormatter().SetDateFormat("2020")
}

logger.Info("Hello from logger!")
logger.Info("Automatic placeholders {p} {p} {p}", 1, 2, 3)
logger.Info("Positional placeholders {p2} {p1} {p0}", 1, 2, 3)

logger.Info("Named placeholders {z} {y} {x}", logger.Named{
    "x": 1,
    "y": 2,
    "z": 3,
})

logger.Info("Object placeholders {.Z} {.Y} {.X}", struct {
    X, Y, Z int
}{
    X: 1,
    Y: 2,
    Z: 3,
})

logger.Flush()

Output:

2020 - Info     - logger_test.go:31:logger_test.Example(): Hello from logger!
2020 - Info     - logger_test.go:32:logger_test.Example(): Automatic placeholders 1 2 3
2020 - Info     - logger_test.go:33:logger_test.Example(): Positional placeholders 3 2 1
2020 - Info     - logger_test.go:35:logger_test.Example(): Named placeholders 3 2 1
2020 - Info     - logger_test.go:41:logger_test.Example(): Object placeholders 3 2 1

Index ▾

Constants
func Alert(message string, arguments ...interface{})
func Close()
func Critical(message string, arguments ...interface{})
func Debug(message string, arguments ...interface{})
func Error(message string, arguments ...interface{})
func Fatal(message string, arguments ...interface{})
func GetErrorCode() int
func GetName() string
func Info(message string, arguments ...interface{})
func IsEnabled() bool
func Log(level int, levelName, message string, arguments ...interface{})
func Notice(message string, arguments ...interface{})
func Panic(message string, arguments ...interface{})
func StreamHandlerDefault(writer io.Writer, record *Record, formatter *Formatter) error
func StreamHandlerNDJSON(writer io.Writer, record *Record, _ *Formatter) error
func Trace(message string, arguments ...interface{})
func Warning(message string, arguments ...interface{})
type Arguments
type Buffer
    func NewBuffer() *Buffer
    func (b *Buffer) Bytes() []byte
    func (b *Buffer) Close() error
    func (b *Buffer) Disable() Handler
    func (b *Buffer) Emit(record *Record) error
    func (b *Buffer) Enable() Handler
    func (b *Buffer) GetBuffer() *bytes.Buffer
    func (b *Buffer) GetFormatter() *Formatter
    func (b *Buffer) GetLevelRange() (min, max int)
    func (b *Buffer) GetMaximumLevel() int
    func (b *Buffer) GetMinimumLevel() int
    func (b *Buffer) IsEnabled() bool
    func (b *Buffer) Length() int
    func (b *Buffer) Reset()
    func (b *Buffer) SetFormatter(formatter *Formatter) Handler
    func (b *Buffer) SetLevel(level int) Handler
    func (b *Buffer) SetLevelRange(min, max int) Handler
    func (b *Buffer) SetMaximumLevel(level int) Handler
    func (b *Buffer) SetMinimumLevel(level int) Handler
    func (b *Buffer) SetStreamHandler(handler StreamHandler) *Buffer
    func (b *Buffer) String() string
type File
    func NewFile() *File
    func (f *File) Close() error
    func (f *File) Disable() Handler
    func (f *File) Emit(record *Record) error
    func (f *File) Enable() Handler
    func (f *File) GetFlags() int
    func (f *File) GetFormatter() *Formatter
    func (f *File) GetLevelRange() (min, max int)
    func (f *File) GetMaximumLevel() int
    func (f *File) GetMinimumLevel() int
    func (f *File) GetMode() os.FileMode
    func (f *File) GetName() string
    func (f *File) IsEnabled() bool
    func (f *File) Open() (io.WriteCloser, error)
    func (f *File) SetFlags(flags int) *File
    func (f *File) SetFormatter(formatter *Formatter) Handler
    func (f *File) SetLevel(level int) Handler
    func (f *File) SetLevelRange(min, max int) Handler
    func (f *File) SetMaximumLevel(level int) Handler
    func (f *File) SetMinimumLevel(level int) Handler
    func (f *File) SetMode(mode os.FileMode) *File
    func (f *File) SetName(name string) *File
    func (f *File) SetStreamHandler(handler StreamHandler) *File
type Formatter
    func NewFormatter() *Formatter
    func (f *Formatter) AddFuncs(funcs FormatterFuncs) *Formatter
    func (f *Formatter) Format(record *Record) (string, error)
    func (f *Formatter) FormatMessage(record *Record) (string, error)
    func (f *Formatter) FormatTime(record *Record) (string, error)
    func (f *Formatter) GetDateFormat() string
    func (f *Formatter) GetFormat() string
    func (f *Formatter) GetPlaceholder() string
    func (f *Formatter) Reset() *Formatter
    func (f *Formatter) SetDateFormat(dateFormat string) *Formatter
    func (f *Formatter) SetFormat(format string) *Formatter
    func (f *Formatter) SetPlaceholder(placeholder string) *Formatter
type FormatterFuncs
type Handler
    func GetHandler(name string) (Handler, error)
type Handlers
    func GetHandlers() Handlers
type IDGenerator
    func GetIDGenerator() IDGenerator
type Level
type Logger
    func AddFuncs(funcs FormatterFuncs) *Logger
    func AddHandler(name string, handler Handler) *Logger
    func Disable() *Logger
    func Emit(record *Record) *Logger
    func Enable() *Logger
    func Flush() *Logger
    func Get() *Logger
    func New() *Logger
    func RemoveHandler(name string) *Logger
    func RemoveHandlers() *Logger
    func Reset() *Logger
    func ResetFormatters() *Logger
    func ResetHandlers() *Logger
    func SetDateFormat(format string) *Logger
    func SetErrorCode(errorCode int) *Logger
    func SetFormat(format string) *Logger
    func SetFormatter(formatter *Formatter) *Logger
    func SetHandler(name string, handler Handler) *Logger
    func SetHandlers(handlers Handlers) *Logger
    func SetIDGenerator(idGenerator IDGenerator) *Logger
    func SetLevel(level int) *Logger
    func SetLevelRange(min, max int) *Logger
    func SetMaximumLevel(level int) *Logger
    func SetMinimumLevel(level int) *Logger
    func SetName(name string) *Logger
    func SetPlaceholder(placeholder string) *Logger
    func (l *Logger) AddFuncs(funcs FormatterFuncs) *Logger
    func (l *Logger) AddHandler(name string, handler Handler) *Logger
    func (l *Logger) Alert(message string, arguments ...interface{})
    func (l *Logger) Close() error
    func (l *Logger) CloseDefer()
    func (l *Logger) Critical(message string, arguments ...interface{})
    func (l *Logger) Debug(message string, arguments ...interface{})
    func (l *Logger) Disable() *Logger
    func (l *Logger) Emit(record *Record) *Logger
    func (l *Logger) Enable() *Logger
    func (l *Logger) Error(message string, arguments ...interface{})
    func (l *Logger) Fatal(message string, arguments ...interface{})
    func (l *Logger) Flush() *Logger
    func (l *Logger) GetErrorCode() int
    func (l *Logger) GetHandler(name string) (Handler, error)
    func (l *Logger) GetHandlers() Handlers
    func (l *Logger) GetIDGenerator() IDGenerator
    func (l *Logger) GetName() string
    func (l *Logger) Info(message string, arguments ...interface{})
    func (l *Logger) IsEnabled() bool
    func (l *Logger) Log(level int, levelName, message string, arguments ...interface{})
    func (l *Logger) LogMessage(level int, levelName, message string, arguments ...interface{})
    func (l *Logger) Notice(message string, arguments ...interface{})
    func (l *Logger) Panic(message string, arguments ...interface{})
    func (l *Logger) RemoveHandler(name string) *Logger
    func (l *Logger) RemoveHandlers() *Logger
    func (l *Logger) Reset() *Logger
    func (l *Logger) ResetFormatters() *Logger
    func (l *Logger) ResetHandlers() *Logger
    func (l *Logger) SetDateFormat(format string) *Logger
    func (l *Logger) SetErrorCode(errorCode int) *Logger
    func (l *Logger) SetFormat(format string) *Logger
    func (l *Logger) SetFormatter(formatter *Formatter) *Logger
    func (l *Logger) SetHandler(name string, handler Handler) *Logger
    func (l *Logger) SetHandlers(handlers Handlers) *Logger
    func (l *Logger) SetIDGenerator(idGenerator IDGenerator) *Logger
    func (l *Logger) SetLevel(level int) *Logger
    func (l *Logger) SetLevelRange(min, max int) *Logger
    func (l *Logger) SetMaximumLevel(level int) *Logger
    func (l *Logger) SetMinimumLevel(level int) *Logger
    func (l *Logger) SetName(name string) *Logger
    func (l *Logger) SetPlaceholder(placeholder string) *Logger
    func (l *Logger) Trace(message string, arguments ...interface{})
    func (l *Logger) Warning(message string, arguments ...interface{})
type Named
type Opener
type Record
    func (r *Record) FromJSON(data []byte) error
    func (r *Record) GetMessage() (string, error)
    func (r *Record) ToJSON() ([]byte, error)
type RuntimeError
    func NewRuntimeError(message string, arguments ...interface{}) *RuntimeError
    func NewRuntimeErrorBase(skipCall int, message string, arguments ...interface{}) *RuntimeError
    func (r *RuntimeError) Error() string
    func (r *RuntimeError) Unwrap() error
type Source
type Stream
    func NewStderr() *Stream
    func NewStdout() *Stream
    func NewStream() *Stream
    func (s *Stream) Close() error
    func (s *Stream) Disable() Handler
    func (s *Stream) Emit(record *Record) error
    func (s *Stream) Enable() Handler
    func (s *Stream) GetFormatter() *Formatter
    func (s *Stream) GetLevelRange() (min, max int)
    func (s *Stream) GetMaximumLevel() int
    func (s *Stream) GetMinimumLevel() int
    func (s *Stream) IsEnabled() bool
    func (s *Stream) Lock()
    func (s *Stream) RLock()
    func (s *Stream) RUnlock()
    func (s *Stream) Reopen() *Stream
    func (s *Stream) SetFormatter(formatter *Formatter) Handler
    func (s *Stream) SetLevel(level int) Handler
    func (s *Stream) SetLevelRange(min, max int) Handler
    func (s *Stream) SetMaximumLevel(level int) Handler
    func (s *Stream) SetMinimumLevel(level int) Handler
    func (s *Stream) SetOpener(opener Opener) *Stream
    func (s *Stream) SetStreamHandler(handler StreamHandler) *Stream
    func (s *Stream) SetWriteCloser(writeCloser io.WriteCloser) error
    func (s *Stream) SetWriter(writer io.Writer) error
    func (s *Stream) Unlock()
type StreamHandler
type Syslog
    func NewSyslog() *Syslog
    func (s *Syslog) Close() error
    func (s *Syslog) Disable() Handler
    func (s *Syslog) Emit(record *Record) error
    func (s *Syslog) Enable() Handler
    func (s *Syslog) GetAddress() string
    func (s *Syslog) GetFormatter() *Formatter
    func (s *Syslog) GetLevelRange() (min, max int)
    func (s *Syslog) GetMaximumLevel() int
    func (s *Syslog) GetMinimumLevel() int
    func (s *Syslog) GetNetwork() string
    func (s *Syslog) GetPort() int
    func (s *Syslog) GetTimeout() time.Duration
    func (s *Syslog) IsEnabled() bool
    func (s *Syslog) Open() (io.WriteCloser, error)
    func (s *Syslog) SetAddress(address string) *Syslog
    func (s *Syslog) SetFormatter(formatter *Formatter) Handler
    func (s *Syslog) SetLevel(level int) Handler
    func (s *Syslog) SetLevelRange(min, max int) Handler
    func (s *Syslog) SetMaximumLevel(level int) Handler
    func (s *Syslog) SetMinimumLevel(level int) Handler
    func (s *Syslog) SetNetwork(network string) *Syslog
    func (s *Syslog) SetPort(port int) *Syslog
    func (s *Syslog) SetTimeout(timeout time.Duration) *Syslog
type Timestamp
type UUID4
    func NewUUID4() *UUID4
    func (u *UUID4) Generate() (id string, err error)
type Worker
    func GetWorker() *Worker
    func NewWorker() *Worker
    func (w *Worker) Flush() *Worker
    func (w *Worker) SetQueueLength(length int) *Worker

Examples

Package

Package files

arguments.go buffer.go doc.go file.go formatter.go glogger.go handler.go id_generator.go level.go logger.go record.go runtime_error.go source.go stderr.go stdout.go stream.go syslog.go timestamp.go utils.go uuid4.go worker.go

Constants

These constants define default values for File log handler.

const (
    DefaultFileName  = "log"
    DefaultFileMode  = 0644
    DefaultFileFlags = os.O_CREATE | os.O_APPEND | os.O_WRONLY
)

These constants define default values for Formatter.

const (
    DefaultDateFormat  = "{year}-{month}-{day} {hour}:{minute}:{second},{millisecond}"
    DefaultFormat      = "{date} - {Level | printf \"%-8s\"} - {file}:{line}:{function}(): {message}"
    DefaultPlaceholder = "p"
)

These constants define log level values and names used by various logger functions like for example Debug or Info. It defines also default logger values.

const (
    OffsetLevel = 10

    TraceLevel    = 0
    DebugLevel    = OffsetLevel + TraceLevel
    InfoLevel     = OffsetLevel + DebugLevel
    NoticeLevel   = OffsetLevel + InfoLevel
    WarningLevel  = OffsetLevel + NoticeLevel
    ErrorLevel    = OffsetLevel + WarningLevel
    CriticalLevel = OffsetLevel + ErrorLevel
    AlertLevel    = OffsetLevel + CriticalLevel
    FatalLevel    = OffsetLevel + AlertLevel
    PanicLevel    = OffsetLevel + FatalLevel

    MinimumLevel = TraceLevel
    MaximumLevel = PanicLevel

    TraceName    = "trace"
    DebugName    = "debug"
    InfoName     = "info"
    NoticeName   = "notice"
    WarningName  = "warning"
    ErrorName    = "error"
    CriticalName = "critical"
    AlertName    = "alert"
    FatalName    = "fatal"
    PanicName    = "panic"

    DefaultTypeName = "log"

    DefaultErrorCode = 1
)

These constants define default values for syslog.

const (
    DefaultSyslogPort    = 514
    DefaultSyslogVersion = 1
    DefaultSyslogNetwork = "udp"
    DefaultSyslogAddress = "localhost"
    DefaultSyslogTimeout = 100 * time.Millisecond
    DefaultSyslogFormat  = "<{syslogPriority}>{syslogVersion} {iso8601} {address} {name} {pid} {id} - " +
        "{file}:{line}:{function}(): {message}"
    DefaultSyslogFacility = 1
)

These constants define default values for Worker.

const (
    DefaultQueueLength = 4096
)

These constants are used for the RuntimeError.

const (
    RuntimeErrorSkipCall = 1
)

func Alert

func Alert(message string, arguments ...interface{})

Alert logs messages for alert conditions. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func Close

func Close()

Close closes all added log handlers.

func Critical

func Critical(message string, arguments ...interface{})

Critical logs messages for critical conditions. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func Debug

func Debug(message string, arguments ...interface{})

Debug logs debugging messages. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func Error

func Error(message string, arguments ...interface{})

Error logs messages for error conditions. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func Fatal

func Fatal(message string, arguments ...interface{})

Fatal logs messages for fatal conditions. It stops logger worker thread and it exists the application with an error code. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func GetErrorCode

func GetErrorCode() int

GetErrorCode returns error code.

func GetName

func GetName() string

GetName returns logger name.

func Info

func Info(message string, arguments ...interface{})

Info logs informational messages. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func IsEnabled

func IsEnabled() bool

IsEnabled returns true if at least one of added log handlers is enabled.

func Log

func Log(level int, levelName, message string, arguments ...interface{})

Log logs messages with user defined log level value and name. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func Notice

func Notice(message string, arguments ...interface{})

Notice logs messages for significant conditions. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func Panic

func Panic(message string, arguments ...interface{})

Panic logs messages for fatal conditions. It stops logger worker thread and it exists the application with a panic. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func StreamHandlerDefault

func StreamHandlerDefault(writer io.Writer, record *Record, formatter *Formatter) error

StreamHandlerDefault is a default stream handler for writing log records to stream.

func StreamHandlerNDJSON

func StreamHandlerNDJSON(writer io.Writer, record *Record, _ *Formatter) error

StreamHandlerNDJSON handles writing log records in the NDJSON format.

func Trace

func Trace(message string, arguments ...interface{})

Trace logs finer-grained informational messages than the Debug. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func Warning

func Warning(message string, arguments ...interface{})

Warning logs messages for warning conditions that can be potentially harmful. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

type Arguments

Arguments defines log arguments.

type Arguments []interface{}

type Buffer

A Buffer represents a log handler object for logging messages using buffer object.

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

func NewBuffer

func NewBuffer() *Buffer

NewBuffer creates a new buffer log handler object.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns a slice of length b.Length() holding the unread portion of the buffer. The slice is valid for use only until the next buffer modification.

func (*Buffer) Close

func (b *Buffer) Close() error

Close closes buffer.

func (*Buffer) Disable

func (b *Buffer) Disable() Handler

Disable disabled log handler.

func (*Buffer) Emit

func (b *Buffer) Emit(record *Record) error

Emit logs messages from logger using buffer.

func (*Buffer) Enable

func (b *Buffer) Enable() Handler

Enable enables log handler.

func (*Buffer) GetBuffer

func (b *Buffer) GetBuffer() *bytes.Buffer

GetBuffer returns internal buffer object.

func (*Buffer) GetFormatter

func (b *Buffer) GetFormatter() *Formatter

GetFormatter returns log formatter.

func (*Buffer) GetLevelRange

func (b *Buffer) GetLevelRange() (min, max int)

GetLevelRange returns minimum and maximum log level values.

func (*Buffer) GetMaximumLevel

func (b *Buffer) GetMaximumLevel() int

GetMaximumLevel returns maximum log level.

func (*Buffer) GetMinimumLevel

func (b *Buffer) GetMinimumLevel() int

GetMinimumLevel returns minimum log level.

func (*Buffer) IsEnabled

func (b *Buffer) IsEnabled() bool

IsEnabled returns if log handler is enabled.

func (*Buffer) Length

func (b *Buffer) Length() int

Length returns the number of bytes of the unread portion of the buffer.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes.

func (*Buffer) SetFormatter

func (b *Buffer) SetFormatter(formatter *Formatter) Handler

SetFormatter sets log formatter.

func (*Buffer) SetLevel

func (b *Buffer) SetLevel(level int) Handler

SetLevel sets log level.

func (*Buffer) SetLevelRange

func (b *Buffer) SetLevelRange(min, max int) Handler

SetLevelRange sets minimum and maximum log level values.

func (*Buffer) SetMaximumLevel

func (b *Buffer) SetMaximumLevel(level int) Handler

SetMaximumLevel sets maximum log level.

func (*Buffer) SetMinimumLevel

func (b *Buffer) SetMinimumLevel(level int) Handler

SetMinimumLevel sets minimum log level.

func (*Buffer) SetStreamHandler

func (b *Buffer) SetStreamHandler(handler StreamHandler) *Buffer

SetStreamHandler sets custom stream handler.

func (*Buffer) String

func (b *Buffer) String() string

String returns the contents of the unread portion of the buffer as a string.

type File

A File represents a log handler object for logging messages to file.

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

func NewFile

func NewFile() *File

NewFile creates a new File log handler object.

func (*File) Close

func (f *File) Close() error

Close closes opened file.

func (*File) Disable

func (f *File) Disable() Handler

Disable disabled log handler.

func (*File) Emit

func (f *File) Emit(record *Record) error

Emit logs messages from Logger to file.

func (*File) Enable

func (f *File) Enable() Handler

Enable enables log handler.

func (*File) GetFlags

func (f *File) GetFlags() int

GetFlags returns file flags.

func (*File) GetFormatter

func (f *File) GetFormatter() *Formatter

GetFormatter returns Formatter.

func (*File) GetLevelRange

func (f *File) GetLevelRange() (min, max int)

GetLevelRange returns minimum and maximum log level values.

func (*File) GetMaximumLevel

func (f *File) GetMaximumLevel() int

GetMaximumLevel returns maximum log level.

func (*File) GetMinimumLevel

func (f *File) GetMinimumLevel() int

GetMinimumLevel returns minimum log level.

func (*File) GetMode

func (f *File) GetMode() os.FileMode

GetMode returns file mode/permissions.

func (*File) GetName

func (f *File) GetName() string

GetName sets file name used for log messages.

func (*File) IsEnabled

func (f *File) IsEnabled() bool

IsEnabled returns if log handler is enabled.

func (*File) Open

func (f *File) Open() (io.WriteCloser, error)

Open file.

func (*File) SetFlags

func (f *File) SetFlags(flags int) *File

SetFlags sets file flags from os package.

func (*File) SetFormatter

func (f *File) SetFormatter(formatter *Formatter) Handler

SetFormatter sets Formatter.

func (*File) SetLevel

func (f *File) SetLevel(level int) Handler

SetLevel sets log level.

func (*File) SetLevelRange

func (f *File) SetLevelRange(min, max int) Handler

SetLevelRange sets minimum and maximum log level values.

func (*File) SetMaximumLevel

func (f *File) SetMaximumLevel(level int) Handler

SetMaximumLevel sets maximum log level.

func (*File) SetMinimumLevel

func (f *File) SetMinimumLevel(level int) Handler

SetMinimumLevel sets minimum log level.

func (*File) SetMode

func (f *File) SetMode(mode os.FileMode) *File

SetMode sets file mode/permissions.

func (*File) SetName

func (f *File) SetName(name string) *File

SetName sets file name used for log messages.

func (*File) SetStreamHandler

func (f *File) SetStreamHandler(handler StreamHandler) *File

SetStreamHandler sets custom stream handler.

type Formatter

A Formatter represents a formatter object used by log handler to format log message.

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

func NewFormatter

func NewFormatter() *Formatter

NewFormatter creates a new Formatter object with default format settings.

func (*Formatter) AddFuncs

func (f *Formatter) AddFuncs(funcs FormatterFuncs) *Formatter

AddFuncs adds template functions to format log message.

func (*Formatter) Format

func (f *Formatter) Format(record *Record) (string, error)

Format returns formatted log message string based on provided log record object.

func (*Formatter) FormatMessage

func (f *Formatter) FormatMessage(record *Record) (string, error)

FormatMessage returns formatted user message string based on provided log record object.

func (*Formatter) FormatTime

func (f *Formatter) FormatTime(record *Record) (string, error)

FormatTime returns formatted date string based on provided log record object.

func (*Formatter) GetDateFormat

func (f *Formatter) GetDateFormat() string

GetDateFormat returns format string used for formatting date in log message.

func (*Formatter) GetFormat

func (f *Formatter) GetFormat() string

GetFormat returns format string used for formatting log message.

func (*Formatter) GetPlaceholder

func (f *Formatter) GetPlaceholder() string

GetPlaceholder returns placeholder string prefix used for automatic and positional placeholders to format log message.

func (*Formatter) Reset

func (f *Formatter) Reset() *Formatter

Reset resets Formatter.

func (*Formatter) SetDateFormat

func (f *Formatter) SetDateFormat(dateFormat string) *Formatter

SetDateFormat sets format string used for formatting date in log message.

func (*Formatter) SetFormat

func (f *Formatter) SetFormat(format string) *Formatter

SetFormat sets format string used for formatting log message.

func (*Formatter) SetPlaceholder

func (f *Formatter) SetPlaceholder(placeholder string) *Formatter

SetPlaceholder sets placeholder string prefix used for automatic and positional placeholders to format log message.

type FormatterFuncs

FormatterFuncs defines map of template functions.

type FormatterFuncs map[string]interface{}

type Handler

Handler defines interface for log handlers.

type Handler interface {
    SetFormatter(formatter *Formatter) Handler

    GetFormatter() *Formatter

    SetLevel(level int) Handler

    SetMinimumLevel(level int) Handler

    GetMinimumLevel() int

    SetMaximumLevel(level int) Handler

    GetMaximumLevel() int

    SetLevelRange(min, max int) Handler

    GetLevelRange() (min, max int)

    Enable() Handler

    Disable() Handler

    IsEnabled() bool

    Emit(record *Record) error

    Close() error
}

func GetHandler

func GetHandler(name string) (Handler, error)

GetHandler returns added log handler by provided name.

type Handlers

Handlers defines map of log handlers.

type Handlers map[string]Handler

func GetHandlers

func GetHandlers() Handlers

GetHandlers returns all added log handlers.

type IDGenerator

IDGenerator type that returns generated ID used in log messages.

type IDGenerator interface {
    Generate() (id string, err error)
}

func GetIDGenerator

func GetIDGenerator() IDGenerator

GetIDGenerator returns ID generator function that is called by logger to generate ID for created log messages.

type Level

Level defines log level information fields.

type Level struct {
    Value int    `json:"value"`
    Name  string `json:"name"`
}

type Logger

A Logger represents an active logging object that generates log messages for different added log handlers. Each logging operations creates and sends lightweight not formatted log message to separate worker thread. It offloads main code from unnecessary resource consuming formatting and I/O operations.

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

func AddFuncs

func AddFuncs(funcs FormatterFuncs) *Logger

AddFuncs adds template functions to format log message to all added log handlers.

func AddHandler

func AddHandler(name string, handler Handler) *Logger

AddHandler sets log handler under provided identifier name.

func Disable

func Disable() *Logger

Disable disabled all added log handlers.

func Emit

func Emit(record *Record) *Logger

Emit emits provided log record to logger worker thread for further formatting and I/O handling from different addded log handlers.

func Enable

func Enable() *Logger

Enable enables all added log handlers.

func Flush

func Flush() *Logger

Flush flushes all log messages.

func Get

func Get() *Logger

Get returns global logger instance.

func New

func New() *Logger

New creates new logger instance with default handlers.

func RemoveHandler

func RemoveHandler(name string) *Logger

RemoveHandler removes added log handler by provided name.

func RemoveHandlers

func RemoveHandlers() *Logger

RemoveHandlers removes all added log handlers.

func Reset

func Reset() *Logger

Reset resets logger to default state and default log handlers.

func ResetFormatters

func ResetFormatters() *Logger

ResetFormatters resets all formatters from added log handlers.

func ResetHandlers

func ResetHandlers() *Logger

ResetHandlers sets logger default log handlers.

func SetDateFormat

func SetDateFormat(format string) *Logger

SetDateFormat sets provided date format string to all added log handlers.

func SetErrorCode

func SetErrorCode(errorCode int) *Logger

SetErrorCode sets error code that is returned during Fatal call. On default it is 1.

func SetFormat

func SetFormat(format string) *Logger

SetFormat sets provided format string to all added log handlers.

func SetFormatter

func SetFormatter(formatter *Formatter) *Logger

SetFormatter sets provided formatter to all added log handlers.

func SetHandler

func SetHandler(name string, handler Handler) *Logger

SetHandler sets a single log handler for logger. It is equivalent to logger.RemoveHandlers().SetHandlers(logger.Handlers{name: handler}).

func SetHandlers

func SetHandlers(handlers Handlers) *Logger

SetHandlers sets log handlers for logger.

func SetIDGenerator

func SetIDGenerator(idGenerator IDGenerator) *Logger

SetIDGenerator sets ID generator function that is called by logger to generate ID for created log messages.

func SetLevel

func SetLevel(level int) *Logger

SetLevel sets log level to all added log handlers.

func SetLevelRange

func SetLevelRange(min, max int) *Logger

SetLevelRange sets minimum and maximum log level values to all added log handlers.

func SetMaximumLevel

func SetMaximumLevel(level int) *Logger

SetMaximumLevel sets maximum log level to all added log handlers.

func SetMinimumLevel

func SetMinimumLevel(level int) *Logger

SetMinimumLevel sets minimum log level to all added log handlers.

func SetName

func SetName(name string) *Logger

SetName sets logger name.

func SetPlaceholder

func SetPlaceholder(placeholder string) *Logger

SetPlaceholder sets provided placeholder string to all added log handlers.

func (*Logger) AddFuncs

func (l *Logger) AddFuncs(funcs FormatterFuncs) *Logger

AddFuncs adds template functions to format log message to all added log handlers.

func (*Logger) AddHandler

func (l *Logger) AddHandler(name string, handler Handler) *Logger

AddHandler sets log handler under provided identifier name.

func (*Logger) Alert

func (l *Logger) Alert(message string, arguments ...interface{})

Alert logs messages for alert conditions. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func (*Logger) Close

func (l *Logger) Close() error

Close closes all added log handlers.

func (*Logger) CloseDefer

func (l *Logger) CloseDefer()

CloseDefer is a small helper function that invokes the .Close() method and it does an error checking with logging. Useful when using with the defer keyword to avoid creating an anonymous function wrapper only to check for an error manually and passing the errcheck linter.

func (*Logger) Critical

func (l *Logger) Critical(message string, arguments ...interface{})

Critical logs messages for critical conditions. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func (*Logger) Debug

func (l *Logger) Debug(message string, arguments ...interface{})

Debug logs debugging messages. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func (*Logger) Disable

func (l *Logger) Disable() *Logger

Disable disabled all added log handlers.

func (*Logger) Emit

func (l *Logger) Emit(record *Record) *Logger

Emit emits provided log record to logger worker thread for further formatting and I/O handling from different addded log handlers.

func (*Logger) Enable

func (l *Logger) Enable() *Logger

Enable enables all added log handlers.

func (*Logger) Error

func (l *Logger) Error(message string, arguments ...interface{})

Error logs messages for error conditions. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different log handlers.

func (*Logger) Fatal

func (l *Logger) Fatal(message string, arguments ...interface{})

Fatal logs messages for fatal conditions. It stops logger worker thread and it exists the application with an error code. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func (*Logger) Flush

func (l *Logger) Flush() *Logger

Flush flushes all log messages.

func (*Logger) GetErrorCode

func (l *Logger) GetErrorCode() int

GetErrorCode returns error code.

func (*Logger) GetHandler

func (l *Logger) GetHandler(name string) (Handler, error)

GetHandler returns added log handler by provided name.

func (*Logger) GetHandlers

func (l *Logger) GetHandlers() Handlers

GetHandlers returns all added log handlers.

func (*Logger) GetIDGenerator

func (l *Logger) GetIDGenerator() IDGenerator

GetIDGenerator returns ID generator function that is called by logger to generate ID for created log messages.

func (*Logger) GetName

func (l *Logger) GetName() string

GetName returns logger name.

func (*Logger) Info

func (l *Logger) Info(message string, arguments ...interface{})

Info logs informational messages. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func (*Logger) IsEnabled

func (l *Logger) IsEnabled() bool

IsEnabled returns true if at least one of added log handlers is enabled.

func (*Logger) Log

func (l *Logger) Log(level int, levelName, message string, arguments ...interface{})

Log logs messages with user defined log level value and name. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func (*Logger) LogMessage

func (l *Logger) LogMessage(level int, levelName, message string, arguments ...interface{})

LogMessage logs message with defined log level value and name. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers. Use this method in custom log wrapper methods.

func (*Logger) Notice

func (l *Logger) Notice(message string, arguments ...interface{})

Notice logs messages for significant conditions. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func (*Logger) Panic

func (l *Logger) Panic(message string, arguments ...interface{})

Panic logs messages for fatal conditions. It stops logger worker thread and it exists the application with a panic. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func (*Logger) RemoveHandler

func (l *Logger) RemoveHandler(name string) *Logger

RemoveHandler removes added log handler by provided name.

func (*Logger) RemoveHandlers

func (l *Logger) RemoveHandlers() *Logger

RemoveHandlers removes all added log handlers.

func (*Logger) Reset

func (l *Logger) Reset() *Logger

Reset resets logger to default state and default log handlers.

func (*Logger) ResetFormatters

func (l *Logger) ResetFormatters() *Logger

ResetFormatters resets all formatters from added log handlers.

func (*Logger) ResetHandlers

func (l *Logger) ResetHandlers() *Logger

ResetHandlers sets logger default log handlers.

func (*Logger) SetDateFormat

func (l *Logger) SetDateFormat(format string) *Logger

SetDateFormat sets provided date format string to all added log handlers.

func (*Logger) SetErrorCode

func (l *Logger) SetErrorCode(errorCode int) *Logger

SetErrorCode sets error code that is returned during Fatal call. On default it is 1.

func (*Logger) SetFormat

func (l *Logger) SetFormat(format string) *Logger

SetFormat sets provided format string to all added log handlers.

func (*Logger) SetFormatter

func (l *Logger) SetFormatter(formatter *Formatter) *Logger

SetFormatter sets provided formatter to all added log handlers.

func (*Logger) SetHandler

func (l *Logger) SetHandler(name string, handler Handler) *Logger

SetHandler sets a single log handler for logger. It is equivalent to logger.RemoveHandlers().SetHandlers(logger.Handlers{name: handler}).

func (*Logger) SetHandlers

func (l *Logger) SetHandlers(handlers Handlers) *Logger

SetHandlers sets log handlers for logger.

func (*Logger) SetIDGenerator

func (l *Logger) SetIDGenerator(idGenerator IDGenerator) *Logger

SetIDGenerator sets ID generator function that is called by logger to generate ID for created log messages.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level int) *Logger

SetLevel sets log level to all added log handlers.

func (*Logger) SetLevelRange

func (l *Logger) SetLevelRange(min, max int) *Logger

SetLevelRange sets minimum and maximum log level values to all added log handlers.

func (*Logger) SetMaximumLevel

func (l *Logger) SetMaximumLevel(level int) *Logger

SetMaximumLevel sets maximum log level to all added log handlers.

func (*Logger) SetMinimumLevel

func (l *Logger) SetMinimumLevel(level int) *Logger

SetMinimumLevel sets minimum log level to all added log handlers.

func (*Logger) SetName

func (l *Logger) SetName(name string) *Logger

SetName sets logger name.

func (*Logger) SetPlaceholder

func (l *Logger) SetPlaceholder(placeholder string) *Logger

SetPlaceholder sets provided placeholder string to all added log handlers.

func (*Logger) Trace

func (l *Logger) Trace(message string, arguments ...interface{})

Trace logs finer-grained informational messages than the Debug. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

func (*Logger) Warning

func (l *Logger) Warning(message string, arguments ...interface{})

Warning logs messages for warning conditions that can be potentially harmful. It creates and sends lightweight not formatted log messages to separate running logger thread for further formatting and I/O handling from different added log handlers.

type Named

Named is used as named string placeholders for logger functions.

type Named map[string]interface{}

type Opener

Opener implements Open method.

type Opener interface {
    Open() (io.WriteCloser, error)
}

type Record

Record defines log record fields created by Logger and it is used by Formatter to format log message based on these fields.

type Record struct {
    ID        string    `json:"id"`
    Type      string    `json:"type"`
    Name      string    `json:"name"`
    Time      time.Time `json:"-"`
    Level     Level     `json:"level"`
    Address   string    `json:"address"`
    Hostname  string    `json:"hostname"`
    Message   string    `json:"message"`
    File      Source    `json:"file"`
    Arguments Arguments `json:"arguments"`
    Timestamp Timestamp `json:"timestamp"`
    // contains filtered or unexported fields
}

func (*Record) FromJSON

func (r *Record) FromJSON(data []byte) error

FromJSON unpacks data from JSON.

func (*Record) GetMessage

func (r *Record) GetMessage() (string, error)

GetMessage returns formatted message.

func (*Record) ToJSON

func (r *Record) ToJSON() ([]byte, error)

ToJSON packs data to JSON.

type RuntimeError

RuntimeError defines runtime error with returned error message, file name, file line number and function name.

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

func NewRuntimeError

func NewRuntimeError(message string, arguments ...interface{}) *RuntimeError

NewRuntimeError creates new RuntimeError object.

func NewRuntimeErrorBase

func NewRuntimeErrorBase(skipCall int, message string, arguments ...interface{}) *RuntimeError

NewRuntimeErrorBase creates new RuntimeError object using custom skip call value.

func (*RuntimeError) Error

func (r *RuntimeError) Error() string

Error returns formatted error string with message, file name, file line number and function name.

func (*RuntimeError) Unwrap

func (r *RuntimeError) Unwrap() error

Unwrap wrapped error.

type Source

Source defines log file information fields.

type Source struct {
    Function string `json:"function"`
    Name     string `json:"name"`
    Path     string `json:"-"`
    Line     int    `json:"line"`
}

type Stream

A Stream represents a log handler object for logging messages using stream object.

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

func NewStderr

func NewStderr() *Stream

NewStderr created a new Stderr log handler object.

func NewStdout

func NewStdout() *Stream

NewStdout created a new Stdout log handler object.

func NewStream

func NewStream() *Stream

NewStream creates a new Stream log handler object.

func (*Stream) Close

func (s *Stream) Close() error

Close closes I/O stream.

func (*Stream) Disable

func (s *Stream) Disable() Handler

Disable disabled log handler.

func (*Stream) Emit

func (s *Stream) Emit(record *Record) error

Emit logs messages from logger using I/O stream.

func (*Stream) Enable

func (s *Stream) Enable() Handler

Enable enables log handler.

func (*Stream) GetFormatter

func (s *Stream) GetFormatter() *Formatter

GetFormatter returns Formatter.

func (*Stream) GetLevelRange

func (s *Stream) GetLevelRange() (min, max int)

GetLevelRange returns minimum and maximum log level values.

func (*Stream) GetMaximumLevel

func (s *Stream) GetMaximumLevel() int

GetMaximumLevel returns maximum log level.

func (*Stream) GetMinimumLevel

func (s *Stream) GetMinimumLevel() int

GetMinimumLevel returns minimum log level.

func (*Stream) IsEnabled

func (s *Stream) IsEnabled() bool

IsEnabled returns if log handler is enabled.

func (*Stream) Lock

func (s *Stream) Lock()

Lock locks stream.

func (*Stream) RLock

func (s *Stream) RLock()

RLock locks stream.

func (*Stream) RUnlock

func (s *Stream) RUnlock()

RUnlock locks stream.

func (*Stream) Reopen

func (s *Stream) Reopen() *Stream

Reopen reopens stream.

func (*Stream) SetFormatter

func (s *Stream) SetFormatter(formatter *Formatter) Handler

SetFormatter sets Formatter.

func (*Stream) SetLevel

func (s *Stream) SetLevel(level int) Handler

SetLevel sets log level.

func (*Stream) SetLevelRange

func (s *Stream) SetLevelRange(min, max int) Handler

SetLevelRange sets minimum and maximum log level values.

func (*Stream) SetMaximumLevel

func (s *Stream) SetMaximumLevel(level int) Handler

SetMaximumLevel sets maximum log level.

func (*Stream) SetMinimumLevel

func (s *Stream) SetMinimumLevel(level int) Handler

SetMinimumLevel sets minimum log level.

func (*Stream) SetOpener

func (s *Stream) SetOpener(opener Opener) *Stream

SetOpener sets opener interface.

func (*Stream) SetStreamHandler

func (s *Stream) SetStreamHandler(handler StreamHandler) *Stream

SetStreamHandler sets custom stream handler.

func (*Stream) SetWriteCloser

func (s *Stream) SetWriteCloser(writeCloser io.WriteCloser) error

SetWriteCloser sets new writer and closer to stream.

func (*Stream) SetWriter

func (s *Stream) SetWriter(writer io.Writer) error

SetWriter sets new writer to stream.

func (*Stream) Unlock

func (s *Stream) Unlock()

Unlock locks stream.

type StreamHandler

StreamHandler defines a custom stream handler for writing log records with writer.

type StreamHandler func(writer io.Writer, record *Record, formatter *Formatter) error

type Syslog

A Syslog represents a log handler object for logging messages to running Syslog server.

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

func NewSyslog

func NewSyslog() *Syslog

NewSyslog creates a new Syslog log handler object.

func (*Syslog) Close

func (s *Syslog) Close() error

Close closes communication to Syslog server.

func (*Syslog) Disable

func (s *Syslog) Disable() Handler

Disable disabled log handler.

func (*Syslog) Emit

func (s *Syslog) Emit(record *Record) error

Emit logs messages from Logger to Syslog server.

func (*Syslog) Enable

func (s *Syslog) Enable() Handler

Enable enables log handler.

func (*Syslog) GetAddress

func (s *Syslog) GetAddress() string

GetAddress returns IP address or hostname that is used to communicate with Syslog server.

func (*Syslog) GetFormatter

func (s *Syslog) GetFormatter() *Formatter

GetFormatter returns Formatter.

func (*Syslog) GetLevelRange

func (s *Syslog) GetLevelRange() (min, max int)

GetLevelRange returns minimum and maximum log level values.

func (*Syslog) GetMaximumLevel

func (s *Syslog) GetMaximumLevel() int

GetMaximumLevel returns maximum log level.

func (*Syslog) GetMinimumLevel

func (s *Syslog) GetMinimumLevel() int

GetMinimumLevel returns minimum log level.

func (*Syslog) GetNetwork

func (s *Syslog) GetNetwork() string

GetNetwork returns network type like "udp" or "tcp" that is used to communicate with Syslog server.

func (*Syslog) GetPort

func (s *Syslog) GetPort() int

GetPort returns port number that is used to communicate with Syslog server.

func (*Syslog) GetTimeout

func (s *Syslog) GetTimeout() time.Duration

GetTimeout returns connection timeout.

func (*Syslog) IsEnabled

func (s *Syslog) IsEnabled() bool

IsEnabled returns if log handler is enabled.

func (*Syslog) Open

func (s *Syslog) Open() (io.WriteCloser, error)

Open opens new connection.

func (*Syslog) SetAddress

func (s *Syslog) SetAddress(address string) *Syslog

SetAddress sets IP address or hostname that is used to communicate with Syslog server.

func (*Syslog) SetFormatter

func (s *Syslog) SetFormatter(formatter *Formatter) Handler

SetFormatter sets Formatter.

func (*Syslog) SetLevel

func (s *Syslog) SetLevel(level int) Handler

SetLevel sets log level.

func (*Syslog) SetLevelRange

func (s *Syslog) SetLevelRange(min, max int) Handler

SetLevelRange sets minimum and maximum log level values.

func (*Syslog) SetMaximumLevel

func (s *Syslog) SetMaximumLevel(level int) Handler

SetMaximumLevel sets maximum log level.

func (*Syslog) SetMinimumLevel

func (s *Syslog) SetMinimumLevel(level int) Handler

SetMinimumLevel sets minimum log level.

func (*Syslog) SetNetwork

func (s *Syslog) SetNetwork(network string) *Syslog

SetNetwork sets network type like "udp" or "tcp" that is used to communicate with Syslog server.

func (*Syslog) SetPort

func (s *Syslog) SetPort(port int) *Syslog

SetPort sets port number that is used to communicate with Syslog server.

func (*Syslog) SetTimeout

func (s *Syslog) SetTimeout(timeout time.Duration) *Syslog

SetTimeout sets connection timeout.

type Timestamp

Timestamp defines log timestamp information fields.

type Timestamp struct {
    Created string `json:"created"`
}

type UUID4

An UUID4 represents uui4 generator.

type UUID4 struct{}

func NewUUID4

func NewUUID4() *UUID4

NewUUID4 create a new UUID4 object.

func (*UUID4) Generate

func (u *UUID4) Generate() (id string, err error)

Generate generates new UUID4.

type Worker

A Worker represents an active logger worker thread. It handles formatting received log messages and I/O operations.

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

func GetWorker

func GetWorker() *Worker

GetWorker returns logger worker instance. First call to it creates and starts logger worker thread.

func NewWorker

func NewWorker() *Worker

NewWorker creates a new Worker object.

func (*Worker) Flush

func (w *Worker) Flush() *Worker

Flush flushes all log messages.

func (*Worker) SetQueueLength

func (w *Worker) SetQueueLength(length int) *Worker

SetQueueLength sets logger worker thread queue length for log messages.