Skip to content

TheZeroSlave/zapsentry

This branch is up to date with master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

f7cf09e · Apr 8, 2024

History

79 Commits
Apr 4, 2024
Jan 12, 2018
Jan 1, 2018
Apr 8, 2024
Jun 26, 2023
Jun 13, 2023
Apr 2, 2024
Jun 26, 2023
Aug 28, 2019
Sep 13, 2023
Mar 1, 2024
Mar 1, 2024
Apr 4, 2024
Apr 4, 2024
Dec 16, 2023
Jan 9, 2018
Aug 28, 2019

Repository files navigation

Sentry client for zap logger

Installation

go get github.com/TheZeroSlave/zapsentry

Integration using Sentry Client

Integration of sentry client into zap.Logger is pretty simple:

func modifyToSentryLogger(log *zap.Logger, client *sentry.Client) *zap.Logger {
	cfg := zapsentry.Configuration{
		Level: zapcore.ErrorLevel, //when to send message to sentry
		EnableBreadcrumbs: true, // enable sending breadcrumbs to Sentry 
		BreadcrumbLevel: zapcore.InfoLevel, // at what level should we sent breadcrumbs to sentry, this level can't be higher than `Level`
		Tags: map[string]string{
			"component": "system",
		},
	}
	core, err := zapsentry.NewCore(cfg, zapsentry.NewSentryClientFromClient(client))
	
	// don't use value if error was returned. Noop core will be replaced to nil soon.
	if err != nil {
		panic(err)
	}
	
	log = zapsentry.AttachCoreToLogger(core, log)

	// if you have web service, create a new scope somewhere in middleware to have valid breadcrumbs.
	return log.With(zapsentry.NewScope())
}

Please note that wrapper does not guarantee that all your events will be sent before the app exits. Flush called internally only in case of writing message with severity level > zapcore.ErrorLevel (i.e. Fatal, Panic, ...). If you want to ensure your messages come to sentry - call the flush on native sentry client at defer. Example:

func main() {
	sentryClient, err := sentry.NewClient(sentry.ClientOptions{
		Dsn: "Sentry DSN",
	})
	if err != nil {
		// Handle the error here
	}
	// Flush buffered events before the program terminates.
	// Set the timeout to the maximum duration the program can afford to wait.
	defer sentryClient.Flush(2 * time.Second)
	
	// create zap log and wrapper...
	
	// create and run your app here...
}