ulog

package module
v1.0.33 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: MIT Imports: 16 Imported by: 6

README

Build Status GoDoc codecov

ulog

A simple logging library which comes with an extension to log to influxDB (https://github.com/dunv/ulog2influx)

It comes with a default template which prints log-lines for example like this

2019-12-30 20:42:20.372 | ERROR | github.com/dunv/uauth/handlers.glob. CheckLoginHandler.go:59 (func1) | renderError [path: /api/checkLogin] token is expired by 141h11m24s
2019-12-30 20:42:20.372 | INFO  | uhttp.Handle | Registered http GET /api/monthReport
2019-12-30 20:42:20.372 | INFO  | main main.go:105 (main) | Serving at 0.0.0.0:8080
Setup
ulog.SetLogLevelFromString(cfg.LogLevel)
  • Additionally one can set up functions to skip (see doc)
  • Additionally one can set up functions to replace (see doc)
Usage examples
ulog.Trace("helloWorld")
ulog.Tracef("helloWorld %s", "today")
ulog.Debug("helloWorld")
ulog.Infof("helloWorld %s", "today")
ulog.Errorf("helloWorld %s", "today")
ulog.Fatalf("helloWorld %s", "today")
ulog.Panicf("helloWorld %s", "today")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddReplaceFunction

func AddReplaceFunction(fnToBeReplaced string, replaceWithFn string)

Add a single replace function. This is used to make log-output origin easier to identify. Instead of getting a filename, package and function, a desired identifier can be chosen. for example

ulog.AddReplaceFunction("github.com/dunv/uhttp/middlewares.AddLogging.func1", "uhttp.Logging")
ulog.AddReplaceFunction("github.com/dunv/uhttp.Handle", "uhttp.Handle")

func AddReplaceFunctions

func AddReplaceFunctions(m map[string]string)

Add a multiple replace functions. See AddReplaceFunction for more detail

func AddSkipFunctions

func AddSkipFunctions(_skipFunctions ...string)

// Make expected output (which is only for info, not for debugging) more readable Add skipFunctions. These functions will be skipped, when trying to determine the origin of a log-line. This way helper functions which are in use for multiple different origins can be omitted. For example

ulog.AddSkipFunctions(
	"github.com/dunv/uhttp.RenderError",
	"github.com/dunv/uhttp/helpers.RenderError",
	"github.com/dunv/uhttp.RenderErrorWithStatusCode",
	"github.com/dunv/uhttp/helpers.RenderErrorWithStatusCode",
	"github.com/dunv/uhttp.RenderMessage",
	"github.com/dunv/uhttp/helpers.RenderMessage",
	"github.com/dunv/uhttp.RenderMessageWithStatusCode",
	"github.com/dunv/uhttp/helpers.RenderMessageWithStatusCode",
	"github.com/dunv/uhttp.renderMessageWithStatusCode",
	"github.com/dunv/uhttp/helpers.renderMessageWithStatusCode",
	"github.com/dunv/uhttp/helpers.renderErrorWithStatusCode",
	"github.com/dunv/uhttp.renderErrorWithStatusCode",
)

func Debug deprecated

func Debug(v ...interface{})

Deprecated: use v2

func DebugIfError added in v1.0.23

func DebugIfError(err error)

Logs the error to DEBUG-level if is not nil

func Debugf deprecated

func Debugf(fmtString string, v ...interface{})

Deprecated: use v2

func EnableColors added in v1.0.30

func EnableColors()

Use a format which colors lines according to logLevel

func Error deprecated

func Error(v ...interface{})

Deprecated: use v2

func Errorf deprecated

func Errorf(fmtString string, v ...interface{})

Deprecated: use v2

func Fatal deprecated

func Fatal(v ...interface{})

Deprecated: use v2

func FatalIfError added in v1.0.23

func FatalIfError(err error)

Logs the error to FATAL-level if is not nil

func FatalIfErrorSecondArg added in v1.0.23

func FatalIfErrorSecondArg(input interface{}, err error) interface{}

Logs if error received as second argument to FATAL-level (and panicking after) is not nil (first argument is discarded)

func Fatalf deprecated

func Fatalf(fmtString string, v ...interface{})

Deprecated: use v2

func Info deprecated

func Info(v ...interface{})

Deprecated: use v2

func InfoIfError added in v1.0.23

func InfoIfError(err error)

Logs the error to INFO-level if is not nil

func Infof deprecated

func Infof(fmtString string, v ...interface{})

Deprecated: use v2

func LogByteArrayLineByLine added in v1.0.22

func LogByteArrayLineByLine(in []byte, f func(string, ...interface{}), prefix ...string)

Logs a byteArray line by line

func LogByteArrayLineByLineToDebug added in v1.0.22

func LogByteArrayLineByLineToDebug(in []byte, prefix ...string)

Logs a byteArray line by line to Info

func LogByteArrayLineByLineToError added in v1.0.22

func LogByteArrayLineByLineToError(in []byte, prefix ...string)

Logs a byteArray line by line to Error

func LogByteArrayLineByLineToInfo added in v1.0.22

func LogByteArrayLineByLineToInfo(in []byte, prefix ...string)

Logs a byteArray line by line to Info

func LogByteArrayLineByLineToTrace added in v1.0.22

func LogByteArrayLineByLineToTrace(in []byte, prefix ...string)

Logs a byteArray line by line to Trace

func LogByteArrayLineByLineToWarn added in v1.0.22

func LogByteArrayLineByLineToWarn(in []byte, prefix ...string)

Logs a byteArray line by line to Warn

func LogEnvStruct

func LogEnvStruct(envStruct interface{}, prefix string)

Helper function to log out a struct annotated with "env", "mask" and "warnIf" annotations Good to use with the "github.com/codingconcepts/env" package

func LogIfError

func LogIfError(err error)

Logs the error to ERROR-level if it is not nil

func LogIfErrorSecondArg

func LogIfErrorSecondArg(input interface{}, err error) interface{}

Logs if error received as second argument to ERROR-level is not nil (first argument is discarded)

func LogIfErrorToInfo

func LogIfErrorToInfo(err error)

Logs the error to INFO-level if it is not nil

func LogIfErrorToInfoSecondArg

func LogIfErrorToInfoSecondArg(_ interface{}, err error)

Logs if error received as second argument to INFO-level is not nil (first argument is discarded)

func LogJSONStruct added in v1.0.24

func LogJSONStruct(envStruct interface{}, prefix string)

Helper function to log out a struct annotated with "json", "mask" and "warnIf" annotations Good to use with the "encoding/json" package

func LogWithLevel deprecated added in v1.0.14

func LogWithLevel(level LogLevel, v ...interface{})

Deprecated: use v2

func LogWithLevelf deprecated added in v1.0.14

func LogWithLevelf(level LogLevel, fmtString string, v ...interface{})

Deprecated: use v2

func NewErrorLogger added in v1.0.5

func NewErrorLogger(ulogger *ULogger) *log.Logger

Helper for instantiating UErrorWriter

func NewInfoLogger added in v1.0.5

func NewInfoLogger(ulogger *ULogger) *log.Logger

Helper for instantiating UInfoWriter

func Panic deprecated

func Panic(v interface{})

Deprecated: use v2

func PanicIfError added in v1.0.3

func PanicIfError(err error)

Logs the error to PANIC-level (and panicking after) if is not nil

func PanicIfErrorSecondArg added in v1.0.3

func PanicIfErrorSecondArg(input interface{}, err error) interface{}

Logs if error received as second argument to PANIC-level (and panicking after) is not nil (first argument is discarded)

func Panicf deprecated

func Panicf(fmtString string, v ...interface{})

Deprecated: use v2

func RemoveWriter added in v1.0.6

func RemoveWriter(name string) error

Remove a writer

func ReplaceFunctions

func ReplaceFunctions() map[string]string

Get replace-functions

func ResetFormat added in v1.0.8

func ResetFormat()

Use original logging format

func ResetReplaceFunctions added in v1.0.14

func ResetReplaceFunctions()

Reset replace-functions

func ResetSkipFunctions added in v1.0.26

func ResetSkipFunctions()

func SetDebug

func SetDebug()

Enable debugging skipFunctions and ReplaceFunctions

func SetFormatString

func SetFormatString(_fmtString string)

Use custom logging format (using text/template) The original is

"{{ .Time }} | {{ .Level }} | {{ .Package }}{{ if .File }} {{ .File }}{{ end }}{{ if .Line }}:{{ .Line }}{{ end }}{{ if .Function }} ({{ .Function }}){{ end }} | {{ .Message }}\n"

Available vars are

  • Time
  • Level
  • Package
  • File (not always available)
  • Line (not always available)
  • Function (not always available)
  • Message

func SetLogLevel

func SetLogLevel(_level LogLevel)

Set logLevel

func SetLogLevelFromString

func SetLogLevelFromString(levelRaw string)

Set logLevel from string takes "trace", "debug", "info", "information", "warn", "warning", "error" and "fatal" as arguments. Case does not matter

func SetTimestampFormat

func SetTimestampFormat(_tsFormat string)

Set a different format for the timestamp the original is "2006-01-02 15:04:05.000"

func SetWriter

func SetWriter(_writer io.Writer, name *string)

Add or replace a writer if name is nil the default writer will be replaced

func SkipFunctions

func SkipFunctions() []string

Get currently configured skipFunctions

func Trace deprecated

func Trace(v ...interface{})

Deprecated: use v2

func TraceIfError added in v1.0.23

func TraceIfError(err error)

Logs the error to TRACE-level if is not nil

func Tracef deprecated

func Tracef(fmtString string, v ...interface{})

Deprecated: use v2

func UnsetDebug

func UnsetDebug()

Disable debugging skipFunctions and ReplaceFunctions

func Warn deprecated

func Warn(v ...interface{})

Deprecated: use v2

func WarnIfError added in v1.0.23

func WarnIfError(err error)

Logs the error to WARN-level if is not nil

func Warnf deprecated

func Warnf(fmtString string, v ...interface{})

Deprecated: use v2

Types

type LogEntry

type LogEntry struct {
	Time     string
	Level    logLevelString
	Package  string
	File     string
	Line     int
	Function string
	Message  string
}

type LogLevel

type LogLevel int

LogLevel for the application

const (
	LEVEL_TRACE LogLevel = iota
	LEVEL_DEBUG
	LEVEL_INFO
	LEVEL_WARNING
	LEVEL_ERROR
	LEVEL_FATAL
)

func GetLogLevel

func GetLogLevel() LogLevel

Get currently set logLevel

func (LogLevel) String added in v1.0.18

func (l LogLevel) String() string

type UErrorWriter added in v1.0.5

type UErrorWriter struct {
	Logger *ULogger
}

A writer (implementing the io.Writer interface) which logs everything to its configured logger and to ERROR-level If not logger is configured, the default logger will be used

func (UErrorWriter) Write added in v1.0.5

func (l UErrorWriter) Write(p []byte) (n int, err error)

type UInfoWriter added in v1.0.5

type UInfoWriter struct {
	Logger *ULogger
}

A writer (implementing the io.Writer interface) which logs everything to its configured logger and to INFO-level If not logger is configured, the default logger will be used

func (UInfoWriter) Write added in v1.0.5

func (l UInfoWriter) Write(p []byte) (n int, err error)

type ULog

type ULog struct{}

A struct satisfying the ULogger interface. Currently only one global config for ulog is supported. This ULog cannot have a different configuration yet

func NewUlog

func NewUlog() *ULog

func (ULog) Debug

func (l ULog) Debug(v ...interface{})

func (ULog) Debugf

func (l ULog) Debugf(fmtString string, v ...interface{})

func (ULog) Error

func (l ULog) Error(v ...interface{})

func (ULog) Errorf

func (l ULog) Errorf(fmtString string, v ...interface{})

func (ULog) Fatal

func (l ULog) Fatal(v ...interface{})

func (ULog) Fatalf

func (l ULog) Fatalf(fmtString string, v ...interface{})

func (ULog) Info

func (l ULog) Info(v ...interface{})

func (ULog) Infof

func (l ULog) Infof(fmtString string, v ...interface{})

func (ULog) LogIfError

func (l ULog) LogIfError(err error)

func (ULog) LogIfErrorSecondArg

func (l ULog) LogIfErrorSecondArg(tmp interface{}, err error)

func (ULog) LogIfErrorToInfo

func (l ULog) LogIfErrorToInfo(err error)

func (ULog) LogIfErrorToInfoSecondArg

func (l ULog) LogIfErrorToInfoSecondArg(tmp interface{}, err error)

func (ULog) LogWithLevel added in v1.0.14

func (l ULog) LogWithLevel(customLevel LogLevel, v ...interface{})

func (ULog) LogWithLevelf added in v1.0.14

func (l ULog) LogWithLevelf(customLevel LogLevel, customFmtString string, v ...interface{})

func (ULog) Panic

func (l ULog) Panic(v interface{})

func (ULog) PanicIfError added in v1.0.3

func (l ULog) PanicIfError(err error)

func (ULog) PanicIfErrorSecondArg added in v1.0.3

func (l ULog) PanicIfErrorSecondArg(tmp interface{}, err error)

func (ULog) Trace

func (l ULog) Trace(v ...interface{})

func (ULog) Tracef

func (l ULog) Tracef(fmtString string, v ...interface{})

func (ULog) Warn

func (l ULog) Warn(v ...interface{})

func (ULog) Warnf

func (l ULog) Warnf(fmtString string, v ...interface{})

type ULogger

type ULogger interface {
	Trace(v ...interface{})
	Tracef(fmtString string, v ...interface{})
	Debug(v ...interface{})
	Debugf(fmtString string, v ...interface{})
	Info(v ...interface{})
	Infof(fmtString string, v ...interface{})
	Warn(v ...interface{})
	Warnf(fmtString string, v ...interface{})
	Error(v ...interface{})
	Errorf(fmtString string, v ...interface{})
	Panic(v interface{})
	Fatal(v ...interface{})
	Fatalf(fmtString string, v ...interface{})
	LogWithLevel(level LogLevel, v ...interface{})
	LogWithLevelf(level LogLevel, fmtString string, v ...interface{})
	LogIfError(err error)
	LogIfErrorSecondArg(tmp interface{}, err error)
	PanicIfError(err error)
	PanicIfErrorSecondArg(tmp interface{}, err error)
	LogIfErrorToInfo(err error)
	LogIfErrorToInfoSecondArg(tmp interface{}, err error)
}

Interface containing all log-helper methods

Jump to

Keyboard shortcuts

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