xlog

package
v0.9.0-beta.6 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2022 License: MIT Imports: 15 Imported by: 1

Documentation

Overview

Package xlog implements a sofisticated logging package.

### Quick Start

When importing xlog, logging is can be used right away:

package main

import "github.com/geertjanvdk/xkit/xlog"

func main() {
	xlog.Info("I am an informational.")
}

It is possible to add fields as well as error or a scope:

package main

import "github.com/geertjanvdk/xkit/xlog"

func main() {
	if err := createUser(user); err != nil {
		xlog.WithError(err).Errorf("creating user for %s", appName)
	} else {
		xlog.WithScope("users").WithField("user", user.ID).Infof("user created")
	}
}

### Setting Logging Levels

xlog does not support the classic way of setting the level of logging. Normally, when you set log level WARN, nothing informational is written, and only errors are reported.

In xlog levels are activated, so that the application can choose which are logged in a dynamic manner.

For example: an application is running and has the defaults set. This means that informational, warning, and error messages are logged, as well as fatal or terminating events. When, however, the developers need for a shor time to have debugging enabled (for example in staging environments), they can do so by activating the debug level.

xlog.ActivateLevel(xlog.DebugLevel)

How this is dynamically done is up to the application. It could be possible to have a process signal, an API call, or as simple as creating a file in a specific folder.

Deactivating is simply done using using the Deactivate method:

xlog.DeactivateLevel(xlog.DebugLevel)

### Custom Loggers

xlog comes with a logger. It is however more practical for applications to instantiate their own, and customize it.

package main

import "github.com/geertjanvdk/xkit/xlog"

func main() {
	logger := xlog.New()
	logger.DeactivateLevels(xlog.WarnLevel, xlog.InfoLevel)
	logger.Scope = "my-app"
	logger.Errorf("only errors are logged (and fatal events)")
}

Index

Constants

View Source
const (
	FieldError    = "err"
	FieldErrCode  = "errCode"
	FieldTime     = "time"
	FieldLevel    = "level"
	FieldMsg      = "msg"
	FieldScope    = "scope"
	FieldFileLine = "fileInfo"
	FieldStack    = "debugStack"
)

Variables

This section is empty.

Functions

func ActivateLevels

func ActivateLevels(levels ...Level)

ActivateLevels is used to activate particular levels of the default logger. For example, ActivateLevels(DebugLevel) can be used if the logger logs errors, but debug message are wanted, without info messages.

func DeactivateLevels

func DeactivateLevels(levels ...Level)

DeactivateLevels is used to deactivate particular levels of the default logger. For example, DeactivateLevels(DebugLevel) can be used to deactivate all debugging messages.

func Debug

func Debug(a ...interface{})

Debug logs a debug entry using the default logger formatting using provided operands.

func Debugf

func Debugf(format string, a ...interface{})

Debugf logs a debug entry using the default logger formatting according to a format specifier and operands.

func Error

func Error(a ...interface{})

Error logs an error entry using the default logger formatting using provided operands.

func Errorf

func Errorf(format string, a ...interface{})

Errorf logs an error entry using the default logger formatting according to a format specifier and operands.

func GetOut

func GetOut() io.Writer

GetOut returns where the default logger sends its output.

func Info

func Info(a ...interface{})

Info logs a informational entry using the default logger formatting using provided operands.

func Infof

func Infof(format string, a ...interface{})

Infof logs a informational entry using the default logger formatting according to a format specifier and operands.

func LevelsAsStrings

func LevelsAsStrings() []string

LevelsAsStrings returns the active levels their names. The result is sorted.

func Panic

func Panic(a ...interface{})

Panic simply panics and formats the message using provided operands.

func Panicf

func Panicf(format string, a ...interface{})

Panicf simply panics and formats the message according to a format specifier and operands.

func SetFormatter

func SetFormatter(f Formatter)

SetFormatter sets the formatter of the default logger.

func SetOut

func SetOut(w io.Writer)

SetOut sets where the output of the default logger goes to.

func Warn

func Warn(a ...interface{})

Warn logs a warning entry using the default logger formatting using provided operands.

func Warnf

func Warnf(format string, a ...interface{})

Warnf logs a warning entry using the default logger formatting according to a format specifier and operands.

Types

type Entry

type Entry struct {
	Fields Fields
	// contains filtered or unexported fields
}

func WithError

func WithError(err error) *Entry

WithError returns an Entry for the default logger which has a field set with value of err. The name of the field is defined as xlog.FieldError.

func WithField

func WithField(name string, value interface{}) *Entry

WithField returns an Entry for the default logger which has field set using name and value.

func WithFields

func WithFields(fields Fields) *Entry

WithFields returns an Entry for the default logger which has all fields set.

func WithScope

func WithScope(scope string) *Entry

WithScope returns an Entry for the default logger which has a field set with value of scope. The name of the field is defined as xlog.FieldScope.

func (*Entry) Debug

func (e *Entry) Debug(a ...interface{})

func (*Entry) Debugf

func (e *Entry) Debugf(format string, a ...interface{})

func (*Entry) Error

func (e *Entry) Error(a ...interface{})

func (*Entry) Errorf

