getzap

package module
v0.0.0-...-1b86d84 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 5 Imported by: 1

README

getzap go.dev reference

Package getzap provides a collection of sophisticated uber-go/zap loggers.

Documentation

Overview

Package getzap provides a collection of sophisticated uber-go/zap loggers.

For each logger, the following aspects are considered:

| Setting              | Option #1      | Option #2 |
| -------------------- | -------------- | --------- |
| Environment          | Dev            | Prod      |
| Format               | TSV with color | JSON      |
| Output Target        | Console        | File      |
| Separate Errors      | Yes            | No        |
| Compress Legacy Logs | Yes            | No        |

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDevelopmentLogger

func GetDevelopmentLogger(normalLogPath, errorLogPath string) *zap.Logger

GetDevelopmentLogger returns a sophisticated, customized logger for development.

Logs with level below than ERROR will be written to:

a) stdout in TSV format with colored level;
b) normal log files in JSON format with rotation and expiration;

Logs with level ERROR or above will be written to:

a) stderr in TSV format with colored level;
b) error log files in JSON format with rotation (max size is 10MB, retain at most 20 files) and expiration (14 days);

Logs with level DPANIC or above will cause panic after writing the message.

Log files won't be created if the given log file path is empty or blank.

Example
package main

import (
	"time"

	"github.com/logcfg/getzap"
	"go.uber.org/zap"
)

func main() {
	logger := getzap.GetDevelopmentLogger("log/access.log", "log/error.log")
	defer logger.Sync()

	logger.Debug("This is a DEBUG message")
	logger.Info("This is an INFO message")
	logger.Info("This is an INFO message with fields", zap.Int("id", 1), zap.Duration("sleep", 128*time.Millisecond))
	logger.Sugar().Infof("The answer to life the universe and everything = %d", 42)
	logger.Warn("This is a WARN message")
	logger.Error("This is an ERROR message")

}
Output:

func GetProductionLogger

func GetProductionLogger(logPath string) *zap.Logger

GetProductionLogger returns a sophisticated, customized logger for production deployment.

Logs with level below than INFO will be ignored.

Logs with level INFO or above will be written to:

a) stdout in JSON format;
b) log files in JSON format with rotation (max size is 10MB), expiration (30 days) and compression (.gz);

Logs with level PANIC or above will cause panic after writing the message.

Log files won't be created if the given log file path is empty or blank.

Example
package main

import (
	"time"

	"github.com/logcfg/getzap"
	"go.uber.org/zap"
)

func main() {
	logger := getzap.GetProductionLogger("log/app.log")
	defer logger.Sync()

	logger.Debug("This is a DEBUG message")
	logger.Info("This is an INFO message")
	logger.Info("This is an INFO message with fields", zap.Int("id", 1), zap.Duration("sleep", 64*time.Millisecond))
	logger.Warn("This is a WARN message")
	logger.Error("This is an ERROR message")
	logger.DPanic("This is a DPANIC message")

}
Output:

func SetGlobalDevelopmentLogger

func SetGlobalDevelopmentLogger(normalLogPath, errorLogPath string)

SetGlobalDevelopmentLogger sets the logger returned from GetDevelopmentLogger() as the global Logger and SugaredLogger of zap.

Example
package main

import (
	"time"

	"github.com/logcfg/getzap"
	"go.uber.org/zap"
)

func main() {
	getzap.SetGlobalDevelopmentLogger("", "log/error.log")
	logger := zap.L().Sugar().Named("dev")

	logger.Debug("This is a DEBUG message")
	logger.Info("This is an INFO message")
	logger.Infow("This is an INFO message with fields", "id", 1, "sleep", 256*time.Millisecond)
	logger.Infof("The answer to life the universe and everything = %d", 42)
	logger.Warn("This is a WARN message")
	logger.Error("This is an ERROR message")

}
Output:

func SetGlobalProductionLogger

func SetGlobalProductionLogger(logPath string)

SetGlobalProductionLogger sets the logger returned from GetProductionLogger() as the global Logger and SugaredLogger of zap.

Example
package main

import (
	"time"

	"github.com/logcfg/getzap"
	"go.uber.org/zap"
)

func main() {
	getzap.SetGlobalProductionLogger("log/app.log")
	logger := zap.L().Named("prod")

	logger.Debug("This is a DEBUG message")
	logger.Info("This is an INFO message")
	logger.Info("This is an INFO message with fields", zap.Int("id", 1), zap.Duration("sleep", 32*time.Millisecond))
	logger.Warn("This is a WARN message")
	logger.Error("This is an ERROR message")
	logger.DPanic("This is a DPANIC message")

}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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