logz

package
v0.0.0-...-841f565 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFormatter

func DefaultFormatter(entry Entry) ([]byte, error)

DefaultFormatter a simple Entry format function

func JsonFormatter

func JsonFormatter(entry Entry) ([]byte, error)

JsonFormatter formats log line as json

func LevelColor

func LevelColor(level Level) string

func SuggestedFilePath

func SuggestedFilePath(tool, filename string) (string, error)

SuggestedFilePath returns the suggested path for a log file based on the host operating system

Types

type Builder

type Builder func() (transport Transport, err error)

Builder initializes a transport with a context If a builder returns an error its assumed that the log destination cannot be used and The error will be surfaced to the caller

type Entry

type Entry struct {
	// Contains all the fields set by the user.
	Fields Fields `json:"data,omitempty"`

	// Timestamp at which the log entry was created
	Timestamp time.Time `json:"timestamp"`

	// Level the log entry was logged at: Trace, Debug, Info, Warn, Error, Fatal or Panic
	// This field will be set on entry firing and the value will be equal to the one in Logger struct field.
	Level Level `json:"level"`

	// Calling method, with package name
	Caller string `json:"caller,omitempty"`

	// Message passed to Trace, Debug, Info, Warn, Error, Fatal or Panic
	Message string `json:"message"`

	Error error `json:"error,omitempty"`
}

type FieldLogger

type FieldLogger interface {
	StdLogger
	io.Writer
	Child(opts ...Option) FieldLogger
	Extend(opts ...Option) FieldLogger
	WithFields(Fields) FieldLogger
}

FieldLogger an extension ot StdLogger that appends contextual field data

type Fields

type Fields = map[string]interface{}

Fields type, used to pass to `WithFields`.

type Formatter

type Formatter func(entry Entry) ([]byte, error)

Formatter is an interface that is expected to receive an Entry and serialize it into a []byte

type Injector

type Injector interface {
	UseLogger(logger FieldLogger)
}

Injector an interface used to inject a field logger

type Level

type Level uint32

Level type

const (
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service./
	ErrorLevel Level = iota
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel
)

These are the different logging levels. You can set the logging level to log on your instance of logger, obtained with `logrus.New()`.

func ParseLevel

func ParseLevel(lvl string) (Level, error)

ParseLevel takes a string level and returns the Logrus log level constant.

func (Level) MarshalText

func (level Level) MarshalText() ([]byte, error)

func (Level) String

func (level Level) String() string

Convert the Level to a string. E.g. PanicLevel becomes "panic".

func (*Level) UnmarshalText

func (level *Level) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type MockLogger

type MockLogger struct {
	mock.Mock
}

MockLogger used for test

func (*MockLogger) Child

func (m *MockLogger) Child(opts ...Option) FieldLogger

func (*MockLogger) Debug

func (m *MockLogger) Debug(opts ...interface{})

func (*MockLogger) Debugf

func (m *MockLogger) Debugf(format string, opts ...interface{})

func (*MockLogger) Error

func (m *MockLogger) Error(opts ...interface{})

func (*MockLogger) Errorf

func (m *MockLogger) Errorf(format string, opts ...interface{})

func (*MockLogger) Extend

func (m *MockLogger) Extend(opts ...Option) FieldLogger

func (*MockLogger) Info

func (m *MockLogger) Info(opts ...interface{})

func (*MockLogger) Infof

func (m *MockLogger) Infof(format string, opts ...interface{})

func (*MockLogger) Trace

func (m *MockLogger) Trace(opts ...interface{})

func (*MockLogger) Tracef

func (m *MockLogger) Tracef(format string, opts ...interface{})

func (*MockLogger) Warn

func (m *MockLogger) Warn(opts ...interface{})

func (*MockLogger) Warnf

func (m *MockLogger) Warnf(format string, opts ...interface{})

func (*MockLogger) WithFields

func (m *MockLogger) WithFields(f Fields) FieldLogger

func (*MockLogger) Write

func (m *MockLogger) Write(p []byte) (n int, err error)

type NoOpLogger

type NoOpLogger struct{}

NoOpLogger for testing purposes

func (NoOpLogger) Child

func (m NoOpLogger) Child(_ ...Option) FieldLogger

func (NoOpLogger) Debug

func (m NoOpLogger) Debug(_ ...interface{})

func (NoOpLogger) Debugf

func (m NoOpLogger) Debugf(_ string, _ ...interface{})

func (NoOpLogger) Error

func (m NoOpLogger) Error(_ ...interface{})

func (NoOpLogger) Errorf

func (m NoOpLogger) Errorf(_ string, _ ...interface{})

func (NoOpLogger) Extend

func (m NoOpLogger) Extend(_ ...Option) FieldLogger

func (NoOpLogger) Info

func (m NoOpLogger) Info(_ ...interface{})

func (NoOpLogger) Infof

func (m NoOpLogger) Infof(_ string, _ ...interface{})

func (NoOpLogger) Trace

func (m NoOpLogger) Trace(_ ...interface{})

func (NoOpLogger) Tracef

func (m NoOpLogger) Tracef(_ string, _ ...interface{})

func (NoOpLogger) Warn

func (m NoOpLogger) Warn(_ ...interface{})

func (NoOpLogger) Warnf

func (m NoOpLogger) Warnf(_ string, _ ...interface{})

func (NoOpLogger) WithFields

func (m NoOpLogger) WithFields(Fields) FieldLogger

func (NoOpLogger) Write

func (m NoOpLogger) Write(p []byte) (n int, err error)

type Option

type Option func(w *Writer) error

Option is a function that applies mutations to a *Writer

func WithDisableAutoConsume

func WithDisableAutoConsume() Option

