trpc.group/trpc-go/trpc-go@v1.0.3/log/config_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package log_test
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	"trpc.group/trpc-go/trpc-go/log"
    21  )
    22  
    23  var defaultConfig = []log.OutputConfig{
    24  	{
    25  		Writer:    "console",
    26  		Level:     "debug",
    27  		Formatter: "console",
    28  		FormatConfig: log.FormatConfig{
    29  			TimeFmt: "2006.01.02 15:04:05",
    30  		},
    31  	},
    32  	{
    33  		Writer:    "file",
    34  		Level:     "info",
    35  		Formatter: "json",
    36  		WriteConfig: log.WriteConfig{
    37  			Filename:   "trpc_size.log",
    38  			RollType:   "size",
    39  			MaxAge:     7,
    40  			MaxBackups: 10,
    41  			MaxSize:    100,
    42  		},
    43  		FormatConfig: log.FormatConfig{
    44  			TimeFmt: "2006.01.02 15:04:05",
    45  		},
    46  	},
    47  	{
    48  		Writer:    "file",
    49  		Level:     "info",
    50  		Formatter: "json",
    51  		WriteConfig: log.WriteConfig{
    52  			Filename:   "trpc_time.log",
    53  			RollType:   "time",
    54  			MaxAge:     7,
    55  			MaxBackups: 10,
    56  			MaxSize:    100,
    57  			TimeUnit:   log.Day,
    58  		},
    59  		FormatConfig: log.FormatConfig{
    60  			TimeFmt: "2006-01-02 15:04:05",
    61  		},
    62  	},
    63  }
    64  
    65  func TestTimeUnit_Format(t *testing.T) {
    66  	tests := []struct {
    67  		name string
    68  		tr   log.TimeUnit
    69  		want string
    70  	}{
    71  		{"Minute", log.Minute, ".%Y%m%d%H%M"},
    72  		{"Hour", log.Hour, ".%Y%m%d%H"},
    73  		{"Day", log.Day, ".%Y%m%d"},
    74  		{"Month", log.Month, ".%Y%m"},
    75  		{"Year", log.Year, ".%Y"},
    76  		{"default", log.TimeUnit("xxx"), ".%Y%m%d"},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			if got := tt.tr.Format(); got != tt.want {
    81  				t.Errorf("TimeUnit.Format() = %v, want %v", got, tt.want)
    82  			}
    83  		})
    84  	}
    85  }
    86  
    87  func TestTimeUnit_RotationGap(t *testing.T) {
    88  	tests := []struct {
    89  		name string
    90  		tr   log.TimeUnit
    91  		want time.Duration
    92  	}{
    93  		{"Minute", log.Minute, time.Minute},
    94  		{"Hour", log.Hour, time.Hour},
    95  		{"Day", log.Day, time.Hour * 24},
    96  		{"Month", log.Month, time.Hour * 24 * 30},
    97  		{"Year", log.Year, time.Hour * 24 * 365},
    98  		{"default", log.TimeUnit("xxx"), time.Hour * 24},
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			if got := tt.tr.RotationGap(); got != tt.want {
   103  				t.Errorf("TimeUnit.RotationGap() = %v, want %v", got, tt.want)
   104  			}
   105  		})
   106  	}
   107  }