...

Source file src/go.opentelemetry.io/otel/internal/global/internal_logging_test.go

Documentation: go.opentelemetry.io/otel/internal/global

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package global
    16  
    17  import (
    18  	"bytes"
    19  	"errors"
    20  	"io"
    21  	"log"
    22  	"sync"
    23  	"testing"
    24  
    25  	"github.com/go-logr/logr"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  
    29  	"github.com/go-logr/logr/funcr"
    30  	"github.com/go-logr/stdr"
    31  )
    32  
    33  func TestLoggerConcurrentSafe(t *testing.T) {
    34  	var wg sync.WaitGroup
    35  	wg.Add(1)
    36  	go func() {
    37  		defer wg.Done()
    38  		SetLogger(stdr.New(log.New(io.Discard, "", 0)))
    39  	}()
    40  	wg.Add(1)
    41  	go func() {
    42  		defer wg.Done()
    43  		Info("")
    44  	}()
    45  
    46  	wg.Wait()
    47  	reset()
    48  }
    49  
    50  func TestLogLevel(t *testing.T) {
    51  	tests := []struct {
    52  		name      string
    53  		verbosity int
    54  		logF      func()
    55  		want      string
    56  	}{
    57  		{
    58  			name:      "Verbosity 0 should log errors.",
    59  			verbosity: 0,
    60  			want:      `"msg"="foobar" "error"="foobar"`,
    61  			logF: func() {
    62  				Error(errors.New("foobar"), "foobar")
    63  			},
    64  		},
    65  		{
    66  			name:      "Verbosity 1 should log warnings",
    67  			verbosity: 1,
    68  			want:      `"level"=1 "msg"="foo"`,
    69  			logF: func() {
    70  				Warn("foo")
    71  			},
    72  		},
    73  		{
    74  			name:      "Verbosity 4 should log info",
    75  			verbosity: 4,
    76  			want:      `"level"=4 "msg"="bar"`,
    77  			logF: func() {
    78  				Info("bar")
    79  			},
    80  		},
    81  		{
    82  			name:      "Verbosity 8 should log debug",
    83  			verbosity: 8,
    84  			want:      `"level"=8 "msg"="baz"`,
    85  			logF: func() {
    86  				Debug("baz")
    87  			},
    88  		},
    89  	}
    90  
    91  	for _, test := range tests {
    92  		t.Run(test.name, func(t *testing.T) {
    93  			var buf bytes.Buffer
    94  			SetLogger(newBuffLogger(&buf, test.verbosity))
    95  
    96  			test.logF()
    97  
    98  			assert.Equal(t, test.want, buf.String())
    99  		})
   100  	}
   101  }
   102  
   103  func newBuffLogger(buf *bytes.Buffer, verbosity int) logr.Logger {
   104  	return funcr.New(func(prefix, args string) {
   105  		_, _ = buf.Write([]byte(args))
   106  	}, funcr.Options{
   107  		Verbosity: verbosity,
   108  	})
   109  }
   110  

View as plain text