WithDisableAutoConsume turns off auto consumption of messages By default the logger will start multiplexing This should really only be used in tests that need to verify channel behavior

func WithFields

func WithFields(fields Fields) Option

WithFields an option that applies fields to a *Writer

func WithLevel

func WithLevel(level Level) Option

WithLevel sets the level for this logger

func WithLevelString

func WithLevelString(levelStr string) Option

WithLevelString sets the level for this logger

func WithMux

func WithMux(builders ...Builder) Option

WithMux multiplexes log.Entry to all provided Observer

func WithParent

func WithParent(parent chan ThreadSaveEntry) Option

WithParent sets the level for this logger

func WithQueue

func WithQueue(queue chan ThreadSaveEntry) Option

WithQueue an Option that applies a queue channel to a *Writer

type StdLogger

type StdLogger interface {
	Error(opts ...interface{})
	Errorf(format string, opts ...interface{})

	Warn(opts ...interface{})
	Warnf(format string, opts ...interface{})

	Info(opts ...interface{})
	Infof(format string, opts ...interface{})

	Debug(opts ...interface{})
	Debugf(format string, opts ...interface{})

	Trace(opts ...interface{})
	Tracef(format string, opts ...interface{})
}

StdLogger a standard interface defining methods for application logging

type ThreadSaveEntry

type ThreadSaveEntry struct {
	// Contains all the fields set by the user.
	Fields [][]string `json:"data,omitempty"`

	// Timestamp at which the log entry was created
	Timestamp time.Time `json:"timestamp"`

	// Level the log entry was logged at: Trace, Debug, Info, Warn, Error, Fatal or Panic
	// This field will be set on entry firing and the value will be equal to the one in Logger struct field.
	Level Level `json:"level"`

	// Calling method, with package name
	Caller string `json:"caller,omitempty"`

	// Message passed to Trace, Debug, Info, Warn, Error, Fatal or Panic
	Message string `json:"message"`

	Error error `json:"error,omitempty"`
}

type Transport

type Transport interface {
	Name() string
	Stream() chan ThreadSaveEntry
	Write(entry Entry) error
	Cleanup() error
}

Transport writes an entry to a given destination

type WriteFunc

type WriteFunc func(Entry) error

WriteFunc is a function passed to the Writer.RegisterTransport function. This method is used in a goroutine and is passed each log entry as they arrive. The behavior of the Writer is to attempt to deliver messages to each WriteFunc as fast as possible. If a WriteFunc cannot keep up and backpressure on the queue reaches 1000 messages will be dropped from this WriteFunc stream until it can handle additional capacity

type Writer

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

Writer implements the FieldLogger interface and provides a channel based write mechanism An Option can be provided that observes the queue and transports Entry records to a log sink

func New

func New(ctx context.Context, opts ...Option) *Writer

New builds a new *Writer

func (*Writer) Child

func (w *Writer) Child(opts ...Option) FieldLogger

Child returns a new logger instance that inherits from its parent By default it receives its parents fields and level All log messages will be propagated to the parent and written to its parents transports This new instance may receive its own transports which will not receive logs from the parent This logger may be closed independently of its parent

func (*Writer) Close

func (w *Writer) Close()

Close stops new Entry records from being produced by the logger allowing the caller to Wait until all messages have been handled by all Observers

func (*Writer) Debug

func (w *Writer) Debug(opts ...interface{})

Debug writes a debug level message

func (*Writer) Debugf

func (w *Writer) Debugf(format string, opts ...interface{})

Debugf writes a debug level message with formatting

func (*Writer) Error

func (w *Writer) Error(opts ...interface{})

Error writes an error level message

func (*Writer) Errorf

func (w *Writer) Errorf(format string, opts ...interface{})

Errorf writes an error level message with formatting

func (*Writer) Extend

func (w *Writer) Extend(opts ...Option) FieldLogger

Extend is an lighter-weight alternative to Child that allows reuse of parent logger options This will not create transports but instead reuse the parents queue

func (*Writer) Info

func (w *Writer) Info(opts ...interface{})

Info writes an info level message

func (*Writer) Infof

func (w *Writer) Infof(format string, opts ...interface{})

Infof writes an info level message with formatting

func (*Writer) InitError

func (w *Writer) InitError() error

InitError check this property to validate that calling New did not result in a error from an option

func (*Writer) RegisterTransport

func (w *Writer) RegisterTransport(transport Transport, filters ...func(entry Entry) bool) error

RegisterTransport accepts a writeFunc and kicks off a go routine which ensures delivery of log messages to the new writeFunc WriteFunc is used in a goroutine and is passed each log entry as they arrive. The behavior of the Writer is to attempt to deliver messages to each WriteFunc as fast as possible. If a WriteFunc cannot keep up and backpressure on the queue reaches 1000 messages will be dropped from this WriteFunc stream until it can handle additional capacity The WriteFunc should only return an error if it should no longer receive log entries

func (*Writer) Trace

func (w *Writer) Trace(opts ...interface{})

Trace writes a trace level message

func (*Writer) Tracef

func (w *Writer) Tracef(format string, opts ...interface{})

Tracef writes a trace level message with formatting

func (*Writer) Wait

func (w *Writer) Wait() error

Wait waits for all transports in the error group to complete before returning

func (*Writer) Warn

func (w *Writer) Warn(opts ...interface{})

Warn writes a warn level message

func (*Writer) Warnf

func (w *Writer) Warnf(format string, opts ...interface{})

Warnf writes a warn level message with formatting

func (*Writer) WithFields

func (w *Writer) WithFields(fields Fields) FieldLogger

WithFields is a lighter-weight alternative to Child The Child method is very heavy and creates a new instance with channels, transports, and the works. When used improperly it can cause memory leaks and the most common task is to create a logger with additional fields

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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