github.com/keakon/golog@v0.0.0-20230330091222-cac71197c18d/log_test.go (about)

     1  //go:build !race
     2  // +build !race
     3  
     4  // golog.FastTimer is not thread-safe.
     5  
     6  package golog
     7  
     8  import (
     9  	"bytes"
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func TestLogger(t *testing.T) {
    18  	fastTimer.start()
    19  	defer fastTimer.stop()
    20  
    21  	infoPath := filepath.Join(os.TempDir(), "test_info.log")
    22  	debugPath := filepath.Join(os.TempDir(), "test_debug.log")
    23  	os.Remove(infoPath)
    24  	os.Remove(debugPath)
    25  
    26  	infoWriter, err := NewFileWriter(infoPath)
    27  	if err != nil {
    28  		t.Error(err)
    29  	}
    30  	debugWriter, err := NewFileWriter(debugPath)
    31  	if err != nil {
    32  		t.Error(err)
    33  	}
    34  
    35  	infoHandler := NewHandler(InfoLevel, DefaultFormatter)
    36  	infoHandler.AddWriter(infoWriter)
    37  
    38  	debugHandler := &Handler{
    39  		formatter: DefaultFormatter,
    40  	}
    41  	debugHandler.AddWriter(debugWriter)
    42  
    43  	l := NewLogger(DebugLevel)
    44  	l.AddHandler(infoHandler)
    45  	l.AddHandler(debugHandler)
    46  
    47  	l.Debugf("test %d", 1)
    48  
    49  	stat, err := os.Stat(infoPath)
    50  	if err != nil {
    51  		t.Error(err)
    52  	}
    53  	if stat.Size() != 0 {
    54  		t.Errorf("file size are %d", stat.Size())
    55  	}
    56  
    57  	debugContent, err := ioutil.ReadFile(debugPath)
    58  	if err != nil {
    59  		t.Error(err)
    60  	}
    61  	size1 := len(debugContent)
    62  	if size1 == 0 {
    63  		t.Error("debug log is empty")
    64  	}
    65  
    66  	l.Infof("test %d", 2)
    67  
    68  	infoContent, err := ioutil.ReadFile(infoPath)
    69  	if err != nil {
    70  		t.Error(err)
    71  	}
    72  	size2 := len(infoContent)
    73  	if size2 != size1 {
    74  		t.Error("the sizes of debug and info logs are not equal")
    75  	}
    76  
    77  	parts := strings.Fields(string(infoContent))
    78  	if len(parts) != 6 {
    79  		t.Errorf("parts length are %d", len(parts))
    80  	}
    81  	if parts[0] != "[I" {
    82  		t.Errorf("parts[0] is " + parts[0])
    83  	}
    84  	if len(parts[1]) != 10 {
    85  		t.Errorf("parts[1] is " + parts[1])
    86  	}
    87  	if len(parts[2]) != 8 {
    88  		t.Errorf("parts[2] is " + parts[2])
    89  	}
    90  	if !strings.HasPrefix(parts[3], "log_test:") {
    91  		t.Errorf("parts[3] is " + parts[3])
    92  	}
    93  	if parts[4] != "test" {
    94  		t.Errorf("parts[4] is " + parts[4])
    95  	}
    96  	if parts[5] != "2" {
    97  		t.Errorf("parts[5] is " + parts[5])
    98  	}
    99  
   100  	debugContent, err = ioutil.ReadFile(debugPath)
   101  	if err != nil {
   102  		t.Error(err)
   103  	}
   104  	size3 := len(debugContent)
   105  	if size3 == size1*2 {
   106  		if !bytes.Equal(debugContent[size1:], infoContent) {
   107  			t.Error("log contents are not equal")
   108  		}
   109  	} else {
   110  		t.Errorf("debug log size are %d bytes", size2)
   111  	}
   112  
   113  	if !bytes.Equal(debugContent[size1:], infoContent) {
   114  		t.Error("log contents are not equal")
   115  	}
   116  
   117  	l.Debug(1)
   118  	l.Info(1)
   119  	l.Warn(1)
   120  	l.Error(1)
   121  	l.Crit(1)
   122  	l.Warnf("1")
   123  	l.Errorf("1")
   124  	l.Critf("1")
   125  
   126  	infoContent, err = ioutil.ReadFile(infoPath)
   127  	if err != nil {
   128  		t.Error(err)
   129  	}
   130  	size4 := len(infoContent)
   131  	if size4 <= size2 {
   132  		t.Error("info log size not changed")
   133  	}
   134  
   135  	debugContent, err = ioutil.ReadFile(debugPath)
   136  	if err != nil {
   137  		t.Error(err)
   138  	}
   139  	size5 := len(debugContent)
   140  	if size5 <= size3 {
   141  		t.Error("debug log size not changed")
   142  	}
   143  	if size5 <= size4 {
   144  		t.Error("info log size is larger than debug log size")
   145  	}
   146  }
   147  
   148  func TestGetMinLevel(t *testing.T) {
   149  	l := NewLogger(InfoLevel)
   150  	defer l.Close()
   151  	if l.GetMinLevel() != disabledLevel {
   152  		t.Errorf("GetMinLevel failed")
   153  	}
   154  
   155  	errorHandler := NewHandler(ErrorLevel, DefaultFormatter)
   156  	l.AddHandler(errorHandler)
   157  	if l.GetMinLevel() != ErrorLevel {
   158  		t.Errorf("GetMinLevel failed")
   159  	}
   160  
   161  	debugHandler := NewHandler(DebugLevel, DefaultFormatter)
   162  	l.AddHandler(debugHandler)
   163  	if l.GetMinLevel() != InfoLevel {
   164  		t.Errorf("GetMinLevel failed")
   165  	}
   166  }
   167  
   168  func TestAddHandler(t *testing.T) {
   169  	w := NewDiscardWriter()
   170  
   171  	dh := NewHandler(DebugLevel, DefaultFormatter)
   172  	dh.AddWriter(w)
   173  
   174  	ih := NewHandler(InfoLevel, DefaultFormatter)
   175  	ih.AddWriter(w)
   176  
   177  	wh := NewHandler(WarnLevel, DefaultFormatter)
   178  	wh.AddWriter(w)
   179  
   180  	eh := NewHandler(ErrorLevel, DefaultFormatter)
   181  	eh.AddWriter(w)
   182  
   183  	ch := NewHandler(CritLevel, DefaultFormatter)
   184  	ch.AddWriter(w)
   185  
   186  	l := NewLogger(InfoLevel)
   187  	if l.IsEnabledFor(CritLevel) {
   188  		t.Error("an empty logger should not be enabled for any level")
   189  	}
   190  
   191  	l.AddHandler(ch)
   192  	if !l.IsEnabledFor(CritLevel) {
   193  		t.Error("the logger is not enable for critical level")
   194  	}
   195  	if l.IsEnabledFor(ErrorLevel) {
   196  		t.Error("the logger is enable for error level")
   197  	}
   198  
   199  	l.AddHandler(eh)
   200  	if !l.IsEnabledFor(ErrorLevel) {
   201  		t.Error("the logger is not enable for error level")
   202  	}
   203  
   204  	l.AddHandler(wh)
   205  	if !l.IsEnabledFor(WarnLevel) {
   206  		t.Error("the logger is not enable for warning level")
   207  	}
   208  
   209  	l.AddHandler(ih)
   210  	if !l.IsEnabledFor(InfoLevel) {
   211  		t.Error("the logger is not enable for info level")
   212  	}
   213  
   214  	l.AddHandler(dh)
   215  	if l.IsEnabledFor(DebugLevel) {
   216  		t.Error("info logger should not enable for debug level")
   217  	}
   218  
   219  	count := len(l.handlers)
   220  	if count != 5 {
   221  		t.Errorf("the logger has %d handlers", count)
   222  	}
   223  
   224  	for i := 0; i < count-1; i++ {
   225  		if l.handlers[i].level > l.handlers[i+1].level {
   226  			t.Errorf("handlers[%d].level > handlers[%d].level", i, i+1)
   227  		}
   228  	}
   229  }
   230  
   231  func TestCloseLogger(t *testing.T) {
   232  	l := &Logger{}
   233  	l.Close()
   234  	l.Close()
   235  
   236  	l = NewStdoutLogger()
   237  	h := l.handlers[0]
   238  	w := h.writers[0].(*ConsoleWriter)
   239  	l.Close()
   240  	if len(l.handlers) > 0 {
   241  		t.Error("closed logger is not empty")
   242  	}
   243  	if len(h.writers) > 0 {
   244  		t.Error("closed handler is not empty")
   245  	}
   246  	if w.File != nil {
   247  		t.Error("close logger left its writer opened")
   248  	}
   249  	l.Close()
   250  }
   251  
   252  func TestNewStdoutLogger(t *testing.T) {
   253  	l := NewStdoutLogger()
   254  	if l.IsEnabledFor(DebugLevel) {
   255  		t.Error("stdout logger is enabled for debug level")
   256  	}
   257  	if !l.IsEnabledFor(InfoLevel) {
   258  		t.Error("stdout logger is not enabled for info level")
   259  	}
   260  	if !l.IsEnabledFor(ErrorLevel) {
   261  		t.Error("stdout logger is not enabled for error level")
   262  	}
   263  }