From b0348e1e02527ce297517a95def39ac1a4f72fcc Mon Sep 17 00:00:00 2001 From: Sophie Wigmore Date: Fri, 7 Jan 2022 15:32:03 -0500 Subject: [PATCH] Add a WithLevel function to scribe.Emitter --- scribe/emitter.go | 7 +++++++ scribe/emitter_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/scribe/emitter.go b/scribe/emitter.go index 4660453f..a7cbe312 100644 --- a/scribe/emitter.go +++ b/scribe/emitter.go @@ -26,6 +26,13 @@ func NewEmitter(output io.Writer) Emitter { } } +// WithLevel takes in a log level string and configures the underlying Logger +// log level. To enable debug logging the log level must be set to "DEBUG". +func (e Emitter) WithLevel(level string) Emitter { + e.Logger = e.Logger.WithLevel(level) + return e +} + // SelectedDependency takes in a buildpack plan entry, a postal dependency, and // the current time, and prints out a message giving the name and version of // the dependency as well as the source of the request for that given diff --git a/scribe/emitter_test.go b/scribe/emitter_test.go index 1d0033e1..17e2ba7b 100644 --- a/scribe/emitter_test.go +++ b/scribe/emitter_test.go @@ -149,6 +149,38 @@ func testEmitter(t *testing.T, context spec.G, it spec.S) { }) }) + context("WithLevel", func() { + context("default", func() { + it("output includes debug level logs", func() { + emitter.Title("non-debug title") + emitter.Debug.Title("debug title") + + Expect(buffer.String()).To(ContainLines( + "non-debug title", + )) + Expect(buffer.String()).ToNot(ContainLines( + "debug title", + )) + }) + }) + + context("DEBUG", func() { + it.Before(func() { + emitter = emitter.WithLevel("DEBUG") + }) + + it("output includes debug level logs", func() { + emitter.Title("non-debug title") + emitter.Debug.Title("debug title") + + Expect(buffer.String()).To(ContainLines( + "non-debug title", + "debug title", + )) + }) + }) + }) + context("Candidates", func() { it("logs the candidate entries", func() { emitter.Candidates([]packit.BuildpackPlanEntry{