Documentation ¶
Overview ¶
Package log contains all the structs to log TTN.
Index ¶
- Variables
- func Debug(ctx context.Context, msg string)
- func Debugf(ctx context.Context, msg string, v ...interface{})
- func Error(ctx context.Context, msg string)
- func Errorf(ctx context.Context, msg string, v ...interface{})
- func Fatal(ctx context.Context, msg string)
- func Fatalf(ctx context.Context, msg string, v ...interface{})
- func Info(ctx context.Context, msg string)
- func Infof(ctx context.Context, msg string, v ...interface{})
- func NewContext(ctx context.Context, logger Interface) context.Context
- func NewContextWithField(ctx context.Context, k string, v interface{}) context.Context
- func NewContextWithFields(ctx context.Context, f Fielder) context.Context
- func Warn(ctx context.Context, msg string)
- func Warnf(ctx context.Context, msg string, v ...interface{})
- type Entry
- type F
- type Fielder
- type Handler
- type HandlerFunc
- type Interface
- type Level
- type Logger
- func (l *Logger) Debug(args ...interface{})
- func (l *Logger) Debugf(msg string, v ...interface{})
- func (l *Logger) Error(args ...interface{})
- func (l *Logger) Errorf(msg string, v ...interface{})
- func (l *Logger) Fatal(args ...interface{})
- func (l *Logger) Fatalf(msg string, v ...interface{})
- func (l *Logger) Info(args ...interface{})
- func (l *Logger) Infof(msg string, v ...interface{})
- func (l *Logger) Use(middleware Middleware)
- func (l *Logger) Warn(args ...interface{})
- func (l *Logger) Warnf(msg string, v ...interface{})
- func (l *Logger) WithError(err error) Interface
- func (l *Logger) WithField(name string, val interface{}) Interface
- func (l *Logger) WithFields(fields Fielder) Interface
- type Middleware
- type MiddlewareFunc
- type Option
- type Stack
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidLevel = errors.New("Invalid log level")
ErrInvalidLevel indicates an invalid log level
var Noop = &noop{}
Noop just does nothing.
var NoopHandler = &noopHandler{}
NoopHandler is a handler that does nothing.
Functions ¶
func NewContext ¶
NewContext returns a derived context with the logger set.
func NewContextWithField ¶
NewContextWithField returns a derived context with the given field added to the logger.
func NewContextWithFields ¶
NewContextWithFields returns a derived context with the given fields added to the logger.
Types ¶
type F ¶
type F struct {
// contains filtered or unexported fields
}
F is a Fielder that uses structural sharing to avoid copying entries. Setting a key is O(1), getting a key is O(n) (where n is the number of entries), but we only use this to accumulate fields so that's ok.
func (*F) Fields ¶
Fields implements Fielder. Returns all fields in O(n), where n is the number of entries in the map.
func (*F) WithError ¶
WithError returns new fields that contain the passed error and all its fields (if any).
func (*F) WithFields ¶
WithFields returns a new fielder that has all the fields of the other fielder.
type Fielder ¶
type Fielder interface {
Fields() map[string]interface{}
}
Fielder is the interface for anything that can have fields.
type HandlerFunc ¶
HandlerFunc is a function that implements Handler.
func (HandlerFunc) HandleLog ¶
func (fn HandlerFunc) HandleLog(e Entry) error
HandleLog implements Handler.
type Interface ¶
type Interface interface { Debug(args ...interface{}) Info(args ...interface{}) Warn(args ...interface{}) Error(args ...interface{}) Fatal(args ...interface{}) Debugf(msg string, v ...interface{}) Infof(msg string, v ...interface{}) Warnf(msg string, v ...interface{}) Errorf(msg string, v ...interface{}) Fatalf(msg string, v ...interface{}) WithField(string, interface{}) Interface WithFields(Fielder) Interface WithError(error) Interface }
Interface is the interface for logging TTN.
func FromContext ¶
FromContext returns the logger that is attached to the context or returns the Noop logger if it does not exist
type Level ¶
type Level int8
Level is the level of logging.
const ( // DebugLevel is the log level for debug messages, usually turned of in production. DebugLevel Level // InfoLevel is the log level for informational messages. InfoLevel // WarnLevel is the log level for warnings. // Warnings are more important than info but do not need individual human review. WarnLevel // ErrorLevel is the log level for high priority error messages. // When everything is running smoothly, an application should not be logging error level messages. ErrorLevel // FatalLevel the log level for unrecoverable errors. // Using this level will exit the program. FatalLevel )
func ParseLevel ¶
ParseLevel parses a string into a log level or returns an error otherwise.
func (Level) MarshalText ¶
MarshalText implments encoding.TextMarshaller.
func (*Level) UnmarshalConfigString ¶
UnmarshalConfigString implements config.Configurable.
func (*Level) UnmarshalText ¶
UnmarshalText implments encoding.TextMarshaller.
type Logger ¶
Logger implements Stack.
func (*Logger) Use ¶
func (l *Logger) Use(middleware Middleware)
Use installs the handler middleware.
func (*Logger) WithFields ¶
WithFields implements log.Interface.
type Middleware ¶
type Middleware interface { // Wrap decorates the next handler by doing some extra work. Wrap(next Handler) Handler }
Middleware is the interface of middleware for handlers. It's similar to middleware in HTTP stacks, where the middleware gets access to the entry being logged and the next handler in the stack.
Example ¶
// build our custom handler (needed for example tests, because it expects us to use fmt.Println) handler := HandlerFunc(func(entry Entry) error { fmt.Printf("%s: %s\n", strings.ToUpper(entry.Level().String()), entry.Message()) return nil }) logger := NewLogger(handler, WithLevel(InfoLevel)) // printer is a middleware that prints the strings be printer := MiddlewareFunc(func(next Handler) Handler { return HandlerFunc(func(entry Entry) error { fmt.Println("== Before ==") err := next.HandleLog(entry) fmt.Println("== After ==") return err }) }) messages := 0 // counter is a middleware that counts the messages counter := MiddlewareFunc(func(next Handler) Handler { return HandlerFunc(func(entry Entry) error { messages++ return next.HandleLog(entry) }) }) logger.Use(printer) logger.Use(counter) logger.Info("Hey!") fmt.Println("Number of messages:", messages)
Output: == Before == INFO: Hey! == After == Number of messages: 1
type MiddlewareFunc ¶
MiddlewareFunc is a function that implements Middleware.
func (MiddlewareFunc) Wrap ¶
func (fn MiddlewareFunc) Wrap(next Handler) Handler
Wrap implements Middleware.
type Stack ¶
type Stack interface { // Log is an Interface. Interface // Use installs the specified middleware in the middleware stack. Use(Middleware) }
Stack is the interface of loggers that have a handler stack with middleware.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
handler
|
|
memory
Package memory implements a pkg/log.Handler that saves all entries in process memory
|
Package memory implements a pkg/log.Handler that saves all entries in process memory |
middleware
|
|
observability
Package observability implements a pkg/log.Handler that exports metrics for the logged messages.
|
Package observability implements a pkg/log.Handler that exports metrics for the logged messages. |
sentry
Package sentry implements a pkg/log.Handler that sends errors to Sentry
|
Package sentry implements a pkg/log.Handler that sends errors to Sentry |