Skip to content

Commit

Permalink
Adds support for buildpack log level environment variable 'BP_LOG_LEV…
Browse files Browse the repository at this point in the history
…EL' which currently accepts 'INFO' (default) or 'DEBUG'

Signed-off-by: David O'Sullivan <davidos@vmware.com>
  • Loading branch information
pivotal-david-osullivan committed Aug 12, 2021
1 parent 3a76485 commit f57226d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
18 changes: 15 additions & 3 deletions poet/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down
15 changes: 15 additions & 0 deletions poet/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f57226d

Please sign in to comment.