github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/log/log_test.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package log
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/likexian/gokit/assert"
    24  	"github.com/sirupsen/logrus"
    25  )
    26  
    27  func TestLogContext(t *testing.T) {
    28  	ctx := WithLogField(context.Background(), "myfield", "myvalue")
    29  	assert.Equal(t, "myvalue", L(ctx).Data["myfield"])
    30  }
    31  
    32  func TestLogContextLimited(t *testing.T) {
    33  	ctx := WithLogField(context.Background(), "myfield", "0123456789012345678901234567890123456789012345678901234567890123456789")
    34  	assert.Equal(t, "0123456789012345678901234567890123456789012345678901234567890...", L(ctx).Data["myfield"])
    35  }
    36  
    37  func TestSettingErrorLevel(t *testing.T) {
    38  	SetLevel("eRrOr")
    39  	assert.Equal(t, logrus.ErrorLevel, logrus.GetLevel())
    40  }
    41  
    42  func TestSettingDebugLevel(t *testing.T) {
    43  	SetLevel("DEBUG")
    44  	assert.Equal(t, logrus.DebugLevel, logrus.GetLevel())
    45  }
    46  
    47  func TestSettingTraceLevel(t *testing.T) {
    48  	SetLevel("trace")
    49  	assert.Equal(t, logrus.TraceLevel, logrus.GetLevel())
    50  }
    51  
    52  func TestSettingInfoLevel(t *testing.T) {
    53  	SetLevel("info")
    54  	assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
    55  }
    56  
    57  func TestSettingDefaultLevel(t *testing.T) {
    58  	SetLevel("something else")
    59  	assert.Equal(t, logrus.InfoLevel, logrus.GetLevel())
    60  }
    61  
    62  func TestSetFormatting(t *testing.T) {
    63  	SetFormatting(Formatting{
    64  		DisableColor: true,
    65  		UTC:          true,
    66  	})
    67  	L(context.Background()).Infof("time in UTC")
    68  }