slog

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2021 License: MIT Imports: 10 Imported by: 2

README

slog Go Reference

Golang simple logger wrapper. It supports leveled logger with the following levels:

  1. PANIC, log message and call panic
  2. FATAL, log message and exit by calling os.Exit(1)
  3. ERROR, log message in error level
  4. WARNING, log message in warning level
  5. INFO, log message in info level
  6. DEBUG, log message in debug level
  7. TRACE, log message in trace level

Logger implements the following interface:

type Logger interface {
    HasLevel(lv Level) bool
    SetLevel(lv Level)

    // Print like methods
    Trace(args ...interface{})
    Debug(args ...interface{})
    Print(args ...interface{})
    Info(args ...interface{})
    Warn(args ...interface{})
    Error(args ...interface{})
    Fatal(args ...interface{})
    Panic(args ...interface{})

    // Println like methods
    Traceln(args ...interface{})
    Debugln(args ...interface{})
    Println(args ...interface{})
    Infoln(args ...interface{})
    Warnln(args ...interface{})
    Errorln(args ...interface{})
    Fatalln(args ...interface{})
    Panicln(args ...interface{})

    // Printf like methods
    Tracef(format string, args ...interface{})
    Debugf(format string, args ...interface{})
    Printf(format string, args ...interface{})
    Infof(format string, args ...interface{})
    Warnf(format string, args ...interface{})
    Errorf(format string, args ...interface{})
    Fatalf(format string, args ...interface{})
    Panicf(format string, args ...interface{})

    // Log with fields (key=value)
    Tracew(msg string, keyVals ...interface{})
    Debugw(msg string, keyVals ...interface{})
    Printw(msg string, keyVals ...interface{})
    Infow(msg string, keyVals ...interface{})
    Warnw(msg string, keyVals ...interface{})
    Errorw(msg string, keyVals ...interface{})
    Fatalw(msg string, keyVals ...interface{})
    Panicw(msg string, keyVals ...interface{})
}

Logger can be initialized with the following constructor

New(name string, w io.Writer, l Level) (Logger, error)
NewWithOptions(w io.Writer, level Level, op Options) (Logger, error)

in which

  • name specify logger name. Currently available values discard, stdlog and logrus
  • w logger output
  • l logger level
  • op loger options

Options

  1. discard, discard log ouput except panic. Options not supported.

  2. stdlog, standar logger options:

    • timestampFormat: timestamp layout format, see time.Time format
    • disableColor: to disable color in log
  3. logrus, support options for logrus.TextFormatter formatter and logrus.JSONFormatter formatter.

    • formatter: logrus formatter, either text or json. Default format is logrus.TextFormatter
    • timestampFormat: timestamp layout format, see time.Time format
    • reportCaller: if set to true, the calling method will be added as a field
    • fullTimestamp: logging the full timestamp instead of elapsed time since application started, default to true
    • disableTimestamp: disable timestamp in log
    • fieldMap: customize default key names
    • dataKey: data key for json formatter
    • prettyPrint: pretty print json output
    • disableHTMLEscape: disable HTML escape in json formatter
    • forceColors: for color in text formatter
    • disableColors: disable colors in text formatter
    • forceQuote: force quote in text formatter
    • disableQuote: disable quote in text formatter
    • environmentOverrideColors: override color based on environment config in text formatter
    • disableSorting: disable key sorting in text formatter
    • disableLevelTruncation: disable log level string truncation in text formatter
    • padLevelText: add padding in level string in text formatter
    • quoteEmptyFields: add quote for empty log entry in text formatter

Credits

License

The MIT License (MIT), see LICENSE.

Documentation

Index

Constants

AllLevel contains all log level

View Source
const DiscardLoggerName = "discard"

Name of discard logger

View Source
const StdLoggerName = "stdlog"

Name of the standard logger

Variables

This section is empty.

Functions

func AsString

func AsString(val interface{}) (string, bool)

AsString convers val into string.

func AsStringQ

func AsStringQ(val interface{}) string

AsStringQ convert value into string. Value will be double-qouted if value is string/time/stringer

func Debug

func Debug(args ...interface{})

func Debugf

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

func Debugln

func Debugln(args ...interface{})

func Debugw

func Debugw(msg string, keyVals ...interface{})

func Error

func Error(args ...interface{})

func Errorf

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

func Errorln

func Errorln(args ...interface{})

func Errorw

