github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/logger/option.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  	"github.com/cihub/seelog"
    19  
    20  	"strconv"
    21  )
    22  
    23  var defaultProductionOptions = []ConfigOption{
    24  	OptionOffConsole,
    25  	OptionAsyncLogger,
    26  	OptionInfoLevel,
    27  	OptionRetainConfig,
    28  	OptionOffMemoryReceiver,
    29  }
    30  
    31  var defaultTestOptions = []ConfigOption{
    32  	OptionOpenConsole,
    33  	OptionSyncLogger,
    34  	OptionInfoLevel,
    35  	OptionOverrideConfig,
    36  	OptionOffMemoryReceiver,
    37  }
    38  
    39  type ConfigOption func()
    40  
    41  func OptionOffConsole() {
    42  	changeConsoleFlag(false)
    43  
    44  }
    45  func OptionOpenConsole() {
    46  	changeConsoleFlag(true)
    47  }
    48  func OptionOffMemoryReceiver() {
    49  	memoryReceiverFlag = false
    50  }
    51  func OptionOpenMemoryReceiver() {
    52  	memoryReceiverFlag = true
    53  }
    54  func OptionSyncLogger() {
    55  	changeSyncFlag(true)
    56  }
    57  func OptionAsyncLogger() {
    58  	changeSyncFlag(false)
    59  }
    60  func OptionDebugLevel() {
    61  	changeLevelFlag(seelog.DebugStr)
    62  }
    63  func OptionInfoLevel() {
    64  	changeLevelFlag(seelog.InfoStr)
    65  }
    66  func OptionWarnLevel() {
    67  	changeLevelFlag(seelog.WarnStr)
    68  }
    69  func OptionErrorLevel() {
    70  	changeLevelFlag(seelog.ErrorStr)
    71  }
    72  
    73  func OptionOverrideConfig() {
    74  	changeReatainFlag(false)
    75  }
    76  
    77  func OptionRetainConfig() {
    78  	changeReatainFlag(true)
    79  }
    80  
    81  func changeSyncFlag(sync bool) {
    82  	if sync {
    83  		template = syncPattern
    84  	} else {
    85  		template = asyncPattern
    86  	}
    87  }
    88  
    89  func changeConsoleFlag(console bool) {
    90  	consoleFlag = console
    91  	if *loggerConsole != "" {
    92  		if c, err := strconv.ParseBool(*loggerConsole); err == nil {
    93  			consoleFlag = c
    94  		}
    95  	}
    96  }
    97  
    98  func changeLevelFlag(level string) {
    99  	levelFlag = level
   100  	if *loggerLevel != "" {
   101  		if _, found := seelog.LogLevelFromString(*loggerLevel); found {
   102  			levelFlag = *loggerLevel
   103  		}
   104  	}
   105  }
   106  
   107  func changeReatainFlag(retain bool) {
   108  	retainFlag = retain
   109  	if *loggerRetain != "" {
   110  		if c, err := strconv.ParseBool(*loggerRetain); err == nil {
   111  			retainFlag = c
   112  		}
   113  	}
   114  }