@@ -171,24 +171,36 @@ type logPanicWrapper struct {
171171 hclog.Logger
172172 panicRecorder func (string )
173173 inPanic bool
174+ // we need to enable at least error level logging to see stack traces from
175+ // go-plugin, but if the user hasn't requested any logging we will only
176+ // record them and not pass them through.
177+ captureOnly bool
174178}
175179
176180// go-plugin will create a new named logger for each plugin binary.
177181func (l * logPanicWrapper ) Named (name string ) hclog.Logger {
178- return & logPanicWrapper {
182+ wrapped := & logPanicWrapper {
179183 Logger : l .Logger .Named (name ),
180184 panicRecorder : panics .registerPlugin (name ),
181185 }
186+
187+ if wrapped .Logger .GetLevel () == hclog .Off {
188+ // if we're not logging anything from the provider, we need to set the
189+ // level to error in order to capture stack traces.
190+ wrapped .captureOnly = true
191+ wrapped .Logger .SetLevel (hclog .Error )
192+ }
193+
194+ return wrapped
182195}
183196
184- // we only need to implement Debug , since that is the default output level used
185- // by go-plugin when encountering unstructured output on stderr.
186- func (l * logPanicWrapper ) Debug (msg string , args ... interface {}) {
187- // We don't have access to the binary itself, so guess based on the stderr
188- // output if this is the start of the traceback. An occasional false
197+ // We only need to implement Error , since that is the level used by go-plugin
198+ // when encountering a stack trace on stderr.
199+ func (l * logPanicWrapper ) Error (msg string , args ... interface {}) {
200+ // We don't have access to the binary itself, so guess based on the log
201+ // stream output if this is the start of the traceback. An occasional false
189202 // positive shouldn't be a big deal, since this is only retrieved after an
190203 // error of some sort.
191-
192204 panicPrefix := strings .HasPrefix (msg , "panic: " ) || strings .HasPrefix (msg , "fatal error: " )
193205
194206 l .inPanic = l .inPanic || panicPrefix
@@ -197,5 +209,9 @@ func (l *logPanicWrapper) Debug(msg string, args ...interface{}) {
197209 l .panicRecorder (msg )
198210 }
199211
200- l .Logger .Debug (msg , args ... )
212+ if ! l .captureOnly {
213+ // pass the message through to the real logger implementation if we're
214+ // not just watching for stack traces
215+ l .Logger .Error (msg , args ... )
216+ }
201217}
0 commit comments