logger

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: MIT Imports: 11 Imported by: 2

README

Logger

This project is a wrapper around the excellent logging framework zap. It provides some opinionated encoders as well as an implementation for a redis sink.

As an aside - the module also includes a work in progress mechanism for setting log level per package.

Example Use

For an in depth overview - please check the zap documentation

Local Development Example
// assumed imports

func main() {
    // configure global settings that will
	// apply to all instance of a logger
	logger.Configure(
		logger.AppName("my-app"),
		logger.Level(zapcore.DebugLevel),
		logger.Mode(mode.Development),
	)

	// New() returns a logger with a strongly typed logging context intended for use in performance critical paths
	log := logger.New()

	log.Info("hello world, this is some info",
		zap.String("field", "value"),
		zap.Int64("count", 12315))

	// Slightly less performant, but simpler to use, the "Sugar" logger provides a loosely typed field context
	sLog := logger.New().Sugar()

	sLog.Warnw("this logger is sugared",
		"count", 1234,
		"flavor", "tasty",
    )
}

Logging to remote redis sink with JSON encoded log output
// assumed imports

func main() {

    p, err := newpool("localhost:6379", "password123")

	rsink := redis.NewSink("logstash.stg.bolcom", p)
	if err != nil {
		log.Fatal("is redis running?", err)
	}

    // all logs will write to the local console as well as to the provided redis instance
	logger.Configure(
		logger.AppName("test-app"),
		logger.Level(zapcore.DebugLevel),
		logger.Mode(mode.Production),
		logger.JSONWriter(rsink),
    )

    log := logger.New().Sugar()

    // log away!


}

// example func for setting up a redigo redis pool
func newpool(addr, password string) (*redigo.Pool, error) {
	pool := &redigo.Pool{
		Dial: func() (redigo.Conn, error) {
			conn, err := redigo.Dial("tcp",
				addr,
				redigo.DialPassword(password),
				redigo.DialConnectTimeout(time.Second*5),
			)

			if err != nil {
				return nil, err
			}

			return conn, err
		},
		TestOnBorrow: func(c redigo.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}

	conn := pool.Get()
	defer conn.Close()

	err := conn.Send("PING")
	if err != nil {
		return nil, err
	}

	return pool, nil
}


Changing log level for all loggers in a package
// assumed imports


func main() {

    // ... log config already set up

    err = logger.SetLevelForPackage("main", zapcore.WarnLevel)
	if err != nil {
		log.Warn(err.Error())
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(options ...Option)

Configure will apply all the supplied options to a global configuration that will be applied to all logger instances.

func GetPackages

func GetPackages() []string

GetPackages retuns all package names that logger instances have been created in

func New

func New() *zap.Logger

New returns an instance of a logger configured via the logger package global options

func SetLevelForPackage

func SetLevelForPackage(pkg string, level zapcore.Level) error

SetLevelForPackage will set the log level for all instances of a logger in the provided package, returning an error if the package name provided does not exist in the registry

Types

type Config

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

Config has settings that are globally applied to all logging instances. It can be configured via a call to Configure with a list of options

type Option

type Option func(config *Config)

An Option can be used to apply a value to a setting on the global config

func AppName

func AppName(name string) Option

AppName sets the "application" field to the provided value on all logging contexts

func ConsoleWriter

func ConsoleWriter(w io.Writer) Option

ConsoleWriter sets the writer that will receive console formatted output from a logger

func JSONWriter

func JSONWriter(w io.Writer) Option

JSONWriter sets the writer that will receive json formatted output from a logger

func Level

func Level(lvl zapcore.Level) Option

Level sets the default log level of all logger instances

func Mode

func Mode(m mode.Kind) Option

Mode sets the kind of mode loggers and their respective encoders should run in

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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