github.com/cloudwego/hertz@v0.9.3/pkg/common/hlog/default_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package hlog
    18  
    19  import (
    20  	"context"
    21  	"log"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    26  )
    27  
    28  func initTestLogger() {
    29  	logger = &defaultLogger{
    30  		stdlog: log.New(os.Stderr, "", 0),
    31  		depth:  4,
    32  	}
    33  }
    34  
    35  type byteSliceWriter struct {
    36  	b []byte
    37  }
    38  
    39  func (w *byteSliceWriter) Write(p []byte) (int, error) {
    40  	w.b = append(w.b, p...)
    41  	return len(p), nil
    42  }
    43  
    44  func TestDefaultLogger(t *testing.T) {
    45  	initTestLogger()
    46  
    47  	var w byteSliceWriter
    48  	SetOutput(&w)
    49  
    50  	Trace("trace work")
    51  	Debug("received work order")
    52  	Info("starting work")
    53  	Notice("something happens in work")
    54  	Warn("work may fail")
    55  	Error("work failed")
    56  
    57  	assert.DeepEqual(t, "[Trace] trace work\n"+
    58  		"[Debug] received work order\n"+
    59  		"[Info] starting work\n"+
    60  		"[Notice] something happens in work\n"+
    61  		"[Warn] work may fail\n"+
    62  		"[Error] work failed\n", string(w.b))
    63  }
    64  
    65  func TestDefaultFormatLogger(t *testing.T) {
    66  	initTestLogger()
    67  
    68  	var w byteSliceWriter
    69  	SetOutput(&w)
    70  
    71  	work := "work"
    72  	Tracef("trace %s", work)
    73  	Debugf("received %s order", work)
    74  	Infof("starting %s", work)
    75  	Noticef("something happens in %s", work)
    76  	Warnf("%s may fail", work)
    77  	Errorf("%s failed", work)
    78  
    79  	assert.DeepEqual(t, "[Trace] trace work\n"+
    80  		"[Debug] received work order\n"+
    81  		"[Info] starting work\n"+
    82  		"[Notice] something happens in work\n"+
    83  		"[Warn] work may fail\n"+
    84  		"[Error] work failed\n", string(w.b))
    85  }
    86  
    87  func TestCtxLogger(t *testing.T) {
    88  	initTestLogger()
    89  
    90  	var w byteSliceWriter
    91  	SetOutput(&w)
    92  
    93  	ctx := context.Background()
    94  	work := "work"
    95  	CtxTracef(ctx, "trace %s", work)
    96  	CtxDebugf(ctx, "received %s order", work)
    97  	CtxInfof(ctx, "starting %s", work)
    98  	CtxNoticef(ctx, "something happens in %s", work)
    99  	CtxWarnf(ctx, "%s may fail", work)
   100  	CtxErrorf(ctx, "%s failed", work)
   101  
   102  	assert.DeepEqual(t, "[Trace] trace work\n"+
   103  		"[Debug] received work order\n"+
   104  		"[Info] starting work\n"+
   105  		"[Notice] something happens in work\n"+
   106  		"[Warn] work may fail\n"+
   107  		"[Error] work failed\n", string(w.b))
   108  }
   109  
   110  func TestFormatLoggerWithEscapedCharacters(t *testing.T) {
   111  	initTestLogger()
   112  
   113  	var w byteSliceWriter
   114  	SetOutput(&w)
   115  
   116  	Infof("http://localhost:8080/ping?f=http://localhost:3000/hello?c=%E5%A4%A7hi%E5%93%A6%E5%95%8A%E8%AF%B4%E5%BE%97%E5%A5%BD")
   117  	assert.DeepEqual(t, "[Info] http://localhost:8080/ping?f=http://localhost:3000/hello?c=%E5%A4%A7hi%E5%93%A6%E5%95%8A%E8%AF%B4%E5%BE%97%E5%A5%BD\n", string(w.b))
   118  }
   119  
   120  func TestSetLevel(t *testing.T) {
   121  	setLogger := &defaultLogger{
   122  		stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
   123  		depth:  4,
   124  	}
   125  
   126  	setLogger.SetLevel(LevelTrace)
   127  	assert.DeepEqual(t, LevelTrace, setLogger.level)
   128  	assert.DeepEqual(t, LevelTrace.toString(), setLogger.level.toString())
   129  
   130  	setLogger.SetLevel(LevelDebug)
   131  	assert.DeepEqual(t, LevelDebug, setLogger.level)
   132  	assert.DeepEqual(t, LevelDebug.toString(), setLogger.level.toString())
   133  
   134  	setLogger.SetLevel(LevelInfo)
   135  	assert.DeepEqual(t, LevelInfo, setLogger.level)
   136  	assert.DeepEqual(t, LevelInfo.toString(), setLogger.level.toString())
   137  
   138  	setLogger.SetLevel(LevelNotice)
   139  	assert.DeepEqual(t, LevelNotice, setLogger.level)
   140  	assert.DeepEqual(t, LevelNotice.toString(), setLogger.level.toString())
   141  
   142  	setLogger.SetLevel(LevelWarn)
   143  	assert.DeepEqual(t, LevelWarn, setLogger.level)
   144  	assert.DeepEqual(t, LevelWarn.toString(), setLogger.level.toString())
   145  
   146  	setLogger.SetLevel(LevelError)
   147  	assert.DeepEqual(t, LevelError, setLogger.level)
   148  	assert.DeepEqual(t, LevelError.toString(), setLogger.level.toString())
   149  
   150  	setLogger.SetLevel(LevelFatal)
   151  	assert.DeepEqual(t, LevelFatal, setLogger.level)
   152  	assert.DeepEqual(t, LevelFatal.toString(), setLogger.level.toString())
   153  
   154  	setLogger.SetLevel(7)
   155  	assert.DeepEqual(t, 7, int(setLogger.level))
   156  	assert.DeepEqual(t, "[?7] ", setLogger.level.toString())
   157  }