logger

package module
v0.0.0-...-aa068c9 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2022 License: MIT Imports: 7 Imported by: 5

README

Logger

A structured logger.

Design

The logger is designed with the idea of following closely the severity levels as defined in RFC5424.

A partial log compatibility layer is available by importing github.com/tzvetkoff-go/logger/compat/log.

Usage

See example.go for an example.

License

The code is subject to the MIT license.

Documentation

Overview

Package logger implements a structured logger.

Index

Constants

View Source
const (
	// revive:disable:var-naming
	LOG_EMERG   = Level(0)
	LOG_ALERT   = Level(1)
	LOG_CRIT    = Level(2)
	LOG_ERROR   = Level(3)
	LOG_WARNING = Level(4)
	LOG_NOTICE  = Level(5)
	LOG_INFO    = Level(6)
	LOG_DEBUG   = Level(7)
)

Levels as defined by RFC5424.

Variables

View Source
var DefaultLogger = &Logger{
	Level:  LOG_DEBUG,
	Fields: Fields{},
	Backends: []Backend{
		DefaultBackend,
	},
}

DefaultLogger is the default logger instance.

View Source
var LevelToString = map[Level]string{
	LOG_EMERG:   "EMERG",
	LOG_ALERT:   "ALERT",
	LOG_CRIT:    "CRIT",
	LOG_ERROR:   "ERROR",
	LOG_WARNING: "WARNING",
	LOG_NOTICE:  "NOTICE",
	LOG_INFO:    "INFO",
	LOG_DEBUG:   "DEBUG",
}

LevelToString maps levels to string names.

View Source
var StringToLevel = map[string]Level{
	"LOG_EMERG":   LOG_EMERG,
	"EMERG":       LOG_EMERG,
	"emerg":       LOG_EMERG,
	"0":           LOG_EMERG,
	"LOG_ALERT":   LOG_ALERT,
	"ALERT":       LOG_ALERT,
	"alert":       LOG_ALERT,
	"1":           LOG_ALERT,
	"LOG_CRIT":    LOG_CRIT,
	"CRIT":        LOG_CRIT,
	"crit":        LOG_CRIT,
	"2":           LOG_CRIT,
	"LOG_ERROR":   LOG_ERROR,
	"ERROR":       LOG_ERROR,
	"error":       LOG_ERROR,
	"3":           LOG_ERROR,
	"LOG_WARNING": LOG_WARNING,
	"WARNING":     LOG_WARNING,
	"warning":     LOG_WARNING,
	"4":           LOG_WARNING,
	"LOG_NOTICE":  LOG_NOTICE,
	"NOTICE":      LOG_NOTICE,
	"notice":      LOG_NOTICE,
	"5":           LOG_NOTICE,
	"LOG_INFO":    LOG_INFO,
	"INFO":        LOG_INFO,
	"info":        LOG_INFO,
	"6":           LOG_INFO,
	"LOG_DEBUG":   LOG_DEBUG,
	"DEBUG":       LOG_DEBUG,
	"debug":       LOG_DEBUG,
	"7":           LOG_DEBUG,
}

StringToLevel maps common string names to log levels.

Functions

func AddBackends

func AddBackends(backends ...Backend)

AddBackends ...

func Alert

func Alert(format string, v ...interface{})

Alert ...

func ClearBackends

func ClearBackends()

ClearBackends ...

func Crit

func Crit(format string, v ...interface{})

Crit ...

func Debug

func Debug(format string, v ...interface{})

Debug ...

func Emerg

func Emerg(format string, v ...interface{})

Emerg ...

func Error

func Error(format string, v ...interface{})

Error ...

func Info

func Info(format string, v ...interface{})

Info ...

func Log

func Log(level Level, format string, v ...interface{})

Log ...

func LookupLevel

func LookupLevel(c Level) string

LookupLevel returns a string from a log level.

func Notice

func Notice(format string, v ...interface{})

Notice ...

func SetColor

func SetColor(color bool)

SetColor ...

func SetFields

func SetFields(fields Fields)

