Help us shape the future of Go! Take the 2026 Go Developer Survey

stackdriver

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2018 License: MIT Imports: 3 Imported by: 0

README

zap-stackdriver

GoDoc Build Status Coverage Status

Prints Stackdriver format logs with zap.

Installation

go get -u 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)
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,
}

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