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

Fixed the default log level always info issue #8

Merged
merged 2 commits into from
Feb 22, 2024
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
12 changes: 8 additions & 4 deletions collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ func main() {
func initLogger() *zap.Logger {
lvl := zap.NewAtomicLevelAt(zapcore.WarnLevel)

envLvl := os.Getenv("OPENTELEMETRY_EXTENSION_LOG_LEVEL")
userLvl, err := zap.ParseAtomicLevel(envLvl)
if err == nil {
lvl = userLvl
var err error
envLvl, ok := os.LookupEnv("OPENTELEMETRY_EXTENSION_LOG_LEVEL")
if ok {
var userLvl zap.AtomicLevel
userLvl, err = zap.ParseAtomicLevel(envLvl)
if err == nil {
lvl = userLvl
}
}

l := zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), os.Stdout, lvl))
Expand Down
21 changes: 21 additions & 0 deletions collector/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap/zapcore"
)

func TestInitLogger(t *testing.T) {
logger := initLogger()
assert.Equal(t, zapcore.WarnLevel, logger.Level())
}

func TestInitLogger_WithEnv(t *testing.T) {
os.Setenv("OPENTELEMETRY_EXTENSION_LOG_LEVEL", "debug")
logger := initLogger()
assert.Equal(t, zapcore.DebugLevel, logger.Level())
os.Unsetenv("OPENTELEMETRY_EXTENSION_LOG_LEVEL")
}