SetFields ...

func SetFormatter

func SetFormatter(formatter Formatter)

SetFormatter sets the formatter of the DefaultBackend.

func SetLevel

func SetLevel(level Level)

SetLevel ...

func SetTimestampFormat

func SetTimestampFormat(format string)

SetTimestampFormat ...

func SetTimestamps

func SetTimestamps(timestamps bool)

SetTimestamps ...

func SetWriter

func SetWriter(writer io.Writer)

SetWriter sets the output writer of the DefaultBackend.

func Warning

func Warning(format string, v ...interface{})

Warning ...

Types

type Backend

type Backend interface {
	Write(Fields) bool
}

Backend describes a logger backend.

var DefaultBackend Backend = &WriterBackend{
	Writer:    os.Stderr,
	Formatter: DefaultFormatter,
}

DefaultBackend is the default logger backend.

func GetBackends

func GetBackends() []Backend

GetBackends ...

type Fields

type Fields map[string]interface{}

Fields is just an alias for map[string]interface{} with a few convenience methods added.

func GetFields

func GetFields() Fields

GetFields ...

func (Fields) Copy

func (f Fields) Copy() Fields

Copy returns a copy of the map.

func (Fields) Keys

func (f Fields) Keys() []string

Keys returns the map keys sorted alphabetically.

func (Fields) Update

func (f Fields) Update(updates Fields)

Update updates the map with keys & values from the other map.

type Formatter

type Formatter interface {
	Format(Fields) string
}

Formatter describes a log formatter.

var DefaultFormatter Formatter = &TextFormatter{
	Timestamps:      true,
	TimestampFormat: "2006-01-02T15:04:05.000000Z",
	Color:           stderrStat.Mode()&os.ModeCharDevice != 0,
}

DefaultFormatter is the default log formatter.

type FormatterFunc

type FormatterFunc func(Fields) string

FormatterFunc is a function wrapper of Formatter.

func (FormatterFunc) Format

func (f FormatterFunc) Format(fields Fields) string

Format returns log fields formatted as a string.

type JSONFormatter

type JSONFormatter struct {
}

JSONFormatter is a simple JSON log formatter.

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(fields Fields) string

Format returns log fields formatted as a string.

type Level

type Level int

Level represents a log level.

func GetLevel

func GetLevel() Level

GetLevel ...

func LookupString

func LookupString(s string) Level

LookupString returns a log level from a string.

type Logger

type Logger struct {
	Level    Level
	Fields   Fields
	Backends []Backend
}

Logger is the logger itself.

func (*Logger) Alert

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

Alert ...

func (*Logger) Crit

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

Crit ...

func (*Logger) Debug

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

Debug ...

func (*Logger) Emerg

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

Emerg ...

func (*Logger) Error

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

Err ...

func (*Logger) Info

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

Info ...

func (*Logger) Log

func (l *Logger) Log(level Level, format string, v ...interface{})

Log ...

func (*Logger) Notice

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

Notice ...

func (*Logger) Warning

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

Warning ...

type TextFormatter

type TextFormatter struct {
	Timestamps      bool
	TimestampFormat string
	Color           bool
}

TextFormatter is a simple text log formatter.

func (*TextFormatter) Format

func (f *TextFormatter) Format(fields Fields) string

Format returns log fields formatted as a string.

type WriterBackend

type WriterBackend struct {
	Writer    io.Writer
	Formatter Formatter
}

WriterBackend is a basic io.Writer logger backend.

func (*WriterBackend) Write

func (h *WriterBackend) Write(fields Fields) bool

Write writes the log message to the backend.

Directories

Path Synopsis
backends
syslog
Package syslog provides a SysLog Logger backend.
Package syslog provides a SysLog Logger backend.
compat
log
Package log contains a compatibility layer for the system "log" package.
Package log contains a compatibility layer for the system "log" package.
/usr/bin/env true; exec /usr/bin/env go run "$0" "$@"
/usr/bin/env true; exec /usr/bin/env go run "$0" "$@"

Jump to

Keyboard shortcuts

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