Skip to content

Commit

Permalink
Add a unit test case for logrus-entry-contents
Browse files Browse the repository at this point in the history
Signed-off-by: yanggang <[email protected]>
  • Loading branch information
yanggang committed Nov 1, 2023
1 parent 705a3bc commit 04233e0
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pkg/util/logging/dual_mode_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package logging

import (
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"strings"
Expand Down Expand Up @@ -59,6 +61,73 @@ func TestDualModeLogger(t *testing.T) {
assert.Equal(t, true, os.IsNotExist(err))
}

func TestDualModeLoggerEntryHook(t *testing.T) {
logCounter := NewLogHook()
logger, err := NewTempFileLogger(
logrus.WarnLevel,
FormatText,
logCounter,
//logrus.Fields{"namespace": "ns1", "resource": "pod", "name": "podxxx"})
logrus.Fields{"namespace": "ns1", "resource": "podxxxx"})
require.NoError(t, err)

logger.Warnln("test 1")
logger.Warnln("test 2")
logger.Warnln("test 3")

logger.WithFields(logrus.Fields{"namespace": "ns2", "resource": "pod", "name": "pod1"})
logger.Warnln("test 1 with field ns2")
logger.WithFields(logrus.Fields{"namespace": "ns2", "resource": "pod", "name": "pod2"})
logger.Warnln("test 2 with field ns2")
logger.WithFields(logrus.Fields{"namespace": "ns2", "resource": "pod", "name": "pod3"})
logger.Warnln("test 3 with field ns2")

logger.WithFields(logrus.Fields{"namespace": "ns3", "resource": "pod", "name": "pod4"})
logger.Warnln("test 4 with field ns3")
logger.WithFields(logrus.Fields{"namespace": "ns3", "resource": "pod", "name": "pod5"})
logger.Warnln("test 5 with field ns3")
logger.WithFields(logrus.Fields{"namespace": "ns3", "resource": "pod", "name": "pod6"})
logger.Warnln("test 6 with field ns3")

logger.WithError(errors.New("found err 1")).WithFields(
logrus.Fields{"namespace": "ns4", "resource": "pod", "name": "pod7"})
logger.Warnln("test 7 with field ns4 and WithError")
logger.WithError(errors.New("found err 2")).WithFields(
logrus.Fields{"namespace": "ns4", "resource": "pod", "name": "pod8"})
logger.Warnln("test 8 with field ns4 and WithError")

logger.Errorln("test error 1 in pod 1")
logger.Errorln("test error 2 in pod 2")
logger.Errorln("test error 3 in pod 3")
logger.Errorln("test error 4 in pod 4")

logger.DoneForPersist(velerotest.NewLogger())

logCntForWarn := logCounter.GetCount(logrus.WarnLevel)
fmt.Printf("logCntForWarn=%v\n", logCntForWarn)

logCntForError := logCounter.GetCount(logrus.ErrorLevel)
fmt.Printf("logCntForError=%v\n", logCntForError)

logResultForWarn := logCounter.GetEntries(logrus.WarnLevel)
for key, value := range logResultForWarn.Namespaces {
fmt.Printf("logResultForWarn namespace key=%+v , value=%+v\n", key, value)
}

logResultForError := logCounter.GetEntries(logrus.WarnLevel)
for key, value := range logResultForError.Namespaces {
fmt.Printf("logResultForError namespace key=%+v , value=%+v\n", key, value)
}

logFile, err := logger.GetPersistFile()
require.NoError(t, err)

logger.Dispose(velerotest.NewLogger())

_, err = os.Stat(logFile.Name())
assert.Equal(t, true, os.IsNotExist(err))
}

func readLogString(file *os.File) (string, error) {
gzr, err := gzip.NewReader(file)
if err != nil {
Expand Down

0 comments on commit 04233e0

Please sign in to comment.