log

package
v0.0.0-...-74273eb Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 4 Imported by: 0

README

log

Package log provides a logger interface which is a thin wrapper around zap's SugaredLogger.

Usage

Initialize:

One may want to do this in main() function. Create a logger instance to be passed around:

logger, err := log.NewLogger(&log.Config{
        Environment: "prod",
})
if err != nil {
        // fail
}
defer logger.Sync()

Note: Use environment local for getting colored log levels during local development for better readability.

Log with fields:

Logger methods that end with w allows you to log with fields. For example:

logger.Infow("this is a message", "field1", "value1", "field2", "value2")
logger.Errorw("this is a message", "field1", "value1", "field2", "value2")

You can also create a persistent field logger:

logger = logger.With("req-id", uuid.New().String())
// all further logging using the above logger will have req-id as a field

logger.Errorf("some error") // this will have req-id as a field

Alerts:

Define an alert:

package alert

const (
        svcName = "PLV_MSG_SOME_SERVICE"
)

var Startup = log.NewAlert(log.AlertP0, svcName, "startup failure")

Use it wherever applicable:

db, err := db.New(connStr)
if err != nil {
        logger.WithAlert(alert.Startup).Fatalw("db.New() failed", "error", err.Error())
}
Unit testing

Mock logger:

If you want to mock the logger to assert that something was indeed logged, you can use the mock logger available in mocks sub-package as follows:

import "github.com/plivo/pkg/log/mocks"

logger := new(mocks.Logger)

// define desired mock behavior
logger.On("With", "req-id", mock.Anything).Return(logger)

No-op logger:

If you have a large number of log calls in a function and do not wish to mock the logger, you can use a no-op logger as follows:

logger, err := log.NewLogger(&log.Config{
        Environment: "no-op",
})

The no-op logger internally writes to io.Discard.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NewLogger = NewZapLogger

Functions

func NewAlert

func NewAlert(priority AlertPriority, servicePrefix string, alertType string) logAlert

NewAlert ceates a new instance of logAlert type which implements the Alert interface.

Types

type Alert

type Alert interface {
	Priority() AlertPriority
	fmt.Stringer
}

Alert interface is implemented by all alerts.

type AlertPriority

type AlertPriority int

AlertPriority is a type representing priority of an alert.

const (
	AlertP0 AlertPriority = iota
	AlertP1
	AlertP2
)

type Config

type Config struct {
	Environment string `json:"environment" toml:"environment" validate:"oneof=dev development prod production no-op"`
}

type Logger

type Logger interface {

	// With returns a Logger containing fields provided
	// and is similar to logrus's FieldLogger
	With(args ...interface{}) Logger
	WithAlert(alert Alert) Logger
	io.Writer
	// contains filtered or unexported methods
}

Logger is an interface that all logging systems will implement.

func NewZapLogger

func NewZapLogger(config *Config) (Logger, error)

NewZapLogger returns a new instance of *zap.SugaredLogger which implements the Logger interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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