github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/zlog/multi_files_test.go (about)

     1  package zlog
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestMultiFilesCore(t *testing.T) {
    12  	getTempFilename := func() string {
    13  		tmpFilenamePattern := "zlog-test-multi-files-core-*.log"
    14  		f1, err := os.CreateTemp("", tmpFilenamePattern)
    15  		require.Nil(t, err)
    16  		f1Name := f1.Name()
    17  		f1.Close()
    18  		return f1Name
    19  	}
    20  
    21  	removeFiles := func(filenames ...string) {
    22  		for _, fn := range filenames {
    23  			os.Remove(fn)
    24  		}
    25  	}
    26  
    27  	f1 := getTempFilename()
    28  	f2 := getTempFilename()
    29  	f3 := getTempFilename()
    30  	defer removeFiles(f1, f2, f3)
    31  
    32  	cfg := &Config{
    33  		File: FileConfig{
    34  			Filename:   f1,
    35  			MaxSize:    100,
    36  			MaxDays:    3,
    37  			MaxBackups: 0,
    38  			Compress:   false,
    39  		},
    40  		PerLoggerFiles: map[string]FileConfig{
    41  			"access": {Filename: f2},
    42  			"pkg2":   {Filename: f3},
    43  		},
    44  	}
    45  
    46  	logger, props, err := New(cfg)
    47  	require.Nil(t, err)
    48  	defer props.CloseWriters()
    49  
    50  	logger.Info("from default logger")
    51  
    52  	lg1 := logger.Named("access")
    53  	lg1.Info("from access logger")
    54  	lg1.Named("sub.logger1").Info("from access.sub.logger1 logger")
    55  	logger.Named("access.sub.logger2").Info("from access.sub.logger2 logger")
    56  
    57  	lg2 := logger.Named("pkg2")
    58  	lg2.Info("from pkg2 logger")
    59  	lg2.Named("sub").Info("from pkg2.sub logger")
    60  
    61  	lg3 := logger.Named("pkg3")
    62  	lg3.Info("from pkg3 logger")
    63  	lg3.Named("sub").Info("from pkg3.sub logger")
    64  
    65  	readFile := func(filename string) string {
    66  		data, err := os.ReadFile(filename)
    67  		require.Nil(t, err)
    68  		return string(data)
    69  	}
    70  
    71  	got1 := readFile(f1)
    72  	assert.Contains(t, got1, "from default logger")
    73  	assert.Contains(t, got1, "from pkg3 logger")
    74  	assert.Contains(t, got1, "from pkg3.sub logger")
    75  	assert.NotContains(t, got1, "from access")
    76  	assert.NotContains(t, got1, "from pkg2")
    77  
    78  	got2 := readFile(f2)
    79  	assert.NotContains(t, got2, "from default")
    80  	assert.NotContains(t, got2, "from pkg2")
    81  	assert.NotContains(t, got2, "from pkg3")
    82  	assert.Contains(t, got2, "from access logger")
    83  	assert.Contains(t, got2, "from access.sub.logger1 logger")
    84  	assert.Contains(t, got2, "from access.sub.logger2 logger")
    85  
    86  	got3 := readFile(f3)
    87  	assert.NotContains(t, got3, "from default")
    88  	assert.NotContains(t, got3, "from access")
    89  	assert.NotContains(t, got3, "from pkg3")
    90  	assert.Contains(t, got3, "from pkg2 logger")
    91  	assert.Contains(t, got3, "from pkg2.sub logger")
    92  }