github.com/cloudwego/hertz@v0.9.3/pkg/common/hlog/system_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 initTestSysLogger() { 29 sysLogger = &systemLogger{ 30 &defaultLogger{ 31 stdlog: log.New(os.Stderr, "", 0), 32 depth: 4, 33 }, 34 systemLogPrefix, 35 } 36 } 37 38 func TestSysLogger(t *testing.T) { 39 initTestSysLogger() 40 var w byteSliceWriter 41 SetOutput(&w) 42 43 sysLogger.Trace("trace work") 44 sysLogger.Debug("received work order") 45 sysLogger.Info("starting work") 46 sysLogger.Notice("something happens in work") 47 sysLogger.Warn("work may fail") 48 sysLogger.Error("work failed") 49 50 assert.DeepEqual(t, "[Trace] HERTZ: trace work\n"+ 51 "[Debug] HERTZ: received work order\n"+ 52 "[Info] HERTZ: starting work\n"+ 53 "[Notice] HERTZ: something happens in work\n"+ 54 "[Warn] HERTZ: work may fail\n"+ 55 "[Error] HERTZ: work failed\n", string(w.b)) 56 } 57 58 func TestSysFormatLogger(t *testing.T) { 59 initTestSysLogger() 60 var w byteSliceWriter 61 SetOutput(&w) 62 63 work := "work" 64 sysLogger.Tracef("trace %s", work) 65 sysLogger.Debugf("received %s order", work) 66 sysLogger.Infof("starting %s", work) 67 sysLogger.Noticef("something happens in %s", work) 68 sysLogger.Warnf("%s may fail", work) 69 sysLogger.Errorf("%s failed", work) 70 71 assert.DeepEqual(t, "[Trace] HERTZ: trace work\n"+ 72 "[Debug] HERTZ: received work order\n"+ 73 "[Info] HERTZ: starting work\n"+ 74 "[Notice] HERTZ: something happens in work\n"+ 75 "[Warn] HERTZ: work may fail\n"+ 76 "[Error] HERTZ: work failed\n", string(w.b)) 77 } 78 79 func TestSysCtxLogger(t *testing.T) { 80 initTestSysLogger() 81 var w byteSliceWriter 82 SetOutput(&w) 83 84 ctx := context.Background() 85 work := "work" 86 sysLogger.CtxTracef(ctx, "trace %s", work) 87 sysLogger.CtxDebugf(ctx, "received %s order", work) 88 sysLogger.CtxInfof(ctx, "starting %s", work) 89 sysLogger.CtxNoticef(ctx, "something happens in %s", work) 90 sysLogger.CtxWarnf(ctx, "%s may fail", work) 91 sysLogger.CtxErrorf(ctx, "%s failed", work) 92 93 assert.DeepEqual(t, "[Trace] HERTZ: trace work\n"+ 94 "[Debug] HERTZ: received work order\n"+ 95 "[Info] HERTZ: starting work\n"+ 96 "[Notice] HERTZ: something happens in work\n"+ 97 "[Warn] HERTZ: work may fail\n"+ 98 "[Error] HERTZ: work failed\n", string(w.b)) 99 }