zapsentry

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 6 Imported by: 1

README

zapsentry

Logging to sentry via zap

Basic setup

Installation:

go get github.com/yzzyx/zapsentry

Usage:

client, err := sentry.NewClient(sentry.ClientOptions{Dsn: "https://xxxx:yyyy@127.0.0.1/1"})
scope := sentry.NewScope()
hub := sentry.NewHub(client, scope)

sentryCore, err := zapsentry.NewCore(hub, zap.InfoLevel)
if err != nil {
	return nil, err
}

logger := zap.New(core)

// Log to sentry with tag 'version' set to 1, and with additional field "somefield" set to "somevalue"
logger.Error("my error", zap.Int("#version", 1), zap.String("somefield", "somevalue"))

Advanced setup

Usually a more advanced setup is required. Maybe some default scope values should be set, or logging should be done both to file and to sentry.

Below a sample setup logging both to sentry, file and console is shown, with a couple of standard fields added, and some go-sentry integrations disabled.

pe := zap.NewProductionEncoderConfig()
fileEncoder := zapcore.NewJSONEncoder(pe)

logFile, err := os.Create("my-project.log")
if err != nil {
	panic(err)
}

pe.EncodeTime = zapcore.ISO8601TimeEncoder
consoleEncoder := zapcore.NewConsoleEncoder(pe)

client, err := sentry.NewClient(sentry.ClientOptions{
	Dsn:   "http://xxx:yyy@sentry-host/2",
	// Skip default integrations, we set our own tags instead
	Integrations: func(defaultIntegrations []sentry.Integration) []sentry.Integration {
		integrations := defaultIntegrations[:0]
		for k := range defaultIntegrations {
			if defaultIntegrations[k].Name() == "Modules" ||
				defaultIntegrations[k].Name() == "Environment" ||
				defaultIntegrations[k].Name() == "ContextifyFrames" {
				continue
			}
			integrations = append(integrations, defaultIntegrations[k])
		}
		return integrations
	},
})

scope := sentry.NewScope()
scope.SetTag("version", VERSION_NUMBER)
hostname, err := os.Hostname()
if err != nil {
	panic(err)
}

scope.SetTag("server_name", hostname)
scope.SetTag("device.arch", runtime.GOARCH)
scope.SetTag("os.name", runtime.GOOS)

scope.SetTag("runtime.name", "go")
scope.SetTag("runtime.version", runtime.Version())
hub := sentry.NewHub(client, scope)

sentryCore, err := NewCore(hub, level)
if err != nil {
	panic(err)
}

core := zapcore.NewTee(
	zapcore.NewCore(fileEncoder, zapcore.AddSync(f), level),
	zapcore.NewCore(consoleEncoder, IgnoreSync(os.Stdout), level),
	sentryCore,
)

l := zap.New(core)

Documentation

Overview

Copyright (c) 2019 Elias Norberg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

View Source
const DefaultTagPrefix = "#"

DefaultTagPrefix defines the default prefix that is used to mark a zap field as a sentry tag All fields that do not have this tag will be added as 'extra' Setting a prefix in the config variable when creating a new core replaces this value in that specific instance of the core

Variables

View Source
var (
	ErrFlushTimeout             = errors.New("sentry Flush() timeout occurred")
	ErrClientOrScopeUnavailable = errors.New("sentry client or scope is unavailable")
)

Errors that might occur when logging to sentry

Functions

func NewCore

func NewCore(hub *sentry.Hub, enab zapcore.LevelEnabler, fields ...zapcore.Field) (zapcore.Core, error)

NewSentryCore creates a new zapcore.Core that logs information to Sentry

Types

type SentryCore

type SentryCore struct {
	zapcore.LevelEnabler

	// Prefix used in field names to denote that they should be marked as a sentry tag.
	// If not specified, DefaultTagPrefix will be used
	TagPrefix string
	// contains filtered or unexported fields
}

SentryCore defines a zapcore.Core that logs information to Sentry

func (*SentryCore) Check

func (core *SentryCore) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry

Check determines whether the supplied Entry should be logged

func (*SentryCore) Sync

func (core *SentryCore) Sync() error

Sync flushes buffered logs (if any)

func (*SentryCore) With

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

With adds structured context to the Core

func (*SentryCore) Write

func (core *SentryCore) Write(entry zapcore.Entry, fields []zapcore.Field) error

Write serializes the Entry and any Fields supplied at the log site and writes them to the sentry client used when creating the SentryCore instance

Jump to

Keyboard shortcuts

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