github.com/blend/go-sdk@v1.20220411.3/logger/maybe_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  	"bytes"
    12  	"context"
    13  	"fmt"
    14  	"testing"
    15  
    16  	"github.com/blend/go-sdk/assert"
    17  )
    18  
    19  func TestMaybeNilLogger(t *testing.T) {
    20  	MaybeInfof(nil, "")
    21  	MaybeInfofContext(context.TODO(), nil, "")
    22  	MaybeInfo(nil, "")
    23  	MaybeInfoContext(context.TODO(), nil, "")
    24  	MaybeDebugf(nil, "")
    25  	MaybeDebugfContext(context.TODO(), nil, "")
    26  	MaybeDebug(nil, "")
    27  	MaybeDebugContext(context.TODO(), nil, "")
    28  	MaybeWarningf(nil, "")
    29  	MaybeWarningfContext(context.TODO(), nil, "")
    30  	MaybeWarning(nil, nil)
    31  	MaybeWarningContext(context.TODO(), nil, nil)
    32  	MaybeErrorf(nil, "")
    33  	MaybeErrorfContext(context.TODO(), nil, "")
    34  	MaybeError(nil, nil)
    35  	MaybeErrorContext(context.TODO(), nil, nil)
    36  	MaybeFatalf(nil, "")
    37  	MaybeFatalfContext(context.TODO(), nil, "")
    38  	MaybeFatal(nil, nil)
    39  	MaybeFatalContext(context.TODO(), nil, nil)
    40  }
    41  
    42  func TestMaybeLogger(t *testing.T) {
    43  	assert := assert.New(t)
    44  
    45  	log := MustNew(OptAll())
    46  	log.Formatter = NewTextOutputFormatter(
    47  		OptTextNoColor(),
    48  		OptTextHideTimestamp(),
    49  	)
    50  
    51  	labelsCtx := func(key, value string) context.Context {
    52  		return WithLabels(context.Background(), Labels{key: value})
    53  	}
    54  
    55  	buf := new(bytes.Buffer)
    56  	log.Output = buf
    57  	MaybeInfof(log, "Infof")
    58  	assert.Equal("[info] Infof\n", buf.String())
    59  
    60  	buf = new(bytes.Buffer)
    61  	log.Output = buf
    62  	MaybeInfofContext(labelsCtx("a", "b"), log, "Infof")
    63  	assert.Equal("[info] Infof\ta=b\n", buf.String())
    64  
    65  	buf = new(bytes.Buffer)
    66  	log.Output = buf
    67  	MaybeInfo(log, "Info")
    68  	assert.Equal("[info] Info\n", buf.String())
    69  
    70  	buf = new(bytes.Buffer)
    71  	log.Output = buf
    72  	MaybeInfoContext(labelsCtx("a", "b"), log, "Info")
    73  	assert.Equal("[info] Info\ta=b\n", buf.String())
    74  
    75  	buf = new(bytes.Buffer)
    76  	log.Output = buf
    77  	MaybeDebugf(log, "Debugf")
    78  	assert.Equal("[debug] Debugf\n", buf.String())
    79  
    80  	buf = new(bytes.Buffer)
    81  	log.Output = buf
    82  	MaybeDebugfContext(labelsCtx("a", "b"), log, "Debugf")
    83  	assert.Equal("[debug] Debugf\ta=b\n", buf.String())
    84  
    85  	buf = new(bytes.Buffer)
    86  	log.Output = buf
    87  	MaybeDebug(log, "Debug")
    88  	assert.Equal("[debug] Debug\n", buf.String())
    89  
    90  	buf = new(bytes.Buffer)
    91  	log.Output = buf
    92  	MaybeDebugContext(labelsCtx("a", "b"), log, "Debug")
    93  	assert.Equal("[debug] Debug\ta=b\n", buf.String())
    94  
    95  	buf = new(bytes.Buffer)
    96  	log.Output = buf
    97  	MaybeWarningf(log, "Warningf")
    98  	assert.Equal("[warning] Warningf\n", buf.String())
    99  
   100  	buf = new(bytes.Buffer)
   101  	log.Output = buf
   102  	MaybeWarningfContext(labelsCtx("a", "b"), log, "Warningf")
   103  	assert.Equal("[warning] Warningf\ta=b\n", buf.String())
   104  
   105  	buf = new(bytes.Buffer)
   106  	log.Output = buf
   107  	MaybeWarning(log, fmt.Errorf("Warning"))
   108  	assert.Equal("[warning] Warning\n", buf.String())
   109  
   110  	buf = new(bytes.Buffer)
   111  	log.Output = buf
   112  	MaybeWarningContext(labelsCtx("a", "b"), log, fmt.Errorf("Warning"))
   113  	assert.Equal("[warning] Warning\ta=b\n", buf.String())
   114  
   115  	buf = new(bytes.Buffer)
   116  	log.Output = buf
   117  	MaybeErrorf(log, "Errorf")
   118  	assert.Equal("[error] Errorf\n", buf.String())
   119  
   120  	buf = new(bytes.Buffer)
   121  	log.Output = buf
   122  	MaybeErrorfContext(labelsCtx("a", "b"), log, "Errorf")
   123  	assert.Equal("[error] Errorf\ta=b\n", buf.String())
   124  
   125  	buf = new(bytes.Buffer)
   126  	log.Output = buf
   127  	MaybeError(log, fmt.Errorf("Error"))
   128  	assert.Equal("[error] Error\n", buf.String())
   129  
   130  	buf = new(bytes.Buffer)
   131  	log.Output = buf
   132  	MaybeErrorContext(labelsCtx("a", "b"), log, fmt.Errorf("Error"))
   133  	assert.Equal("[error] Error\ta=b\n", buf.String())
   134  
   135  	buf = new(bytes.Buffer)
   136  	log.Output = buf
   137  	MaybeFatalf(log, "Fatalf")
   138  	assert.Equal("[fatal] Fatalf\n", buf.String())
   139  
   140  	buf = new(bytes.Buffer)
   141  	log.Output = buf
   142  	MaybeFatalfContext(labelsCtx("a", "b"), log, "Fatalf")
   143  	assert.Equal("[fatal] Fatalf\ta=b\n", buf.String())
   144  
   145  	buf = new(bytes.Buffer)
   146  	log.Output = buf
   147  	MaybeFatal(log, fmt.Errorf("Fatal"))
   148  	assert.Equal("[fatal] Fatal\n", buf.String())
   149  
   150  	buf = new(bytes.Buffer)
   151  	log.Output = buf
   152  	MaybeFatalContext(labelsCtx("a", "b"), log, fmt.Errorf("Fatal"))
   153  	assert.Equal("[fatal] Fatal\ta=b\n", buf.String())
   154  }