stackdriver

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2020 License: MIT Imports: 3 Imported by: 13 modules, 13 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

zap-stackdriver

GitHub tag (latest SemVer) go.dev reference Test codecov

Prints Stackdriver format logs with zap.

Installation

go get github.com/tommy351/zap-stackdriver

Usage

package main

import (
	"github.com/tommy351/zap-stackdriver"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	config := &zap.Config{
		Level:            zap.NewAtomicLevelAt(zapcore.InfoLevel),
		Encoding:         "json",
		EncoderConfig:    stackdriver.EncoderConfig,
		OutputPaths:      []string{"stdout"},
		ErrorOutputPaths: []string{"stderr"},
	}

	logger, err := config.Build(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
		return &stackdriver.Core{
			Core: core,
		}
	}), zap.Fields(
		stackdriver.LogServiceContext(&stackdriver.ServiceContext{
			Service: "foo",
			Version: "bar",
		}),
	))

	if err != nil {
		panic(err)
	}

	logger.Info("Hello",
		stackdriver.LogUser("token"),
		stackdriver.LogHTTPRequest(&stackdriver.HTTPRequest{
			Method:             "GET",
			URL:                "/foo",
			UserAgent:          "bar",
			Referrer:           "baz",
			ResponseStatusCode: 200,
			RemoteIP:           "1.2.3.4",
		}))
}

Documentation

Overview

Example (Basic)
package main

import (
	stackdriver "github.com/tommy351/zap-stackdriver"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	config := &zap.Config{
		Level:            zap.NewAtomicLevelAt(zapcore.InfoLevel),
		Encoding:         "json",
		EncoderConfig:    stackdriver.EncoderConfig,
		OutputPaths:      []string{"stdout"},
		ErrorOutputPaths: []string{"stderr"},
	}

	logger, err := config.Build(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
		return &stackdriver.Core{
			Core: core,
		}
	}), zap.Fields(
		stackdriver.LogServiceContext(&stackdriver.ServiceContext{
			Service: "foo",
			Version: "bar",
		}),
	))

	if err != nil {
		panic(err)
	}

	logger.Info("Hello",
		stackdriver.LogUser("token"),
		stackdriver.LogHTTPRequest(&stackdriver.HTTPRequest{
			Method:             "GET",
			URL:                "/foo",
			UserAgent:          "bar",
			Referrer:           "baz",
			ResponseStatusCode: 200,
			RemoteIP:           "1.2.3.4",
		}))
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	EncoderConfig = zapcore.EncoderConfig{
		TimeKey:        "eventTime",
		LevelKey:       "severity",
		NameKey:        "logger",
		CallerKey:      "caller",
		MessageKey:     "message",
		StacktraceKey:  "stacktrace",
		LineEnding:     zapcore.DefaultLineEnding,
		EncodeLevel:    EncodeLevel,
		EncodeTime:     zapcore.ISO8601TimeEncoder,
		EncodeDuration: zapcore.SecondsDurationEncoder,
		EncodeCaller:   zapcore.ShortCallerEncoder,
	}
)

nolint: gochecknoglobals

Functions

func EncodeLevel

func EncodeLevel(lv zapcore.Level, enc zapcore.PrimitiveArrayEncoder)

func LogHTTPRequest

func LogHTTPRequest(req *HTTPRequest) zapcore.Field

func LogReportLocation

func LogReportLocation(loc *ReportLocation) zapcore.Field

func LogServiceContext

func LogServiceContext(ctx *ServiceContext) zapcore.Field

func LogUser

func LogUser(user string) zapcore.Field

Types

type Context

type Context struct {
	User           string          `json:"user"`
	HTTPRequest    *HTTPRequest    `json:"httpRequest"`
	ReportLocation *ReportLocation `json:"reportLocation"`
}

func (*Context) Clone

func (c *Context) Clone() *Context

func (*Context) MarshalLogObject

func (c *Context) MarshalLogObject(e zapcore.ObjectEncoder) (err error)

type Core

type Core struct {
	zapcore.Core

	SetReportLocation bool
	// contains filtered or unexported fields
}

func (*Core) Check

func (c *Core) Check(entry zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry

func (*Core) Sync added in v0.1.2

func (c *Core) Sync() error

func (*Core) With

func (c *Core) With(fields []zapcore.Field) zapcore.Core

func (*Core) Write

func (c *Core) Write(entry zapcore.Entry, fields []zapcore.Field) error

type HTTPRequest

type HTTPRequest struct {
	Method             string `json:"method"`
	URL                string `json:"url"`
	UserAgent          string `json:"userAgent"`
	Referrer           string `json:"referrer"`
	ResponseStatusCode int    `json:"responseStatusCode"`
	RemoteIP           string `json:"remoteIp"`
}

func (*HTTPRequest) Clone

func (h *HTTPRequest) Clone() *HTTPRequest

func (*HTTPRequest) MarshalLogObject

func (h *HTTPRequest) MarshalLogObject(e zapcore.ObjectEncoder) error

type ReportLocation

type ReportLocation struct {
	FilePath     string
	LineNumber   int
	FunctionName string
}

func (*ReportLocation) Clone

func (r *ReportLocation) Clone() *ReportLocation

func (*ReportLocation) MarshalLogObject

func (r *ReportLocation) MarshalLogObject(e zapcore.ObjectEncoder) error

type ServiceContext

type ServiceContext struct {
	Service string `json:"service"`
	Version string `json:"version"`
}

func (*ServiceContext) Clone

func (s *ServiceContext) Clone() *ServiceContext

func (*ServiceContext) MarshalLogObject

func (s *ServiceContext) MarshalLogObject(e zapcore.ObjectEncoder) error

Jump to

Keyboard shortcuts

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