@@ -13,108 +13,86 @@ Logrus structured logging is supported in Sentry Go SDK version `0.34.0` and abo
1313
1414## Install
1515
16- Install the ` sentrylogrus ` package:
1716``` bash
17+ go get github.com/getsentry/sentry-go
1818go get github.com/getsentry/sentry-go/logrus
1919```
2020
2121## Configure
2222
23- To enable logging, you need to initialize the SDK with the ` EnableLogs ` option set to true.
23+ ### Initialize the Sentry SDK
2424
25- ``` go
26- sentry.Init (sentry.ClientOptions {
27- Dsn : " ___PUBLIC_DSN___" ,
28- // Enable logs to be sent to Sentry
29- EnableLogs : true ,
30- })
31- ```
25+ <PlatformContent includePath = " getting-started-config" />
3226
3327### Options
3428
3529` sentrylogrus ` provides two types of hooks to configure the integration with Sentry. Both hooks accept these options:
3630- ** Levels** : A slice of ` logrus.Level ` specifying which log levels to capture
3731- ** ClientOptions** : Standard ` sentry.ClientOptions ` for configuration
3832
39- ## Usage
33+ ## Verify
4034
4135To integrate Sentry with Logrus, you can set up both log hooks and event hooks to capture different types of data at various log levels.
4236
4337``` go
44- func main () {
45- // Initialize Logrus
46- logger := logrus.New ()
47-
48- // Log DEBUG and higher level logs to STDERR
49- logger.Level = logrus.DebugLevel
50- logger.Out = os.Stderr
51-
52- // send logs on InfoLevel
53- logHook , err := sentrylogrus.NewLogHook (
54- []logrus.Level {logrus.InfoLevel },
55- sentry.ClientOptions {
56- Dsn: " ___PUBLIC_DSN___" ,
57- BeforeSend: func (event *sentry.Event , hint *sentry.EventHint ) *sentry.Event {
58- if hint.Context != nil {
59- if req , ok := hint.Context .Value (sentry.RequestContextKey ).(*http.Request ); ok {
60- // You have access to the original Request
61- fmt.Println (req)
62- }
63- }
64- fmt.Println (event)
65- return event
66- },
67- // need to have logs enabled
68- EnableLogs: true ,
69- AttachStacktrace: true ,
70- })
71-
72- // send events on Error, Fatal, Panic levels
73- eventHook , err := sentrylogrus.NewEventHook ([]logrus.Level {
74- logrus.ErrorLevel ,
75- logrus.FatalLevel ,
76- logrus.PanicLevel ,
77- }, sentry.ClientOptions {
78- Dsn: " ___PUBLIC_DSN___" ,
79- BeforeSend: func (event *sentry.Event , hint *sentry.EventHint ) *sentry.Event {
80- if hint.Context != nil {
81- if req , ok := hint.Context .Value (sentry.RequestContextKey ).(*http.Request ); ok {
82- // You have access to the original Request
83- fmt.Println (req)
84- }
85- }
86- fmt.Println (event)
87- return event
88- },
89- AttachStacktrace: true ,
90- })
91- if err != nil {
92- panic (err)
93- }
94- defer eventHook.Flush (5 * time.Second )
95- defer logHook.Flush (5 * time.Second )
96- logger.AddHook (eventHook)
97- logger.AddHook (logHook)
98-
99- // Flushes before calling os.Exit(1) when using logger.Fatal
100- // (else all defers are not called, and Sentry does not have time to send the event)
101- logrus.RegisterExitHandler (func () {
102- eventHook.Flush (5 * time.Second )
103- logHook.Flush (5 * time.Second )
104- })
105-
106- // Log a InfoLevel entry STDERR which is sent as a log to Sentry
107- logger.Infof (" Application has started" )
108-
109- // Log an error to STDERR which is also sent to Sentry
110- logger.Errorf (" oh no!" )
111-
112- // Log a fatal error to STDERR, which sends an event to Sentry and terminates the application
113- logger.Fatalf (" can't continue..." )
114-
115- // Example of logging with attributes
116- logger.WithField (" user" , " test-user" ).Error (" An error occurred" )
38+ // Initialize Sentry SDK
39+ if err := sentry.Init (sentry.ClientOptions {
40+ Dsn : " ___PUBLIC_DSN___" ,
41+ EnableLogs : true ,
42+ }); err != nil {
43+ log.Fatalf (" Sentry initialization failed: %v " , err)
44+ }
45+
46+ logger := logrus.New ()
47+ logger.Level = logrus.DebugLevel
48+ logger.Out = os.Stderr
49+
50+ // Get the Sentry client from the current hub
51+ hub := sentry.CurrentHub ()
52+ client := hub.Client ()
53+ if client == nil {
54+ log.Fatalf (" Sentry client is nil" )
11755}
56+
57+ // Create log hook to send logs on Info level
58+ logHook := sentrylogrus.NewLogHookFromClient (
59+ []logrus.Level {logrus.InfoLevel },
60+ client,
61+ )
62+
63+ // Create event hook to send events on Error, Fatal, Panic levels
64+ eventHook := sentrylogrus.NewEventHookFromClient (
65+ []logrus.Level {
66+ logrus.ErrorLevel ,
67+ logrus.FatalLevel ,
68+ logrus.PanicLevel ,
69+ },
70+ client,
71+ )
72+
73+ defer eventHook.Flush (5 * time.Second )
74+ defer logHook.Flush (5 * time.Second )
75+ logger.AddHook (eventHook)
76+ logger.AddHook (logHook)
77+
78+ // Flushes before calling os.Exit(1) when using logger.Fatal
79+ // (else all defers are not called, and Sentry does not have time to send the event)
80+ logrus.RegisterExitHandler (func () {
81+ eventHook.Flush (5 * time.Second )
82+ logHook.Flush (5 * time.Second )
83+ })
84+
85+ // Info level is sent as a log to Sentry
86+ logger.Infof (" Application has started" )
87+
88+ // Example of logging with attributes
89+ logger.WithField (" user" , " test-user" ).Error (" An error occurred" )
90+
91+ // Error level is sent as an error event to Sentry
92+ logger.Errorf (" oh no!" )
93+
94+ // Fatal level is sent as an error event to Sentry and terminates the application
95+ logger.Fatalf (" can't continue..." )
11896```
11997
12098### LogHook
@@ -139,16 +117,21 @@ logHook, err := sentrylogrus.NewLogHook(
139117Use ` NewLogHookFromClient ` if you've already initialized the Sentry SDK.
140118``` go
141119if err := sentry.Init (sentry.ClientOptions {
142- Dsn :
" https://[email protected] /0 " ,
120+ Dsn : " ___PUBLIC_DSN___ " ,
143121 EnableLogs : true ,
144122}); err != nil {
145123 log.Fatalf (" Sentry initialization failed: %v " , err)
146124}
147125hub := sentry.CurrentHub ()
148- logHook := sentrylogrus.NewLogHookFromClient (
149- []logrus.Level {logrus.InfoLevel , logrus.WarnLevel },
150- hub.Client (),
151- )
126+ client := hub.Client ()
127+ if client != nil {
128+ logHook := sentrylogrus.NewLogHookFromClient (
129+ []logrus.Level {logrus.InfoLevel , logrus.WarnLevel },
130+ client,
131+ )
132+ } else {
133+ log.Fatalf (" Sentrylogrus initialization failed: nil client" )
134+ }
152135```
153136
154137### EventHook
@@ -179,10 +162,15 @@ if err := sentry.Init(sentry.ClientOptions{
179162 log.Fatalf (" Sentry initialization failed: %v " , err)
180163}
181164hub := sentry.CurrentHub ()
182- eventHook := sentrylogrus.NewEventHookFromClient (
183- []logrus.Level {logrus.InfoLevel , logrus.WarnLevel },
184- hub.Client (),
185- )
165+ client := hub.Client ()
166+ if client != nil {
167+ eventHook := sentrylogrus.NewEventHookFromClient (
168+ []logrus.Level {logrus.InfoLevel , logrus.WarnLevel },
169+ client,
170+ )
171+ } else {
172+ log.Fatalf (" Sentrylogrus initialization failed: nil client" )
173+ }
186174```
187175
188176<Alert >
0 commit comments