log

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2020 License: MIT Imports: 8 Imported by: 4

README

log

a simple wrapper for uber-go/zap that simplifies configuration and handles log rotation

Why?

Because I often need a logger that is extremely simple to hook up and just use. However I also want callers, logfile configuration, and structured logs. Zap is a good choice except I also want to have log rotation which zap doesn't support natively.

This package is intended to be a very lightweight wrapper on top of zap while greatly simplifying the configuration, providing capbaility to easily produce colored logs or json logs, and seamlessly integrate with rotating log files.

Usage:

func main() {
	// configuration is compatible with env loading or json if you want to store it in a file
	// I also provide log.Default for convenience

	// Simply call Init with the configuration and all the files you want to log to (if any)
	err := log.Init(log.Default, "./test.log")
	if err != nil {
		log.Fatalf("unable to initialize logger: %v", err)
	}

	log.Debug("no formatting")
	log.Debugf("formatting [%s]", "here")
	log.Debugw("extra fields", log.Fields{"like": "this", "or_numbers": 1})

	log.Info("no formatting")
	log.Infof("formatting [%s]", "here")
	log.Infow("extra fields", log.Fields{"like": "this", "or_numbers": 1})

	log.Warn("no formatting")
	log.Warnf("formatting [%s]", "here")
	log.Warnw("extra fields", log.Fields{"like": "this", "or_numbers": 1})

	log.Error("no formatting")
	log.Errorf("formatting [%s]", "here")
	log.Errorw("extra fields", log.Fields{"like": "this", "or_numbers": 1})

	log.Fatal("All done")
}

Configuration

This is the options struct you can use

type Opts struct {
    Level              Level `json:"log_level"            env:"log_level"             default:"INFO"  description:"log level to log at (possible values are debug, info, warn, error, fatal, panic)"`
    MaxLogSize         int   `json:"log_max_size"         env:"log_max_size"          default:"10"    description:"Max size of a log in mb before rolling over"`
    MaxLogBackups      int   `json:"log_max_backups"      env:"log_max_backups"       default:"5"     description:"Max number of backups to keep"`
    CompressBackupLogs bool  `json:"log_compress_backups" env:"log_compress_backups"  default:"false" description:"Whether to compress backups or not"`
    Console            bool  `json:"log_console"          env:"log_console"           default:"true"  description:"Whether to log to the console or not (through stdout)"`
    CallerSkip         int   `json:"log_caller_skip"      env:"log_caller_skip"       default:"1"     description:"How many levels of stack to skip before logging in your application (defaults to 1 for this library)"`
}

Documentation

Index

Constants

View Source
const (
	DebugLevel = "DEBUG"
	InfoLevel  = "INFO"
	WarnLevel  = "WARN"
	ErrorLevel = "ERROR"
	FatalLevel = "FATAL"
	PanicLevel = "PANIC"
)

Wrap the log levels so you only have to import this library

Variables

View Source
var Default = Opts{
	Level:              InfoLevel,
	CallerSkip:         1,
	MaxLogSize:         10,
	MaxLogBackups:      5,
	CompressBackupLogs: false,
	Console:            true,
}

Default is the default config for on the fly use

Functions

func Debug

func Debug(args ...interface{})

Debug logs Debug statements

func Debugf

func Debugf(template string, args ...interface{})

Debugf logs Debugf statements

func Debugw

func Debugw(msg string, fields Fields)

Debugw logs Debugw statements

func Error

func Error(args ...interface{})

Error logs Error statements

func Errorf

func Errorf(template string, args ...interface{})

Errorf logs Errorf statements

func Errorw

func Errorw(msg string, fields Fields)

Errorw logs Errorw statements

func Fatal

func Fatal(args ...interface{})

Fatal logs Fatal statements

func Fatalf

func Fatalf(template string, args ...interface{})

Fatalf logs Fatalf statements

func Fatalw

func Fatalw(msg string, fields Fields)

Fatalw logs Fatalw statements

func GetLogger added in v1.1.0

func GetLogger() *zap.SugaredLogger

GetLogger will return an instance of the logger in case we need to plug it in somewhere

func Info

func Info(args ...interface{})

Info logs info statements

func Infof

func Infof(template string, args ...interface{})

Infof logs infof statements

func Infow

func Infow(msg string, fields Fields)

Infow logs infow statements

func Init

func Init(opts Opts, logPaths ...string) (err error)

Init initializes the logger a level and any number of log files (including none)

func Panic

func Panic(args ...interface{})

Panic logs Panic statements

func Panicf

func Panicf(template string, args ...interface{})

Panicf logs Panicf statements

func Panicw

func Panicw(msg string, fields Fields)

Panicw logs Panicw statements

func Warn

func Warn(args ...interface{})

Warn logs Warn statements

func Warnf

func Warnf(template string, args ...interface{})

Warnf logs Warnf statements

func Warnw

func Warnw(msg string, fields Fields)

Warnw logs Warnw statements

Types

type Fields

type Fields map[string]interface{}

Fields are key values that we will decorate the message with

type Level

type Level string

Level is a simple wrapper for log levels so you just import this library

type Opts

type Opts struct {
	Level              Level `` /* 166-byte string literal not displayed */
	MaxLogSize         int   `` /* 129-byte string literal not displayed */
	MaxLogBackups      int   `json:"log_max_backups"      env:"log_max_backups"       default:"5"     description:"Max number of backups to keep"`
	CompressBackupLogs bool  `json:"log_compress_backups" env:"log_compress_backups"  default:"false" description:"Whether to compress backups or not"`
	Console            bool  `` /* 139-byte string literal not displayed */
	CallerSkip         int   `` /* 186-byte string literal not displayed */
}

Opts are options you can configure the logger with

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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