logging

package
v0.0.0-...-b5d9cbe Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package logging provides the standard logging mechanism for Tast.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttachLogger

func AttachLogger(ctx context.Context, logger Logger) context.Context

AttachLogger creates a new context with logger attached. Logs emitted via the new context are propagated to the parent context.

func AttachLoggerNoPropagation

func AttachLoggerNoPropagation(ctx context.Context, logger Logger) context.Context

AttachLoggerNoPropagation creates a new context with logger attached. In contrast to AttachLogger, logs emitted via the new context are not propagated to the parent context.

func Debug

func Debug(ctx context.Context, args ...interface{})

Debug emits a log with debug level.

func Debugf

func Debugf(ctx context.Context, format string, args ...interface{})

Debugf is similar to Debug but formats its arguments using fmt.Sprintf.

func HasLogger

func HasLogger(ctx context.Context) bool

HasLogger checks if any logger is attached to ctx.

func Info

func Info(ctx context.Context, args ...interface{})

Info emits a log with info level.

func Infof

func Infof(ctx context.Context, format string, args ...interface{})

Infof is similar to Info but formats its arguments using fmt.Sprintf.

func ReplaceInvalidUTF8

func ReplaceInvalidUTF8(msg string) string

ReplaceInvalidUTF8 replaces all invalid UTF-8 characters from a string.

func SetLogPrefix

func SetLogPrefix(ctx context.Context, prefix string) context.Context

SetLogPrefix takes in a prefix string to prepend to all logs coming through the logger

func UnsetLogPrefix

func UnsetLogPrefix(ctx context.Context) context.Context

UnsetLogPrefix removes the prefix that is prepended to all logs coming through the logger

Types

type FuncLogger

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

FuncLogger is a Logger that calls a function.

All calls to the underlying function are synchronized.

func NewFuncLogger

func NewFuncLogger(f func(level Level, ts time.Time, msg string)) *FuncLogger

NewFuncLogger creates a new FuncLogger.

func (*FuncLogger) Log

func (l *FuncLogger) Log(level Level, ts time.Time, msg string)

Log sends a log to the associated sink.

type FuncSink

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

FuncSink is a Sink that calls a function. You probably do not want this, you want NewFuncLogger instead.

All calls to the underlying function are synchronized.

func NewFuncSink

func NewFuncSink(f func(msg string)) *FuncSink

NewFuncSink creates a new FuncSink from a function.

func (*FuncSink) Log

func (s *FuncSink) Log(msg string)

Log consumes a log as a function call.

type Level

type Level int

Level indicates a logging level. A larger level value means a log is more important.

const (
	// LevelDebug represents the DEBUG level.
	LevelDebug Level = iota
	// LevelInfo represents the INFO level.
	LevelInfo
)

type Logger

type Logger interface {
	// Log gets called for a log entry.
	Log(level Level, ts time.Time, msg string)
}

Logger defines the interface for loggers that consume logs sent via context.Context.

You can create a new context with a Logger attached by AttachLogger. The attached logger will consume all logs sent to the context, as well as those logs sent to its descendant contexts as long as you don't attach another logger to a descendant context with no propagation. See AttachLogger for more details.

type MultiLogger

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

MultiLogger is a Logger that copies logs to multiple underlying loggers. A logger can be added and removed from MultiLogger at any time.

func NewMultiLogger

func NewMultiLogger(loggers ...Logger) *MultiLogger

NewMultiLogger creates a new MultiLogger with a specified initial set of underlying loggers.

func (*MultiLogger) AddLogger

func (ml *MultiLogger) AddLogger(logger Logger)

AddLogger adds a logger to the set of underlying loggers.

func (*MultiLogger) Log

func (ml *MultiLogger) Log(level Level, ts time.Time, msg string)

Log copies a log to the current underlying loggers.

func (*MultiLogger) RemoveLogger

func (ml *MultiLogger) RemoveLogger(logger Logger)

RemoveLogger removes a logger from the set of underlying loggers.

type Sink

type Sink interface {
	// Log gets called for a log entry.
	Log(msg string)
}

Sink represents a destination of logs, e.g. a log file or console.

type SinkLogger

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

SinkLogger is a Logger that processes logs by a Sink.

func NewSinkLogger

func NewSinkLogger(level Level, timestamp bool, sink Sink) *SinkLogger

NewSinkLogger creates a new logger that filters and formats the messages and logs them to a sink.

level specifies the minimum level of logs the sink should get notified of. If timestamp is true, a timestamp is prepended to a log before it is sent to the sink.

func (*SinkLogger) Log

func (l *SinkLogger) Log(level Level, ts time.Time, msg string)

Log sends a log to the associated sink.

type SyslogLogger

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

SyslogLogger is a Logger that routes logs to syslog.

func NewSyslogLogger

func NewSyslogLogger() (*SyslogLogger, error)

NewSyslogLogger creates a new SyslogLogger. It returns an error if it fails to connect to the syslog endpoint.

func (*SyslogLogger) Close

func (l *SyslogLogger) Close() error

Close closes the underlying connection to the syslog endpoint.

func (*SyslogLogger) Log

func (l *SyslogLogger) Log(level Level, ts time.Time, msg string)

Log sends a log to syslog.

type WriterSink

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

WriterSink is a Sink that writes logs to io.Writer.

All writes to io.Writer are synchronized.

func NewWriterSink

func NewWriterSink(w io.Writer) *WriterSink

NewWriterSink creates a new WriterSink from io.Writer.

func (*WriterSink) Log

func (s *WriterSink) Log(msg string)

Log writes a log to the underlying io.Writer.

Directories

Path Synopsis
Package loggingtest provides logging utilities for unit tests.
Package loggingtest provides logging utilities for unit tests.

Jump to

Keyboard shortcuts

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