|
| 1 | +package zapsentry_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/getsentry/sentry-go" |
| 9 | + "go.uber.org/zap" |
| 10 | + "go.uber.org/zap/zapcore" |
| 11 | + "go.uber.org/zap/zaptest/observer" |
| 12 | + |
| 13 | + "github.com/TheZeroSlave/zapsentry" |
| 14 | +) |
| 15 | + |
| 16 | +func ExampleAttachCoreToLogger() { |
| 17 | + // Setup zap with observer (for testing), originally we use |
| 18 | + // config = zap.NewDevelopmentConfig() |
| 19 | + // logger, err := config.Build() |
| 20 | + // to build zap logger, here we use zap/zaptest/observer for testing |
| 21 | + core, recordedLogs := observer.New(zapcore.DebugLevel) |
| 22 | + logger := zap.New(core, zap.AddStacktrace(zap.DebugLevel)) |
| 23 | + |
| 24 | + // Setup mock sentry client for testing, in general we use sentry.NewClient |
| 25 | + var recordedSentryEvent *sentry.Event |
| 26 | + sentryClient := mockSentryClient(func(event *sentry.Event) { |
| 27 | + recordedSentryEvent = event |
| 28 | + }) |
| 29 | + |
| 30 | + // Setup zapsentry |
| 31 | + core, err := zapsentry.NewCore(zapsentry.Configuration{ |
| 32 | + Level: zapcore.ErrorLevel, // when to send message to sentry |
| 33 | + Tags: map[string]string{ |
| 34 | + "component": "system", |
| 35 | + }, |
| 36 | + }, zapsentry.NewSentryClientFromClient(sentryClient)) |
| 37 | + if err != nil { |
| 38 | + log.Fatal(err) |
| 39 | + } |
| 40 | + newLogger := zapsentry.AttachCoreToLogger(core, logger) |
| 41 | + |
| 42 | + // Send error log |
| 43 | + newLogger.Error("[error] something went wrong!", zap.String("method", "unknown")) |
| 44 | + |
| 45 | + // Check output |
| 46 | + fmt.Println(recordedLogs.All()[0].Message) |
| 47 | + fmt.Println(recordedSentryEvent.Message) |
| 48 | + fmt.Println(recordedSentryEvent.Extra) |
| 49 | + // Output: [error] something went wrong! |
| 50 | + // [error] something went wrong! |
| 51 | + // map[method:unknown] |
| 52 | +} |
| 53 | + |
| 54 | +func mockSentryClient(f func(event *sentry.Event)) *sentry.Client { |
| 55 | + client, _ := sentry.NewClient(sentry.ClientOptions{ |
| 56 | + Dsn: "", |
| 57 | + Transport: &transport{MockSendEvent: f}, |
| 58 | + }) |
| 59 | + return client |
| 60 | +} |
| 61 | + |
| 62 | +type transport struct { |
| 63 | + MockSendEvent func(event *sentry.Event) |
| 64 | +} |
| 65 | + |
| 66 | +// Flush waits until any buffered events are sent to the Sentry server, blocking |
| 67 | +// for at most the given timeout. It returns false if the timeout was reached. |
| 68 | +func (f *transport) Flush(_ time.Duration) bool { return true } |
| 69 | + |
| 70 | +// Configure is called by the Client itself, providing it it's own ClientOptions. |
| 71 | +func (f *transport) Configure(_ sentry.ClientOptions) {} |
| 72 | + |
| 73 | +// SendEvent assembles a new packet out of Event and sends it to remote server. |
| 74 | +// We use this method to capture the event for testing |
| 75 | +func (f *transport) SendEvent(event *sentry.Event) { |
| 76 | + f.MockSendEvent(event) |
| 77 | +} |
| 78 | + |
0 commit comments