Documentation ¶
Overview ¶
Package log contains all the structs to log TTN.
Index ¶
- Variables
- func Debug(msg string)
- func Debugf(msg string, v ...interface{})
- func Error(msg string)
- func Errorf(msg string, v ...interface{})
- func Fatal(msg string)
- func Fatalf(msg string, v ...interface{})
- func Info(msg string)
- func Infof(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(msg string)
- func Warnf(msg string, v ...interface{})
- type CLIHandler
- type CLIHandlerOption
- type Entry
- type F
- type Fielder
- type Handler
- type HandlerFunc
- type Interface
- type Level
- type Logger
- func (l *Logger) Debug(msg string)
- func (l *Logger) Debugf(msg string, v ...interface{})
- func (l *Logger) Error(msg string)
- func (l *Logger) Errorf(msg string, v ...interface{})
- func (l *Logger) Fatal(msg string)
- func (l *Logger) Fatalf(msg string, v ...interface{})
- func (l *Logger) Info(msg string)
- func (l *Logger) Infof(msg string, v ...interface{})
- func (l *Logger) Use(middleware Middleware)
- func (l *Logger) Warn(msg string)
- 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 ColorFromTerm = func(handler *CLIHandler) { COLORTERM := os.Getenv("COLORTERM") TERM := os.Getenv("TERM") color := COLORTERM != "" for _, substring := range colorTerms { color = color || strings.Contains(TERM, substring) } color = color && COLORTERM != "0" if out, ok := handler.Writer.(*os.File); ok { color = color && (isatty.IsTerminal(out.Fd()) || isatty.IsCygwinTerminal(out.Fd())) } handler.UseColor = color }
ColorFromTerm determines from the TERM and COLORTERM environment variables wether or not to use colors. If set, colors will be enabled in these cases: - COLORTERM is set and has a value different from 0 - TERM contains the substring "xterm" or "color" and COLORTERM is not 0
var Colors = [...]int{
DebugLevel: gray,
InfoLevel: blue,
WarnLevel: yellow,
ErrorLevel: red,
FatalLevel: red,
}
Colors mapping.
var Default, _ = NewLogger()
Default is the default logger used for the package global logging functions
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 CLIHandler ¶
CLIHandler implements Handler.
func NewCLI ¶
func NewCLI(w io.Writer, opts ...CLIHandlerOption) *CLIHandler
NewCLI returns a new CLIHandler.
func (*CLIHandler) HandleLog ¶
func (h *CLIHandler) HandleLog(e Entry) error
HandleLog implements Handler.
type CLIHandlerOption ¶
type CLIHandlerOption func(*CLIHandler)
CLIHandlerOption is the type of options for the CLIHandler.
func UseColor ¶
func UseColor(arg bool) CLIHandlerOption
UseColor is a functional option with which you can force the usage of colors on or off.
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(msg string) Info(msg string) Warn(msg string) Error(msg string) Fatal(msg string) 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(WithHandler(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 |
multi
Package multi implements a pkg/log.Handler that applies every log message on multiple Handlers
|
Package multi implements a pkg/log.Handler that applies every log message on multiple Handlers |
middleware
|
|
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 |
Package test exposes simple implementations of log interfaces that can be used for testing.
|
Package test exposes simple implementations of log interfaces that can be used for testing. |