func (e *Entry) Errorf(format string, a ...interface{})

func (*Entry) Fatal

func (e *Entry) Fatal(a ...interface{})

func (*Entry) Fatalf

func (e *Entry) Fatalf(format string, a ...interface{})

func (*Entry) Info

func (e *Entry) Info(a ...interface{})

func (*Entry) Infof

func (e *Entry) Infof(format string, a ...interface{})

func (*Entry) Panic

func (e *Entry) Panic(a ...interface{})

func (*Entry) Panicf

func (e *Entry) Panicf(format string, a ...interface{})

func (*Entry) String

func (e *Entry) String() string

String returns the textual representation serialized by the logger's formatter.

func (*Entry) UnmarshalJSON

func (e *Entry) UnmarshalJSON(data []byte) error

func (*Entry) Warn

func (e *Entry) Warn(a ...interface{})

func (*Entry) Warnf

func (e *Entry) Warnf(format string, a ...interface{})

func (*Entry) WithError

func (e *Entry) WithError(err error) *Entry

func (*Entry) WithField

func (e *Entry) WithField(name string, value interface{}) *Entry

func (*Entry) WithFields

func (e *Entry) WithFields(fields Fields) *Entry

func (*Entry) WithScope

func (e *Entry) WithScope(scope string) *Entry

type Fields

type Fields map[string]interface{}

Fields is used to add extra, custom fields to a log entry.

func (Fields) MarshalJSON

func (f Fields) MarshalJSON() ([]byte, error)

type Formatter

type Formatter interface {
	Format(e *Entry) ([]byte, error)
}

func GetFormatter

func GetFormatter() Formatter

GetFormatter returns the formatter of the default logger.

type JSONFormat

type JSONFormat struct {
	FormatType TextFormatType
	// contains filtered or unexported fields
}

func (*JSONFormat) Format

func (j *JSONFormat) Format(e *Entry) ([]byte, error)

type Level

type Level int
const (
	FatalLevel   Level = -1 // exits
	DefaultLevel Level = 0  // use whatever is xlog's default
	ErrorLevel   Level = 1
	WarnLevel    Level = 2
	InfoLevel    Level = 3
	DebugLevel   Level = 4
	PanicLevel   Level = 5
)

A classic way of describing the logging level. Note that 'Panic' is the Go-way for 'Trace'.

func Levels

func Levels() []Level

Levels returns the active levels.

type Logger

type Logger struct {
	Out       io.Writer
	Formatter Formatter
	Scope     string
	UseUTC    bool
	// contains filtered or unexported fields
}

func New

func New() *Logger

func (*Logger) ActivateLevels

func (l *Logger) ActivateLevels(levels ...Level)

ActivateLevels is used to activate particular levels of l. For example, ActivateLevels(DebugLevel) can be used if the logger logs errors, but debug message are wanted, without info messages.

func (*Logger) DeactivateLevels

func (l *Logger) DeactivateLevels(levels ...Level)

DeactivateLevels is used to deactivate particular levels of l. For example, DeactivateLevels(DebugLevel) can be used to deactivate all debugging messages.

func (*Logger) Debug

func (l *Logger) Debug(a ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, a ...interface{})

func (*Logger) Error

func (l *Logger) Error(a ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, a ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(a ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, a ...interface{})

func (*Logger) Info

func (l *Logger) Info(a ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, a ...interface{})

func (*Logger) Levels

func (l *Logger) Levels() []Level

Levels returns the active levels. The result is sorted.

func (*Logger) LevelsAsStrings

func (l *Logger) LevelsAsStrings() []string

LevelsAsStrings returns the active levels their names. The result is sorted.

func (*Logger) Log

func (l *Logger) Log(level Level, a ...interface{})

Log logs then entry according to a level using provided operands.

func (*Logger) Logf

func (l *Logger) Logf(level Level, format string, a ...interface{})

Logf logs according to a format specifier, and optional arguments, for given level.

func (*Logger) NewEntry

func (l *Logger) NewEntry() *Entry

func (*Logger) Panic

func (l *Logger) Panic(a ...interface{})

func (*Logger) Panicf

func (l *Logger) Panicf(format string, a ...interface{})

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{})

func (*Logger) SetFormatter

func (l *Logger) SetFormatter(f Formatter)

SetFormatter sets f as formatter for l.

func (*Logger) Warn

func (l *Logger) Warn(a ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, a ...interface{})

func (*Logger) WithError

func (l *Logger) WithError(err error) *Entry

WithError returns an entry with value of field 'error' set to err. This is the same as calling Logger.WithField(FieldError, err).

func (*Logger) WithField

func (l *Logger) WithField(name string, value interface{}) *Entry

func (*Logger) WithFields

func (l *Logger) WithFields(fields Fields) *Entry

type TextFormat

type TextFormat struct {
	FormatType TextFormatType
	TimeFormat string // defaults to  (tf *TextFormat)
}

func (*TextFormat) Format

func (tf *TextFormat) Format(e *Entry) ([]byte, error)

type TextFormatType

type TextFormatType int
const (
	TextFullFields TextFormatType = iota + 1
	TextCompat
)

Jump to

Keyboard shortcuts

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