github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/countlog_test.go (about)

     1  package countlog
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/v2pro/plz/countlog/output"
    12  	"github.com/v2pro/plz/countlog/output/async"
    13  	"github.com/v2pro/plz/countlog/output/compact"
    14  	"github.com/v2pro/plz/countlog/output/json"
    15  	"github.com/v2pro/plz/countlog/output/lumberjack"
    16  	"github.com/v2pro/plz/countlog/spi"
    17  )
    18  
    19  func Test_trace(t *testing.T) {
    20  	EventWriter = output.NewEventWriter(output.EventWriterConfig{
    21  		Format: &json.JsonFormat{},
    22  	})
    23  	Trace("hello", "a", "b", "int", 100)
    24  }
    25  
    26  func Test_trace_call(t *testing.T) {
    27  	should := require.New(t)
    28  	err := DebugCall("call func with {k1}", errors.New("failure"),
    29  		"k1", "v1")
    30  	should.Equal("call func with v1: failure", err.Error())
    31  }
    32  
    33  func Test_call_with_same_event_but_different_properties(t *testing.T) {
    34  	ctx := Ctx(context.Background())
    35  	for i := 0; i < 3; i++ {
    36  		ctx.Trace("same event name", "key", 100)
    37  		Trace("same event name", "key", "value")
    38  	}
    39  }
    40  
    41  func Test_log_file(t *testing.T) {
    42  	should := require.New(t)
    43  	logFile, err := os.OpenFile("/tmp/test.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    44  	should.NoError(err)
    45  	defer logFile.Close()
    46  	EventWriter = output.NewEventWriter(output.EventWriterConfig{
    47  		Format: &compact.Format{},
    48  		Writer: async.NewAsyncWriter(async.AsyncWriterConfig{
    49  			QueueLength:     1024,
    50  			IsQueueBlocking: false,
    51  			Writer:          logFile,
    52  		}),
    53  	})
    54  	for i := 0; i < 1000; i++ {
    55  		Info("something happened", "input", "abc", "output", "def")
    56  	}
    57  	time.Sleep(time.Second)
    58  }
    59  
    60  func Test_rolling_log_file(t *testing.T) {
    61  	logFile := &lumberjack.Logger{
    62  		BackupTimeFormat: "2006-01-02T15-04-05.000",
    63  		Filename:         "/tmp/test.log",
    64  		MaxSize:          1, // megabytes
    65  		MaxBackups:       3,
    66  	}
    67  	defer logFile.Close()
    68  	EventWriter = output.NewEventWriter(output.EventWriterConfig{
    69  		Format: &compact.Format{},
    70  		Writer: logFile,
    71  	})
    72  	for i := 0; i < 10000; i++ {
    73  		Info("something  happened", "input", "abc", "output", "def")
    74  	}
    75  }
    76  
    77  func Test_different_file(t *testing.T) {
    78  	should := require.New(t)
    79  	infoLogFile, err := os.OpenFile("/tmp/test.info.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    80  	should.NoError(err)
    81  	defer infoLogFile.Close()
    82  	infoWriter := output.NewEventWriter(output.EventWriterConfig{
    83  		Format: &compact.Format{},
    84  		Writer: infoLogFile,
    85  	})
    86  	errorLogFile, err := os.OpenFile("/tmp/test.error.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    87  	should.NoError(err)
    88  	defer errorLogFile.Close()
    89  	errorWriter := output.NewEventWriter(output.EventWriterConfig{
    90  		Format: &compact.Format{},
    91  		Writer: errorLogFile,
    92  	})
    93  	EventWriter = spi.FuncEventSink(func(site *spi.LogSite) spi.EventHandler {
    94  		infoHandler := infoWriter.HandlerOf(site)
    95  		errorHandler := errorWriter.HandlerOf(site)
    96  		return spi.FuncEventHandler(func(event *spi.Event) {
    97  			if event.Level > spi.LevelInfo {
    98  				errorHandler.Handle(event)
    99  			} else {
   100  				infoHandler.Handle(event)
   101  			}
   102  		})
   103  	})
   104  	Info("something  happened", "input", "abc", "output", "def")
   105  	Error("some error  happened", "input", "abc", "output", "def")
   106  }
   107  
   108  func Benchmark_trace(b *testing.B) {
   109  	SetMinLevel(LevelDebug)
   110  	b.ReportAllocs()
   111  	for i := 0; i < b.N; i++ {
   112  		Trace("trace without if check",
   113  			"k1", "v1",
   114  			"k2", "v2",
   115  			"k3", "v3")
   116  	}
   117  }