Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for buildpack log level environment variable BP_LOG_LEVEL #69

Merged
merged 3 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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