github.com/blend/go-sdk@v1.20220411.3/logger/config_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package logger
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  	"github.com/blend/go-sdk/env"
    17  )
    18  
    19  func TestConfig(t *testing.T) {
    20  	assert := assert.New(t)
    21  
    22  	var cfg Config
    23  	assert.Equal(DefaultFlags, cfg.FlagsOrDefault())
    24  	assert.Equal(FormatText, cfg.FormatOrDefault())
    25  	_, ok := cfg.Formatter().(*TextOutputFormatter)
    26  	assert.True(ok)
    27  
    28  	cfg = Config{
    29  		Flags:  []string{Info, Error},
    30  		Format: FormatJSON,
    31  	}
    32  
    33  	assert.Equal([]string{Info, Error}, cfg.FlagsOrDefault())
    34  	assert.Equal(FormatJSON, cfg.FormatOrDefault())
    35  }
    36  
    37  func TestConfigResolve(t *testing.T) {
    38  	assert := assert.New(t)
    39  
    40  	defer env.Restore()
    41  	env.Env().Set("LOG_FLAGS", "foo,bar")
    42  	env.Env().Set("LOG_HIDE_TIMESTAMP", "true")
    43  	env.Env().Set("LOG_HIDE_FIELDS", "true")
    44  	env.Env().Set("LOG_TIME_FORMAT", time.Kitchen)
    45  	env.Env().Set("NO_COLOR", "true")
    46  
    47  	cfg := &Config{}
    48  	ctx := env.WithVars(context.Background(), env.Env())
    49  	assert.Nil(cfg.Resolve(ctx))
    50  
    51  	assert.Any(cfg.Flags, func(v interface{}) bool { return v.(string) == "foo" })
    52  	assert.Any(cfg.Flags, func(v interface{}) bool { return v.(string) == "bar" })
    53  	assert.True(cfg.Text.HideTimestamp)
    54  	assert.True(cfg.Text.HideFields)
    55  	assert.True(cfg.Text.NoColor)
    56  	assert.Equal(time.Kitchen, cfg.Text.TimeFormat)
    57  }