zapup

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2020 License: MIT Imports: 8 Imported by: 0

README

zapup

Zapup is an initializer around ZAP.

It was intentionally created to provide a zap logger instance that considers:

  • log output format
  • log level
  • provides file and line number of caller

Quickstart

Create a zapuptest app:

mkdir zapuptest
cd zapuptest
go mod init

Use zapup, e.g in `main.go``:

package main

import "github.com/metal-stack/metal-lib/zapup"

var log = zapup.MustRootLogger()

func main() {
	log.Info("Hello, metal!")
}

Compile and run:

go run main.go

Expected output:

[...]
{"level":"info","ts":1540279941.106842,"caller":"zapuptest/main.go:8","msg":"Hello, metal!","app":"","customer":"","stage":""}

Configuration

Zapup can be configured by configuring environment variables. All of them have defaults:

#  Sets the log level. Valid values: "info", "debug", "warn", "error", "dpanic", "panic", "fatal". Defaults to "info".
export ZAP_LEVEL=debug
# Set the log encoding. Valid values: "console", "json". Defaults to "json".
export ZAP_ENCODING=console 
# Sets the field "customer" to annotate the log for usage e.g. within elasticsearch. Defaults to empty string.
export ZAP_CUSTOMER=""
# Sets the field "app" to annotate the log for usage e.g. within elasticsearch. Defaults to empty string.
export ZAP_APP=""
# Sets the field "stage" to annotate the log for usage e.g. within elasticsearch. Defaults to empty string.
export ZAP_STAGE="" 
# Sets the output path to write logs to. Valid values: "stdout", "stderr", "<file path>". Defaults to "stdout"
export ZAP_OUT="stdout"

Usage

See Zap.

But Obacht! Instead of:

sugar := zap.NewExample().Sugar()

Run:

sugar := zapup.MustRootLogger().Sugar()

Documentation

Overview

Package zapup (from: zap up) contains code to initialize a zap(https://github.com/uber-go/zap/) logger.

Index

Constants

View Source
const (
	// KeyLogLevel sets log level. Valid values: "info", "debug", "warn", "error", "dpanic", "panic", "fatal".
	KeyLogLevel string = "ZAP_LEVEL"
	// KeyLogEncoding sets log output encoding. Valid values: "console", "json".
	KeyLogEncoding string = "ZAP_ENCODING"
	// KeyFieldCustomer sets  meta information for field "customer" to use within log analytics.
	KeyFieldCustomer string = "ZAP_CUSTOMER"
	// KeyFieldApp sets meta information for a field "app" to use within log analytics. Defaults to empty string.
	KeyFieldApp string = "ZAP_APP"
	// KeyFieldStage sets meta information for a field "stage" to use within log analytics. Defaults to empty string.
	KeyFieldStage string = "ZAP_STAGE"
	// KeyOutput sets the output path for zap. Valid values: "stdout", "stderr", "<file path>". Defaults to empty string.
	KeyOutput string = "ZAP_OUT"
)

Variables

This section is empty.

Functions

func MustRootLogger

func MustRootLogger() *zap.Logger

MustRootLogger returns the inited logger or panics.

func PutLogger

func PutLogger(ctx context.Context, lg *zap.Logger) context.Context

func RequestLogger

func RequestLogger(rq *http.Request) *zap.Logger

RequestLogger returns the request logger from the request.

func Reset

func Reset()

Reset resets the Root Logger to enable creating it again on changed environment.

func RootLogger

func RootLogger() (*zap.Logger, error)

RootLogger initiates and returns the root logger considering environment variables: ZAP_LEVEL, ZAP_ENCODING, ZAP_CUSTOMER, ZAP_APP, ZAP_OUT. init() is explicitly NOT(!) used to avoid issues with writing logs by other init() functions and init races.

Types

This section is empty.

Jump to

Keyboard shortcuts

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