github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekalog/logger_test.go (about)

     1  // Copyright © 2020-2021. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekalog_test
     7  
     8  import (
     9  	"bytes"
    10  	"fmt"
    11  	"io/ioutil"
    12  	"strings"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/qioalice/ekago/v3/ekadeath"
    17  	"github.com/qioalice/ekago/v3/ekaerr"
    18  	"github.com/qioalice/ekago/v3/ekalog"
    19  )
    20  
    21  func foo(isLightWeight bool) *ekaerr.Error {
    22  
    23  	gen := ekaerr.Interrupted.New
    24  	if isLightWeight {
    25  		gen = ekaerr.Interrupted.LightNew
    26  	}
    27  
    28  	return gen("fwefwf").
    29  		AddMessage("custom message").
    30  		WithInt("test", 42).
    31  		Throw()
    32  }
    33  
    34  func TestLog(t *testing.T) {
    35  
    36  	consoleEncoder := new(ekalog.CI_ConsoleEncoder)
    37  	b := bytes.NewBuffer(nil)
    38  
    39  	stdoutConsoleIntegrator := new(ekalog.CommonIntegrator).
    40  		WithEncoder(consoleEncoder).
    41  		WithMinLevel(ekalog.LEVEL_DEBUG).
    42  		WriteTo(b)
    43  
    44  	ekadeath.Reg(func() {
    45  		str := b.String()
    46  		//str = strings.ReplaceAll(str, "\033", "\\033")
    47  		_ = strings.ReplaceAll
    48  		fmt.Println(str)
    49  	})
    50  
    51  	ekalog.ReplaceIntegrator(stdoutConsoleIntegrator)
    52  
    53  	ekalog.Warne("", ekaerr.Interrupted.New("test"), "key", "value")
    54  
    55  	ekalog.Debug("test %s %d", "hello", 25, "arg", 42)
    56  
    57  	ekalog.Debug("test", "field1", 42, "field2", nil)
    58  	ekalog.Info("test", "dur", time.Minute*20+time.Second*12, "i64", int64(3234234))
    59  	ekalog.Warn("test", "time", time.Now())
    60  	ekalog.Error("test", "sys.this_field_is_ignored", 0)
    61  
    62  	ekalog.Alerte("", foo(true), "log_field")
    63  	ekalog.Emerge("emerg", foo(false), "log_field")
    64  }
    65  
    66  func BenchmarkLog(b *testing.B) {
    67  	b.ReportAllocs()
    68  
    69  	devNullIntegrator := new(ekalog.CommonIntegrator).
    70  		WithEncoder(new(ekalog.CI_ConsoleEncoder)).
    71  		WithMinLevel(ekalog.LEVEL_DEBUG).
    72  		WriteTo(ioutil.Discard)
    73  
    74  	ekalog.ReplaceIntegrator(devNullIntegrator)
    75  
    76  	b.Run("Log", func(b *testing.B) {
    77  		for i := 0; i < b.N; i++ {
    78  			ekalog.Debugw("test")
    79  			//ekalog.Debug("test", "field", 41, "field2", nil)
    80  		}
    81  	})
    82  
    83  	var eps = ekalog.EPS()
    84  	fmt.Printf("%+v\n", eps)
    85  }