github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/logger/option_test.go (about)

     1  // Copyright 2021 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logger
    16  
    17  import (
    18  	"context"
    19  	"flag"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func excludeFlag() {
    26  	flag.Set(FlagLevelName, "xxx")
    27  	flag.Set(FlagConsoleName, "xxx")
    28  	flag.Set(FlagRetainName, "xxx")
    29  }
    30  
    31  func TestOptionDebugLevel(t *testing.T) {
    32  	mu.Lock()
    33  	defer mu.Unlock()
    34  	excludeFlag()
    35  	clean()
    36  	initTestLogger(OptionDebugLevel, OptionOffConsole)
    37  	Debug(context.Background(), "line0")
    38  	assert.True(t, readLog(0) != "")
    39  
    40  }
    41  
    42  func TestOptionInfoLevel(t *testing.T) {
    43  	mu.Lock()
    44  	defer mu.Unlock()
    45  	excludeFlag()
    46  	clean()
    47  	initTestLogger(OptionInfoLevel)
    48  	Debug(context.Background(), "line")
    49  	assert.True(t, readLog(0) == "", "read %s", readLog(0))
    50  	Info(context.Background(), "line")
    51  	assert.True(t, readLog(0) != "")
    52  }
    53  
    54  func TestOptionWarnLevel(t *testing.T) {
    55  	mu.Lock()
    56  	defer mu.Unlock()
    57  	excludeFlag()
    58  	clean()
    59  	initTestLogger(OptionWarnLevel)
    60  	Info(context.Background(), "line")
    61  	assert.True(t, readLog(0) == "")
    62  	Warning(context.Background(), "ALARM", "line")
    63  	assert.True(t, readLog(0) != "")
    64  }
    65  
    66  func TestOptionErrorLevel(t *testing.T) {
    67  	mu.Lock()
    68  	defer mu.Unlock()
    69  	excludeFlag()
    70  	clean()
    71  	initTestLogger(OptionErrorLevel)
    72  	Warning(context.Background(), "line")
    73  	assert.True(t, readLog(0) == "")
    74  	Error(context.Background(), "ALARM", "line")
    75  	assert.True(t, readLog(0) != "")
    76  }
    77  
    78  func TestOptionConsole(t *testing.T) {
    79  	mu.Lock()
    80  	defer mu.Unlock()
    81  	excludeFlag()
    82  	clean()
    83  	initTestLogger()
    84  	Info(context.Background(), "hello")
    85  	initTestLogger(OptionOffConsole)
    86  	Info(context.Background(), "hello2")
    87  }
    88  
    89  func TestOptionConfigRetain(t *testing.T) {
    90  	mu.Lock()
    91  	defer mu.Unlock()
    92  	excludeFlag()
    93  	clean()
    94  	initTestLogger(OptionDebugLevel, OptionOffConsole)
    95  	Debug(context.Background(), "line0")
    96  	assert.True(t, readLog(0) != "")
    97  	initTestLogger(OptionRetainConfig)
    98  	Debug(context.Background(), "line1")
    99  	assert.True(t, readLog(1) != "")
   100  }
   101  
   102  func TestOptionAsync(t *testing.T) {
   103  	mu.Lock()
   104  	defer mu.Unlock()
   105  	excludeFlag()
   106  	clean()
   107  	initTestLogger(OptionOffConsole)
   108  	Info(context.Background(), "line0")
   109  	assert.True(t, readLog(0) != "")
   110  	initTestLogger(OptionOffConsole, OptionAsyncLogger)
   111  	Info(context.Background(), "line1")
   112  	assert.True(t, readLog(1) == "")
   113  	Flush()
   114  	assert.True(t, readLog(1) != "")
   115  }
   116  
   117  func TestOpenMemoryReceiver(t *testing.T) {
   118  	mu.Lock()
   119  	defer mu.Unlock()
   120  	excludeFlag()
   121  	clean()
   122  	initTestLogger(OptionOpenMemoryReceiver)
   123  	Info(context.Background(), "a")
   124  	_, ok := ReadMemoryLog(1)
   125  	assert.True(t, ok)
   126  }