log

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

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

Go to latest
Published: Apr 17, 2020 License: MIT Imports: 8 Imported by: 2

README

go-logging

Build Status Go Report Card GoDoc

Advanced logger for Golang that supports multiple output formats and simple logging functions.

Simple Example

log.Info("Starting")
defer log.Info("Exiting")
a := 2
b := 3
sum := 5
log.Notice("sum of a and b", "a", a, "b", b, "sum", sum)
if err := doSomethingWithError(); err != nil {
	log.Error("could not do something with error", "error", err)
} else {
	log.Notice("successfully done something without error")
}
2020-04-17T08:59:51Z [INF] Starting
2020-04-17T08:59:51Z [NOT] sum of a and b A: 2 B: 3 SUM: 5
2020-04-17T08:59:51Z [ERR] could not do something with error ERROR: no such file or directory
2020-04-17T08:59:51Z [INF] Exiting

Advanced Example

opts := []log.Option{
	log.OptionEnableDebug(true),
	log.OptionWithTimeFormat(time.ANSIC),
	log.OptionEnableLocalTime(true),
	log.OptionWithLevelKey("_LVL"),
	log.OptionWithTimeKey("_TIME"),
	log.OptionWithMessageKey("message"),
	log.OptionWithStaticKV("_HOSTNAME", "my-hostname"),
	log.OptionWithStaticKV("_IP", "123.123.123.123"),
	log.OptionWithWriter(os.Stdout),
}
logger := log.New(log.FormatterJSON, opts...)
log.SetLogger(logger)
log.Info("Starting")
defer log.Info("Exiting")
a := 2
b := 3
log.Debug("calculating sum of a and b", "a", a, "b", b)
sum := a + b
log.Notice("sum of a and b", "a", a, "b", b, "sum", sum)
if err := doSomethingWithError(); err != nil {
	log.Error("could not do something with error", "error", err)
} else {
	log.Notice("successfully done something without error")
}
{"_HOSTNAME":"my-hostname","_IP":"123.123.123.123","_LVL":"Info","_TIME":"Fri Apr 17 11:40:52 2020","_message":"Starting"}
{"_HOSTNAME":"my-hostname","_IP":"123.123.123.123","_LVL":"Debug","_TIME":"Fri Apr 17 11:40:52 2020","_message":"calculating sum of a and b","a":2,"b":3}
{"_HOSTNAME":"my-hostname","_IP":"123.123.123.123","_LVL":"Notice","_TIME":"Fri Apr 17 11:40:52 2020","_message":"sum of a and b","a":2,"b":3,"sum":5}
{"_HOSTNAME":"my-hostname","_IP":"123.123.123.123","_LVL":"Error","_TIME":"Fri Apr 17 11:40:52 2020","_message":"could not do something with error","error":"no such file or directory"}
{"_HOSTNAME":"my-hostname","_IP":"123.123.123.123","_LVL":"Info","_TIME":"Fri Apr 17 11:40:52 2020","_message":"Exiting"}   

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(message string, kv ...interface{}) (int, error)

Debug message with global logger

func Error

func Error(message string, kv ...interface{}) (int, error)

Error message with global logger

func Fatal

func Fatal(message string, kv ...interface{}) (int, error)

Fatal message with global logger

func FormatterJSON

func FormatterJSON(l *Logger, level string, message string, kv ...interface{}) string

FormatterJSON formats every log line as JSON format

func FormatterKV

func FormatterKV(l *Logger, level string, message string, kv ...interface{}) string

FormatterKV formats every log line with key: "value" format using strconv.Quote(...)

func FormatterSimple

func FormatterSimple(l *Logger, level string, message string, kv ...interface{}) string

FormatterSimple formats every log line in a simple format %timestamp% [%level%] %message% %key-values%

func Info

func Info(message string, kv ...interface{}) (int, error)

Info message with global logger

func Notice

func Notice(message string, kv ...interface{}) (int, error)

Notice message with global logger

func OptionDisableLevel

func OptionDisableLevel(levelOff bool) func(l *Logger)

OptionDisableLevel removes level from log lines

func OptionDisableMutex

func OptionDisableMutex(mutexOff bool) func(*Logger)

OptionDisableMutex disables mutex protection of writer

func OptionDisableTime

func OptionDisableTime(timeOff bool) func(l *Logger)

OptionDisableTime removes time from log lines

func OptionEnableDebug

func OptionEnableDebug(debugOn bool) func(*Logger)

OptionEnableDebug enables/disables debug mode

func OptionEnableLocalTime

func OptionEnableLocalTime(localTimeOn bool) func(*Logger)

OptionEnableLocalTime enables/disables local time instead of utc time

func OptionEnableTrace

func OptionEnableTrace(traceOn bool) func(*Logger)

OptionEnableTrace enables/disables trace mode

func OptionWithLevelKey

func OptionWithLevelKey(key string) func(l *Logger)

OptionWithLevelKey overwrites the key that is used for level

func OptionWithMessageKey

func OptionWithMessageKey(key string) func(l *Logger)

OptionWithMessageKey overwrites the key that is used for message

func OptionWithStaticKV

func OptionWithStaticKV(key string, value interface{}) func(*Logger)

OptionWithStaticKV adds a key value pair to each upcoming log line .. This option can be passed multiple times.

func OptionWithTimeFormat

func OptionWithTimeFormat(format string) func(*Logger)

OptionWithTimeFormat changes the time format

func OptionWithTimeKey

func OptionWithTimeKey(key string) func(l *Logger)

OptionWithTimeKey overwrites the key that is used for time

func OptionWithWriter

func OptionWithWriter(w io.Writer) func(l *Logger)

OptionWithWriter overwrites the writer of the logger

func SetLogger

func SetLogger(l *Logger)

SetLogger sets a global logger

func Trace

func Trace(message string, kv ...interface{}) (int, error)

Trace message with global logger

func Warning

func Warning(message string, kv ...interface{}) (int, error)

Warning message with global logger

Types

type Formatter

type Formatter func(*Logger, string, string, ...interface{}) string

Formatter defines the log format for a single log line

type Logger

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

Logger holds all logger configurations

func New

func New(formatter Formatter, options ...Option) *Logger

New initializes a new logger using options to configure the logger

func (*Logger) Debug

func (l *Logger) Debug(message string, kv ...interface{}) (int, error)

Debug message

func (*Logger) Error

func (l *Logger) Error(message string, kv ...interface{}) (int, error)

Error message

func (*Logger) Fatal

func (l *Logger) Fatal(message string, kv ...interface{}) (int, error)

Fatal message

func (*Logger) Info

func (l *Logger) Info(message string, kv ...interface{}) (int, error)

Info message

func (*Logger) Notice

func (l *Logger) Notice(message string, kv ...interface{}) (int, error)

Notice message

func (*Logger) Print

func (l *Logger) Print(message string, kv ...interface{}) (int, error)

func (*Logger) Trace

func (l *Logger) Trace(message string, kv ...interface{}) (int, error)

Trace message

func (*Logger) Warning

func (l *Logger) Warning(message string, kv ...interface{}) (int, error)

Warning message

type Option

type Option func(*Logger)

Option is used to configure the logger on initialization

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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