github.com/rudderlabs/rudder-go-kit@v0.30.0/logger/factory_test.go (about)

     1  package logger_test
     2  
     3  import (
     4  	"bufio"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  	"github.com/zenizh/go-capturer"
    10  
    11  	"github.com/rudderlabs/rudder-go-kit/config"
    12  	"github.com/rudderlabs/rudder-go-kit/logger"
    13  )
    14  
    15  func Test_Config_Default(t *testing.T) {
    16  	c := config.New()
    17  	stdout := capturer.CaptureStdout(func() {
    18  		loggerFactory := logger.NewFactory(c, constantClockOpt)
    19  		logger := loggerFactory.NewLogger()
    20  		logger.Info("hello world")
    21  		loggerFactory.Sync()
    22  	})
    23  	require.Contains(t, stdout, "2077-01-23T10:15:13.000Z")
    24  	require.Contains(t, stdout, "INFO")
    25  	require.Contains(t, stdout, "hello world")
    26  }
    27  
    28  func Test_Config_FileOutput(t *testing.T) {
    29  	tmpDir := t.TempDir()
    30  	c := config.New()
    31  	c.Set("Logger.enableTimestamp", false)
    32  	c.Set("Logger.enableConsole", false)
    33  	c.Set("Logger.enableFile", true)
    34  	c.Set("Logger.enableFileNameInLog", false)
    35  	c.Set("Logger.enableLoggerNameInLog", false)
    36  	c.Set("Logger.logFileLocation", tmpDir+"out.log")
    37  
    38  	stdout := capturer.CaptureStdout(func() {
    39  		loggerFactory := logger.NewFactory(c, constantClockOpt)
    40  		logger := loggerFactory.NewLogger()
    41  		logger.Info("hello world")
    42  	})
    43  	require.Empty(t, stdout, "it should not log anything to stdout")
    44  
    45  	fileOut, err := os.ReadFile(tmpDir + "out.log")
    46  
    47  	require.NoError(t, err, "file should exist")
    48  	require.Equal(t, "INFO	hello world\n", string(fileOut))
    49  }
    50  
    51  func TestLogLevelFromConfig(t *testing.T) {
    52  	fileName := t.TempDir() + "out.log"
    53  	f, err := os.Create(fileName)
    54  	require.NoError(t, err)
    55  	defer func() { _ = f.Close() }()
    56  
    57  	c := config.New()
    58  	// start with default log level WARN
    59  	c.Set("LOG_LEVEL", "INFO")
    60  	c.Set("Logger.enableConsole", false)
    61  	c.Set("Logger.enableFile", true)
    62  	c.Set("Logger.enableFileNameInLog", false)
    63  	c.Set("Logger.enableLoggerNameInLog", false)
    64  	c.Set("Logger.logFileLocation", fileName)
    65  
    66  	c.Set("Logger.moduleLevels", "1=DEBUG:1.2=WARN:1.2.3=ERROR")
    67  	loggerFactory := logger.NewFactory(c, constantClockOpt)
    68  	rootLogger := loggerFactory.NewLogger()
    69  	lvl1Logger := rootLogger.Child("1")
    70  	lvl2Logger := lvl1Logger.Child("2")
    71  	lvl3Logger := lvl2Logger.Child("3")
    72  
    73  	rootLogger.Info("hello world")
    74  	scanner := bufio.NewScanner(f)
    75  	require.True(t, scanner.Scan(), "it should print a log statement")
    76  	require.Equal(t, "2077-01-23T10:15:13.000Z	INFO	hello world", scanner.Text())
    77  
    78  	lvl1Logger.Debug("hello world")
    79  	require.True(t, scanner.Scan(), "it should print a log statement")
    80  	require.Equal(t, "2077-01-23T10:15:13.000Z	DEBUG	hello world", scanner.Text())
    81  
    82  	lvl2Logger.Warn("hello world")
    83  	require.True(t, scanner.Scan(), "it should print a log statement")
    84  	require.Equal(t, "2077-01-23T10:15:13.000Z	WARN	hello world", scanner.Text())
    85  
    86  	lvl3Logger.Error("hello world")
    87  	require.True(t, scanner.Scan(), "it should print a log statement")
    88  	require.Equal(t, "2077-01-23T10:15:13.000Z	ERROR	hello world", scanner.Text())
    89  
    90  	require.Equal(t, map[string]int{"1": 1, "1.2": 3, "1.2.3": 4}, loggerFactory.GetLoggingConfig())
    91  }
    92  
    93  func Test_SetLogLevel(t *testing.T) {
    94  	fileName := t.TempDir() + "out.log"
    95  	f, err := os.Create(fileName)
    96  	require.NoError(t, err)
    97  	defer func() { _ = f.Close() }()
    98  
    99  	c := config.New()
   100  	// start with default log level WARN
   101  	c.Set("LOG_LEVEL", "WARN")
   102  	c.Set("Logger.enableConsole", false)
   103  	c.Set("Logger.enableFile", true)
   104  	c.Set("Logger.enableFileNameInLog", false)
   105  	c.Set("Logger.enableLoggerNameInLog", false)
   106  	c.Set("Logger.logFileLocation", fileName)
   107  	loggerFactory := logger.NewFactory(c, constantClockOpt)
   108  	rootLogger := loggerFactory.NewLogger()
   109  
   110  	rootLogger.Info("hello world")
   111  	require.False(t, bufio.NewScanner(f).Scan(), "it should not print a log statement for a level lower than WARN")
   112  
   113  	rootLogger.Warn("hello world")
   114  	scanner := bufio.NewScanner(f)
   115  	require.True(t, scanner.Scan(), "it should print a log statement")
   116  	require.Equal(t, "2077-01-23T10:15:13.000Z	WARN	hello world", scanner.Text())
   117  
   118  	// change level to INFO
   119  	require.NoError(t, loggerFactory.SetLogLevel("", "INFO"))
   120  	rootLogger.Info("hello world")
   121  	require.True(t, scanner.Scan(), "it should print a log statement")
   122  	require.Equal(t, "2077-01-23T10:15:13.000Z	INFO	hello world", scanner.Text())
   123  
   124  	otherLogger := rootLogger.Child("other")
   125  	otherLogger.Info("other hello world")
   126  	require.True(t, scanner.Scan(), "it should print a log statement")
   127  	require.Equal(t, "2077-01-23T10:15:13.000Z	INFO	other hello world", scanner.Text())
   128  
   129  	require.NoError(t, loggerFactory.SetLogLevel("other", "DEBUG"))
   130  	otherLogger.Debug("other hello world")
   131  	require.True(t, scanner.Scan(), "it should print a log statement")
   132  	require.Equal(t, "2077-01-23T10:15:13.000Z	DEBUG	other hello world", scanner.Text())
   133  	rootLogger.Debug("other hello world")
   134  	require.False(t, scanner.Scan(), "it should not print a log statement for a level lower than INFO")
   135  }
   136  
   137  func Test_Config_Suppressed_Logs(t *testing.T) {
   138  	fileName := t.TempDir() + "out.log"
   139  	f, err := os.Create(fileName)
   140  	require.NoError(t, err)
   141  	defer func() { _ = f.Close() }()
   142  
   143  	c := config.New()
   144  	// start with default log level WARN
   145  	c.Set("LOG_LEVEL", "FATAL")
   146  	c.Set("Logger.enableConsole", false)
   147  	c.Set("Logger.enableFile", true)
   148  	c.Set("Logger.enableFileNameInLog", false)
   149  	c.Set("Logger.logFileLocation", fileName)
   150  	loggerFactory := logger.NewFactory(c, constantClockOpt)
   151  	rootLogger := loggerFactory.NewLogger()
   152  
   153  	rootLogger.Debug("hello world")
   154  	rootLogger.Debugf("hello %s", "world")
   155  	rootLogger.Debugw("hello world", "key", "value")
   156  	require.False(t, bufio.NewScanner(f).Scan(), "it should not print a log statement for a level lower than FATAL")
   157  
   158  	rootLogger.Info("hello world")
   159  	rootLogger.Infof("hello %s", "world")
   160  	rootLogger.Infow("hello world", "key", "value")
   161  	require.False(t, bufio.NewScanner(f).Scan(), "it should not print a log statement for a level lower than FATAL")
   162  
   163  	rootLogger.Warn("hello world")
   164  	rootLogger.Warnf("hello %s", "world")
   165  	rootLogger.Warnw("hello world", "key", "value")
   166  	require.False(t, bufio.NewScanner(f).Scan(), "it should not print a log statement for a level lower than FATAL")
   167  
   168  	rootLogger.Error("hello world")
   169  	rootLogger.Errorf("hello %s", "world")
   170  	rootLogger.Errorw("hello world", "key", "value")
   171  	require.False(t, bufio.NewScanner(f).Scan(), "it should not print a log statement for a level lower than FATAL")
   172  }