-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New log encoder to report errors through stackdriver (#42)
* new encoder, zapdriver config * Update logger.go * stacktrace into message only on ErrorLevel * inherit from zapcore.Encoder * alter error with >= zapcore.ErrorLevel * if stacktrace is present put it in the message * test of grpc interceptor report on error * Revert "test of grpc interceptor report on error" This reverts commit 5accb62. * implement clone function
- Loading branch information
1 parent
225ad19
commit f7e60f5
Showing
2 changed files
with
62 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package zapvml | ||
|
||
import ( | ||
"regexp" | ||
|
||
"go.uber.org/zap/buffer" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func newEncoder(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) { | ||
return &Encoder{zapcore.NewJSONEncoder(cfg)}, nil | ||
} | ||
|
||
// Wraps zapcore.Encoder to customize stack traces to be picked up by Stackdriver error reporting. | ||
// The following issue might make this unnecessary: | ||
// https://github.com/uber-go/zap/issues/514 | ||
type Encoder struct { | ||
zapcore.Encoder | ||
} | ||
|
||
// multiline pattern to match the function name line | ||
var functionNamePattern = regexp.MustCompile(`(?m)^(\S+)$`) | ||
|
||
func (s *Encoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) { | ||
if ent.Stack != "" { | ||
// Make the message look like a real panic, so Stackdriver error reporting picks it up. | ||
// This used to need the string "panic: " at the beginning, but no longer seems to need it! | ||
// ent.Message = "panic: " + ent.Message + "\n\ngoroutine 1 [running]:\n" | ||
ent.Message = ent.Message + "\n\ngoroutine 1 [running]:\n" | ||
|
||
// Trial-and-error: On App Engine Standard go111 the () are needed after function calls | ||
// zap does not add them, so hack it with a regexp | ||
replaced := functionNamePattern.ReplaceAllString(ent.Stack, "$1(...)") | ||
ent.Message += replaced | ||
ent.Stack = "" | ||
} | ||
return s.Encoder.EncodeEntry(ent, fields) | ||
} | ||
|
||
func (s *Encoder) Clone() zapcore.Encoder { | ||
return &Encoder{s.Encoder.Clone()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters