Skip to content

Commit 55cdd69

Browse files
authored
Adds DPanic and Panic level logging (writes log, then panics) (#3689)
1 parent e127430 commit 55cdd69

11 files changed

+149
-19
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ all: update-tools clean proto bins check test
1111
# Used by Buildkite.
1212
ci-build: bins build-tests update-tools shell-check check proto go-generate gomodtidy ensure-no-changes
1313

14-
# Delete all build artefacts.
14+
# Delete all build artifacts
1515
clean: clean-bins clean-test-results
1616

1717
# Recompile proto files.

common/log/config.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,19 @@ package log
2727
type (
2828
// Config contains the config items for logger
2929
Config struct {
30-
// Stdout is true if the output needs to goto standard out
30+
// Stdout is true if the output needs to goto standard out; default is stderr
3131
Stdout bool `yaml:"stdout"`
32-
// Level is the desired log level
32+
// Level is the desired log level; see colocated zap_logger.go::parseZapLevel()
3333
Level string `yaml:"level"`
3434
// OutputFile is the path to the log output file
3535
OutputFile string `yaml:"outputFile"`
3636
// Format determines the format of each log file printed to the output.
3737
// Acceptable values are "json" or "console". The default is "json".
3838
// Use "console" if you want stack traces to appear on multiple lines.
3939
Format string `yaml:"format"`
40+
// Development determines whether the logger is run in Development (== Test) or in
41+
// Production mode. Default is Production. Production-stage disables panics from
42+
// DPanic logging.
43+
Development bool `yaml:"development"`
4044
}
4145
)

common/log/interface.go

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type (
4444
Info(msg string, tags ...tag.Tag)
4545
Warn(msg string, tags ...tag.Tag)
4646
Error(msg string, tags ...tag.Tag)
47+
DPanic(msg string, tags ...tag.Tag)
48+
Panic(msg string, tags ...tag.Tag)
4749
Fatal(msg string, tags ...tag.Tag)
4850
}
4951

common/log/interface_mock.go

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/log/lazy_logger.go

+10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func (l *lazyLogger) Error(msg string, tags ...tag.Tag) {
6868
l.logger.Error(msg, tags...)
6969
}
7070

71+
func (l *lazyLogger) DPanic(msg string, tags ...tag.Tag) {
72+
l.once.Do(l.tagLogger)
73+
l.logger.DPanic(msg, tags...)
74+
}
75+
76+
func (l *lazyLogger) Panic(msg string, tags ...tag.Tag) {
77+
l.once.Do(l.tagLogger)
78+
l.logger.Panic(msg, tags...)
79+
}
80+
7181
func (l *lazyLogger) Fatal(msg string, tags ...tag.Tag) {
7282
l.once.Do(l.tagLogger)
7383
l.logger.Fatal(msg, tags...)

common/log/noop_logger.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ func NewNoopLogger() *noopLogger {
3737
return &noopLogger{}
3838
}
3939

40-
func (n *noopLogger) Debug(string, ...tag.Tag) {}
41-
func (n *noopLogger) Info(string, ...tag.Tag) {}
42-
func (n *noopLogger) Warn(string, ...tag.Tag) {}
43-
func (n *noopLogger) Error(string, ...tag.Tag) {}
44-
func (n *noopLogger) Fatal(string, ...tag.Tag) {}
40+
func (n *noopLogger) Debug(string, ...tag.Tag) {}
41+
func (n *noopLogger) Info(string, ...tag.Tag) {}
42+
func (n *noopLogger) Warn(string, ...tag.Tag) {}
43+
func (n *noopLogger) Error(string, ...tag.Tag) {}
44+
func (n *noopLogger) DPanic(string, ...tag.Tag) {}
45+
func (n *noopLogger) Panic(string, ...tag.Tag) {}
46+
func (n *noopLogger) Fatal(string, ...tag.Tag) {}
4547
func (n *noopLogger) With(...tag.Tag) Logger {
4648
return n
4749
}

common/log/replay_logger.go

+14
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ func (r *replayLogger) Error(msg string, tags ...tag.Tag) {
8282
r.logger.Error(msg, tags...)
8383
}
8484

85+
func (r *replayLogger) DPanic(msg string, tags ...tag.Tag) {
86+
if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
87+
return
88+
}
89+
r.logger.DPanic(msg, tags...)
90+
}
91+
92+
func (r *replayLogger) Panic(msg string, tags ...tag.Tag) {
93+
if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
94+
return
95+
}
96+
r.logger.Panic(msg, tags...)
97+
}
98+
8599
func (r *replayLogger) Fatal(msg string, tags ...tag.Tag) {
86100
if workflow.IsReplaying(r.ctx) && !r.enableLogInReplay {
87101
return

common/log/throttle_logger.go

+12
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ func (tl *throttledLogger) Error(msg string, tags ...tag.Tag) {
8080
})
8181
}
8282

83+
func (tl *throttledLogger) DPanic(msg string, tags ...tag.Tag) {
84+
tl.rateLimit(func() {
85+
tl.logger.DPanic(msg, tags...)
86+
})
87+
}
88+
89+
func (tl *throttledLogger) Panic(msg string, tags ...tag.Tag) {
90+
tl.rateLimit(func() {
91+
tl.logger.Panic(msg, tags...)
92+
})
93+
}
94+
8395
func (tl *throttledLogger) Fatal(msg string, tags ...tag.Tag) {
8496
tl.rateLimit(func() {
8597
tl.logger.Fatal(msg, tags...)

common/log/with_logger.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ type withLogger struct {
3535

3636
var _ Logger = (*withLogger)(nil)
3737

38-
// With returns Logger instance that prepend every log entry with tags. If logger implements WithLogger it is used, otherwise every log call will be intercepted.
38+
// With returns Logger instance that prepend every log entry with tags. If logger implements
39+
// WithLogger it is used, otherwise every log call will be intercepted.
3940
func With(logger Logger, tags ...tag.Tag) Logger {
4041
if wl, ok := logger.(WithLogger); ok {
4142
return wl.With(tags...)
4243
}
43-
4444
return newWithLogger(logger, tags...)
4545
}
4646

@@ -52,27 +52,37 @@ func (l *withLogger) prependTags(tags []tag.Tag) []tag.Tag {
5252
return append(l.tags, tags...)
5353
}
5454

55-
// Debug writes message to the log.
55+
// Debug writes message to the log (if enabled).
5656
func (l *withLogger) Debug(msg string, tags ...tag.Tag) {
5757
l.logger.Debug(msg, l.prependTags(tags)...)
5858
}
5959

60-
// Info writes message to the log.
60+
// Info writes message to the log (if enabled).
6161
func (l *withLogger) Info(msg string, tags ...tag.Tag) {
6262
l.logger.Info(msg, l.prependTags(tags)...)
6363
}
6464

65-
// Warn writes message to the log.
65+
// Warn writes message to the log (if enabled).
6666
func (l *withLogger) Warn(msg string, tags ...tag.Tag) {
6767
l.logger.Warn(msg, l.prependTags(tags)...)
6868
}
6969

70-
// Error writes message to the log.
70+
// Error writes message to the log (if enabled).
7171
func (l *withLogger) Error(msg string, tags ...tag.Tag) {
7272
l.logger.Error(msg, l.prependTags(tags)...)
7373
}
7474

75-
// Error writes message to the log.
75+
// DPanic writes message to the log (if enabled), then calls panic() in development mode
76+
func (l *withLogger) DPanic(msg string, tags ...tag.Tag) {
77+
l.logger.DPanic(msg, l.prependTags(tags)...)
78+
}
79+
80+
// Panic writes message to the log (if enabled), then calls panic() no matter what
81+
func (l *withLogger) Panic(msg string, tags ...tag.Tag) {
82+
l.logger.Panic(msg, l.prependTags(tags)...)
83+
}
84+
85+
// Fatal writes message to the log no matter what, then terminates the process
7686
func (l *withLogger) Fatal(msg string, tags ...tag.Tag) {
7787
l.logger.Fatal(msg, l.prependTags(tags)...)
7888
}

common/log/zap_logger.go

+23-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ func NewTestLogger() *zapLogger {
5858
return NewZapLogger(BuildZapLogger(Config{
5959
// Uncomment next line if you need debug level logging in tests.
6060
// Level: "debug",
61-
Format: "console",
61+
Format: "console",
62+
Development: true,
6263
}))
6364
}
6465

@@ -145,6 +146,22 @@ func (l *zapLogger) Error(msg string, tags ...tag.Tag) {
145146
}
146147
}
147148

149+
func (l *zapLogger) DPanic(msg string, tags ...tag.Tag) {
150+
if l.zl.Core().Enabled(zap.DPanicLevel) {
151+
msg = setDefaultMsg(msg)
152+
fields := l.buildFieldsWithCallAt(tags)
153+
l.zl.DPanic(msg, fields...)
154+
}
155+
}
156+
157+
func (l *zapLogger) Panic(msg string, tags ...tag.Tag) {
158+
if l.zl.Core().Enabled(zap.PanicLevel) {
159+
msg = setDefaultMsg(msg)
160+
fields := l.buildFieldsWithCallAt(tags)
161+
l.zl.Panic(msg, fields...)
162+
}
163+
}
164+
148165
func (l *zapLogger) Fatal(msg string, tags ...tag.Tag) {
149166
if l.zl.Core().Enabled(zap.FatalLevel) {
150167
msg = setDefaultMsg(msg)
@@ -197,15 +214,13 @@ func buildZapLogger(cfg Config, disableCaller bool) *zap.Logger {
197214
if cfg.Stdout {
198215
outputPath = "stdout"
199216
}
200-
201217
encoding := "json"
202218
if cfg.Format == "console" {
203219
encoding = "console"
204220
}
205-
206221
config := zap.Config{
207222
Level: zap.NewAtomicLevelAt(parseZapLevel(cfg.Level)),
208-
Development: false,
223+
Development: cfg.Development,
209224
Sampling: nil,
210225
Encoding: encoding,
211226
EncoderConfig: encodeConfig,
@@ -258,6 +273,10 @@ func parseZapLevel(level string) zapcore.Level {
258273
return zap.WarnLevel
259274
case "error":
260275
return zap.ErrorLevel
276+
case "dpanic":
277+
return zap.DPanicLevel
278+
case "panic":
279+
return zap.PanicLevel
261280
case "fatal":
262281
return zap.FatalLevel
263282
default:

common/log/zap_logger_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func (s *LogSuite) TestParseLogLevel() {
6262
s.Equal(zap.WarnLevel, parseZapLevel("warn"))
6363
s.Equal(zap.ErrorLevel, parseZapLevel("error"))
6464
s.Equal(zap.FatalLevel, parseZapLevel("fatal"))
65+
s.Equal(zap.DPanicLevel, parseZapLevel("dpanic"))
66+
s.Equal(zap.PanicLevel, parseZapLevel("panic"))
6567
s.Equal(zap.InfoLevel, parseZapLevel("unknown"))
6668
}
6769

@@ -77,6 +79,27 @@ func (s *LogSuite) TestNewLogger() {
7779
s.NotNil(log)
7880
_, err := os.Stat(dir + "/test.log")
7981
s.Nil(err)
82+
log.DPanic("Development default is false; should not panic here!")
83+
s.Panics(nil, func() {
84+
log.Panic("Must Panic")
85+
})
86+
87+
cfg = Config{
88+
Level: "info",
89+
OutputFile: dir + "/test.log",
90+
Development: true,
91+
}
92+
log = BuildZapLogger(cfg)
93+
s.NotNil(log)
94+
_, err = os.Stat(dir + "/test.log")
95+
s.Nil(err)
96+
s.Panics(nil, func() {
97+
log.DPanic("Must panic!")
98+
})
99+
s.Panics(nil, func() {
100+
log.Panic("Must panic!")
101+
})
102+
80103
}
81104

82105
func TestDefaultLogger(t *testing.T) {

0 commit comments

Comments
 (0)