Skip to content

Commit

Permalink
Merge pull request #69 from pivotal-david-osullivan/common_logging
Browse files Browse the repository at this point in the history
Adds support for buildpack log level environment variable BP_LOG_LEVEL
  • Loading branch information
ekcasey authored Sep 24, 2021
2 parents d893d2a + 7eb31bf commit 0a77d8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
16 changes: 13 additions & 3 deletions poet/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,23 @@ 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 0a77d8c

Please sign in to comment.