github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/logger/flag_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/cihub/seelog"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestOverrideLevel(t *testing.T) {
    27  	mu.Lock()
    28  	defer mu.Unlock()
    29  	excludeFlag()
    30  	clean()
    31  	flag.Set(FlagLevelName, seelog.DebugStr)
    32  	initTestLogger()
    33  	Debugf(context.Background(), "%s", "hello")
    34  	assert.True(t, readLog(0) != "")
    35  	clean()
    36  	flag.Set(FlagLevelName, "xxx")
    37  	initTestLogger()
    38  	Debugf(context.Background(), "%s", "hello")
    39  	assert.True(t, readLog(0) == "")
    40  }
    41  
    42  func TestOverrideConsole(t *testing.T) {
    43  	mu.Lock()
    44  	defer mu.Unlock()
    45  	excludeFlag()
    46  	clean()
    47  	flag.Set(FlagConsoleName, "true")
    48  	initTestLogger(OptionOffConsole)
    49  	Infof(context.Background(), "%s", "hello")
    50  	clean()
    51  	flag.Set(FlagConsoleName, "xxx")
    52  	initTestLogger()
    53  	initTestLogger(OptionOffConsole)
    54  	Infof(context.Background(), "%s", "hello")
    55  }
    56  
    57  func TestOverrideRetain(t *testing.T) {
    58  	mu.Lock()
    59  	defer mu.Unlock()
    60  	excludeFlag()
    61  	clean()
    62  	initTestLogger(OptionDebugLevel)
    63  	Debugf(context.Background(), "%s", "hello")
    64  	assert.True(t, readLog(0) != "")
    65  	flag.Set(FlagRetainName, "true")
    66  	initTestLogger()
    67  	Debugf(context.Background(), "%s", "hello")
    68  	assert.True(t, readLog(1) != "")
    69  }