logger

package
v0.0.0-...-8aad50e Latest Latest
Warning

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

Go to latest
Published: May 7, 2021 License: MIT Imports: 7 Imported by: 0

README

Logger

status: completed

A basic logger package which supports multiple levels of logging. Users can enable or disable log levels using the "log" environment variable. By default, logger logs info and error level logs. Custom log levels can be added anytime as required. More information can be found on godoc.

Installing

go get -u github.com/kadirahq/go-tools/logger

Examples

// create a new logger
log := logger.New("webapp")
log.Info("starting application on port:", 8080)

// Stack trace will be printed when the go-errors/errors
// package is used to create (or wrap) the error.
err := errors.New("error message")
log.Error(err, "foo =>", "bar")

// debug level is not enabled by default
// use logger.Enable("debug") or the env variable
// ex. log="info,error,debug" go run main.go
log.Debug("debug options", []int{1, 2, 3, 4, 5})

// any number of custom log levels can be used
// use logger.Enable("lvl9001") or the env variable
// ex. log="info,error,lvl9001" go run main.go
log.Print("lvl9001", "!!!")

Example output

Documentation

Overview

Package logger supports multiple levels of logging. Users can enable or disable log levels using the "log" environment variable. By default, logger logs **info** and **error** levle logs. Custom log levels can be added anytime as required.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(logs ...interface{})

Debug prints debug level logs using the default logger

func Disable

func Disable(lvl string)

Disable disables a level

func Enable

func Enable(lvl string)

Enable enables a level

func Error

func Error(err error, logs ...interface{})

Error prints error logs using the default logger

func Info

func Info(logs ...interface{})

Info prints info level logs using the default logger

func Print

func Print(lvl string, logs ...interface{})

Print prints any level logs using the default logger

func Time

func Time(beg time.Time, min time.Duration, logs ...interface{})

Time tracks the time duration using the default logger

Types

type Logger

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

Logger is a logger with a header

func New

func New(head string) *Logger

New creates a new logger

func (*Logger) Debug

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

Debug logs lots of details useful for debugging

func (*Logger) Error

func (l *Logger) Error(err error, logs ...interface{})

Error prints an error and some additional information

func (*Logger) Info

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

Info prints basic information to stdout

func (*Logger) New

func (l *Logger) New(head string) *Logger

New creates a logger extending the header

func (*Logger) Print

func (l *Logger) Print(lvl string, logs ...interface{})

Print prints log items if given level is enabled

func (*Logger) Time

func (l *Logger) Time(beg time.Time, min time.Duration, logs ...interface{})

Time tracks the time duration from start time and logs if its > minimum. The Time method can be used to track elapsed time when used with a defer.

Jump to

Keyboard shortcuts

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