func Errorw(msg string, keyVals ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(format string, args ...interface{})

func Fatalln

func Fatalln(args ...interface{})

func Fatalw

func Fatalw(msg string, keyVals ...interface{})

func FieldsToMap

func FieldsToMap(keyVals []interface{}) map[string]interface{}

FieldsToMap convert key-value array to map[string]interface{}

func HasLevel

func HasLevel(lv Level) bool

may be unsafe?

func Info

func Info(args ...interface{})

func Infof

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

func Infoln

func Infoln(args ...interface{})

func Infow

func Infow(msg string, keyVals ...interface{})

func LevelFixedString

func LevelFixedString(lv Level) string

LevelFixedString return fixed string for given level

func LevelsCount

func LevelsCount() int

LevelCount return number of valid log levels

func MustUse

func MustUse(name string, w io.Writer, l Level)

MustUse specific logger

func Panic

func Panic(args ...interface{})

func Panicf

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

func Panicln

func Panicln(args ...interface{})

func Panicw

func Panicw(msg string, keyVals ...interface{})

func Print

func Print(args ...interface{})

func Printf

func Printf(format string, args ...interface{})

func Println

func Println(args ...interface{})

func Printw

func Printw(msg string, keyVals ...interface{})

func Register

func Register(name string, constructor Constructor)

Register logger constructor

func SeparateFields

func SeparateFields(keyVals []interface{}) ([]string, []interface{})

SeparateFields into array of keys and array of values

func SetLevel

func SetLevel(lv Level)

func SimpleFormatter

func SimpleFormatter(msg string, keyVals []interface{}, sep string) string

Simpleformatter return simple key-value formatter, separated with `sep`

func Trace

func Trace(args ...interface{})

func Tracef

func Tracef(format string, args ...interface{})

func Traceln

func Traceln(args ...interface{})

func Tracew

func Tracew(msg string, keyVals ...interface{})

func Use

func Use(name string, w io.Writer, l Level) error

Use specific logger

func Warn

func Warn(args ...interface{})

func Warnf

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

func Warnln

func Warnln(args ...interface{})

func Warnw

func Warnw(msg string, keyVals ...interface{})

Types

type Constructor

type Constructor interface {
	New(w io.Writer, level Level) (Logger, error)
	NewWithOptions(w io.Writer, level Level, op Options) (Logger, error)
}

Constructor for reader creator

func ConstructorFor

func ConstructorFor(name string) (Constructor, bool)

ConstructorFor return sql query for given name

type Level

type Level uint32

Level of the log

const (
	PanicLevel Level = 1 << iota
	FatalLevel
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
	TraceLevel
)

Logger level

func Levels

func Levels() []Level

Levels return all level

func ParseLevel

func ParseLevel(level string) (Level, error)

ParseLevel string

func (*Level) Clear

func (l *Level) Clear(lv Level)

Clear specific flag

func (Level) Has

func (l Level) Has(lv Level) bool

Has specific level

func (Level) MarshalText

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

MarshalText return level as byte string

func (*Level) Set

func (l *Level) Set(lv Level)

Set log level

func (Level) String

func (l Level) String() string

Stringer interface

func (*Level) Toggle

func (l *Level) Toggle(lv Level)

Toggle log level

func (*Level) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

type LevelLoggerBase

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

LevelLogger base

func NewLevelLoggerBase

func NewLevelLoggerBase(lv Level) *LevelLoggerBase

NewLevelLoggerBase return new instance of level logger base

func (*LevelLoggerBase) HasLevel

func (b *LevelLoggerBase) HasLevel(lv Level) bool

HasLevel return current logger level

func (*LevelLoggerBase) SetLevel

func (b *LevelLoggerBase) SetLevel(lv Level)

SetLevel set logger level using flags

type Logger

type Logger interface {
	HasLevel(lv Level) bool
	SetLevel(lv Level)

	Trace(args ...interface{})
	Debug(args ...interface{})
	Print(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	Traceln(args ...interface{})
	Debugln(args ...interface{})
	Println(args ...interface{})
	Infoln(args ...interface{})
	Warnln(args ...interface{})
	Errorln(args ...interface{})
	Fatalln(args ...interface{})
	Panicln(args ...interface{})

	Tracef(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Printf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	Tracew(msg string, keyVals ...interface{})
	Debugw(msg string, keyVals ...interface{})
	Printw(msg string, keyVals ...interface{})
	Infow(msg string, keyVals ...interface{})
	Warnw(msg string, keyVals ...interface{})
	Errorw(msg string, keyVals ...interface{})
	Fatalw(msg string, keyVals ...interface{})
	Panicw(msg string, keyVals ...interface{})
}

Loger interface

var (
	UnknownFieldName        = "@logfield"
	DefaultLogger    Logger = discardL
)

Unknown field name and default logger

var Discard Logger = &discardLogger{LevelLoggerBase: LevelLoggerBase{level: AllLevel}}

Global discard logger

func New

func New(name string, w io.Writer, l Level) (Logger, error)

New create new logger with given name

func NewDiscardLogger

func NewDiscardLogger(l Level) Logger

NewDiscardLogger creates discard logger

func NewStdLogger

func NewStdLogger(w io.Writer, l Level, op Options) (Logger, error)

NewStdLogger creates new logger with given parameters

func NewWithOptions

func NewWithOptions(name string, w io.Writer, l Level, op Options) (Logger, error)

NewWithOptions create new logger with given name, and options for spesific logger

type Options

type Options map[string]interface{}

Options stores additional log configuration

func (Options) Get

func (op Options) Get(key string, def interface{}) interface{}

Get configuration value or default if doesn't exist

func (Options) GetBool

func (op Options) GetBool(key string, def bool) bool

GetBool get boolean value from options with given key

func (Options) GetInt

func (op Options) GetInt(key string, def int) int

GetInt get integer value from options with given key

func (Options) GetMap added in v0.2.0

func (op Options) GetMap(key string) map[string]interface{}

GetMap return string mapper

func (Options) GetOptions added in v0.2.0

func (op Options) GetOptions(key string) Options

GetOptions get value as map[string]interface{}

func (Options) GetString

func (op Options) GetString(key string, def string) string

GetString get string from options with given key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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