logr

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

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

Go to latest
Published: May 13, 2024 License: Apache-2.0 Imports: 6 Imported by: 15

README

Logr

Logr is a stupid simple logging package for Go. It embeds and extends the standard library logger which means it's very simple to use.

Example
package main

import (
    "github.com/CoverWhale/logr"
)

func main() {
    logger := logr.NewLogger()

    // set level directly or with env vars
    logger.Level = logr.DebugLevel

    logger.Info("Info log")
    logger.Debug("Debug log")

    logger.Infof("logging with %s", "formatting")

    logger.Error("uh oh spaghettios")

    // Calling without the instantiated logger (relies on env vars: LOG_LEVEL=debug)
    logr.Debugf("debug level %s", "stuff")

    // You can crate a new logger with a context message 
    // Get caller returns the name of the current function
    functionContext := map[string]string{"function", logr.GetCaller()}
    ctx := logger.WithContext(functionContext)
    ctx.Info("another message")
}
Output

Since this uses the default Go logger under the hood, you can easily set an output file instead of stdout.

package main

import (
    "log"
    "github.com/CoverWhale/logr"
)

func main() {
	f, err := os.OpenFile("test.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
	if err != nil {
		log.Fatal("error")
	}
	defer f.Close()


	logger := logr.NewLogger()
	// use SetOutput from std logger
	logger.Logger.SetOutput(f)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorLevel = Level{/* contains filtered or unexported fields */}
	InfoLevel  = Level{/* contains filtered or unexported fields */}
	DebugLevel = Level{/* contains filtered or unexported fields */}
)

Functions

func Debug

func Debug(s interface{})

func Debugf

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

func Error

func Error(s interface{})

func Errorf

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

func Fatal

func Fatal(s interface{})

func Fatalf

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

func GetCaller

func GetCaller() string

GetCaller is a helper function to get the function name to provide context for an error

func Info

func Info(s interface{})

func Infof

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

Types

type Level

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

type Logger

type Logger struct {
	Level Level
	*log.Logger
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger() *Logger

func (*Logger) Debug

func (l *Logger) Debug(s interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, s ...interface{})

func (*Logger) Error

func (l *Logger) Error(s interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, s ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(s interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, s ...interface{})

func (*Logger) Info

func (l *Logger) Info(s interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, s ...interface{})

func (*Logger) WithContext

func (l *Logger) WithContext(s map[string]string) *Logger

Jump to

Keyboard shortcuts

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