From adb1b1992937b7c0e1c3297fe969aca852c4dfcf Mon Sep 17 00:00:00 2001 From: David O'Sullivan Date: Thu, 12 Aug 2021 11:29:05 +0100 Subject: [PATCH] Adds support for buildpack log level environment variable 'BP_LOG_LEVEL' which currently accepts 'INFO' (default) or 'DEBUG' --- poet/logger.go | 18 +++++++++++++++--- poet/logger_test.go | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/poet/logger.go b/poet/logger.go index 3799b0a..9ab6b54 100644 --- a/poet/logger.go +++ b/poet/logger.go @@ -57,13 +57,25 @@ func NewLoggerWithOptions(writer io.Writer, options ...Option) Logger { func NewLogger(writer io.Writer) Logger { var options []Option - if _, ok := os.LookupEnv("BP_DEBUG"); ok { - options = append(options, WithDebug(writer)) - } + // check for presence and value of log level environment variable + options = LogLevel(options, writer) return NewLoggerWithOptions(writer, options...) } +func LogLevel(options []Option, writer io.Writer) []Option { + + // Check for older log level env variable + _, dbSet := os.LookupEnv("BP_DEBUG") + + // Then check for common buildpack log level env variable - if either are set to DEBUG/true, enable Debug Writer + if level, ok := os.LookupEnv("BP_LOG_LEVEL"); (ok && strings.ToLower(level) == "debug") || dbSet { + + options = append(options, WithDebug(writer)) + } + return options +} + // Debug formats using the default formats for its operands and writes to the configured debug writer. Spaces are added // between operands when neither is a string. func (l Logger) Debug(a ...interface{}) { diff --git a/poet/logger_test.go b/poet/logger_test.go index 49d7dc4..ebf0c13 100644 --- a/poet/logger_test.go +++ b/poet/logger_test.go @@ -64,6 +64,21 @@ func testLogger(t *testing.T, context spec.G, it spec.S) { }) }) + context("with BP_LOG_LEVEL set to DEBUG", func() { + it.Before(func() { + Expect(os.Setenv("BP_LOG_LEVEL", "DEBUG")).To(Succeed()) + l = poet.NewLogger(b) + }) + + it.After(func() { + Expect(os.Unsetenv("BP_LOG_LEVEL")).To(Succeed()) + }) + + it("configures debug", func() { + Expect(l.IsDebugEnabled()).To(BeTrue()) + }) + }) + context("with debug disabled", func() { it.Before(func() { l = poet.NewLoggerWithOptions(b)