log

package module
v0.0.0-...-47519c1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2021 License: Unlicense Imports: 3 Imported by: 1

README

log GoDoc

log acts as drop-in replacement for std/log and uses the power of Uber's Zap logging library internally.

It implements the following Zap cores:

Usage

A development setup is used by default. Similar to std/log. All logs are written to stderr.

import "github.com/mattes/log"

func main() {
  defer log.Sync()

  log.Info("Hello world")
}

In production the setup depends on where you want to ship your logs to. Here is an example that ships all logs to Google Stackdriver, as well as Google Error Reporting.

import (
  "go.uber.org/zap"
  "go.uber.org/zap/zapcore"
  "github.com/mattes/log"
  gerr "github.com/mattes/log/googleErrorReporting"
  gsdr "github.com/mattes/log/googleStackdriver"

)

func init() {
  cores := []zapcore.Core{}

  {
    // Stackdriver core
    c := gsdr.NewConfig()
    c.LogID = "my-service.v2"
    core, err := c.Build()
    cores = append(cores, core)
  }

  {
    // Error reporting core
    c := gerr.NewConfig()
    c.ServiceName = "my-service"
    c.ServiceVersion = "v2"
    core, err := c.Build()
    cores = append(cores, core)
  }

  // Build Zap logger with options
  logger := zap.New(zapcore.NewTee(cores...)).WithOptions(
    zap.AddCaller(),
    zap.AddCallerSkip(1),
    zap.AddStacktrace(zapcore.ErrorLevel),
    log.ErrorOutput("stderr"), // for internal errors
    log.Sampling(100, 100),
  )

  // Set global logger
  log.Use(logger)
}

func main() {
  defer log.Sync()
  defer log.CapturePanic() // optional, it logs unhandled panics before crashing

  log.Error("Hello Production!")
}

Changing log level

Update the config to use a reference of zap#AtomicLevel that you control. It can serve as HTTP handler, too.

Replacing logger in third-party lib

Sometimes third-party libraries log on their own with no way of disabling it. With the help of Go modules we can replace third-party logging libraries and instruct them to log through our logging infrastructure.

Testing

Some tests require credentials and configration that can't be commited to the repo. I recommend putting a .env file in each directory with contents like:

export SLACK_URL='https://hooks.slack.com/services/xxx'

Then run test with source .env && go test -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CapturePanic

func CapturePanic()

CapturePanic captures, logs and re-throws a panic. Only useful when used with defer.

func DPanic

func DPanic(args ...interface{})

func DPanicf

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

func DPanicw

func DPanicw(msg string, keysAndValues ...interface{})

func Debug

func Debug(args ...interface{})

func Debugf

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

func Debugw

func Debugw(msg string, keysAndValues ...interface{})

func Error

func Error(args ...interface{})

func ErrorOutput

func ErrorOutput(paths ...string) zap.Option

ErrorOutput is a convenience function that creates a zapcore.WriteSyncer and returns it as zap.ErrorOutput option. It panics if path can't be opened. Example: ErrorOutput("stderr")

func Errorf

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

func Errorw

func Errorw(msg string, keysAndValues ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

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

func Fatalln

func Fatalln(v ...interface{})

func Fatalw

func Fatalw(msg string, keysAndValues ...interface{})

func Info

func Info(args ...interface{})

func Infof

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

func Infow

func Infow(msg string, keysAndValues ...interface{})

func Named

func Named(name string) *zap.SugaredLogger

func NewDevelopmentConfig

func NewDevelopmentConfig() zap.Config

func NewDevelopmentEncoderConfig

func NewDevelopmentEncoderConfig() zapcore.EncoderConfig

func Panic

func Panic(args ...interface{})

func Panicf

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

func Panicln

func Panicln(v ...interface{})

func Panicw

func Panicw(msg string, keysAndValues ...interface{})

func Print

func Print(v ...interface{})

func Printf

func Printf(format string, v ...interface{})

func Println

func Println(v ...interface{})

func Sampling

func Sampling(initial, thereafter int) zap.Option

Sampling is a convenience function that returns a zap.Option which wraps a core with a sample policy.

func Sync

func Sync() error

func Use

func Use(logger *zap.Logger)

Use sets the default logger used by the package

func Warn

func Warn(args ...interface{})

func Warnf

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

func Warnw

func Warnw(msg string, keysAndValues ...interface{})

func With

func With(args ...interface{}) *zap.SugaredLogger

Types

This section is empty.

Directories

Path Synopsis
glog module

Jump to

Keyboard shortcuts

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