github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/log/writer_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package log
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  type CallbackWriteCloser struct {
    18  	callback func([]byte, bool)
    19  }
    20  
    21  func (c CallbackWriteCloser) Write(p []byte) (int, error) {
    22  	c.callback(p, false)
    23  	return len(p), nil
    24  }
    25  
    26  func (c CallbackWriteCloser) Close() error {
    27  	c.callback(nil, true)
    28  	return nil
    29  }
    30  
    31  func TestBaseLogger(t *testing.T) {
    32  	var written []byte
    33  	var closed bool
    34  
    35  	c := CallbackWriteCloser{
    36  		callback: func(p []byte, close bool) {
    37  			written = p
    38  			closed = close
    39  		},
    40  	}
    41  	prefix := "TestPrefix "
    42  	b := WriterLogger{
    43  		out:    c,
    44  		Level:  INFO,
    45  		Flags:  LstdFlags | LUTC,
    46  		Prefix: prefix,
    47  	}
    48  	location, _ := time.LoadLocation("EST")
    49  
    50  	date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
    51  
    52  	dateString := date.UTC().Format("2006/01/02 15:04:05")
    53  
    54  	event := Event{
    55  		level:    INFO,
    56  		msg:      "TEST MSG",
    57  		caller:   "CALLER",
    58  		filename: "FULL/FILENAME",
    59  		line:     1,
    60  		time:     date,
    61  	}
    62  
    63  	assert.Equal(t, INFO, b.GetLevel())
    64  
    65  	expected := fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
    66  	b.LogEvent(&event)
    67  	assert.Equal(t, expected, string(written))
    68  	assert.False(t, closed)
    69  	written = written[:0]
    70  
    71  	event.level = DEBUG
    72  	expected = ""
    73  	b.LogEvent(&event)
    74  	assert.Equal(t, expected, string(written))
    75  	assert.False(t, closed)
    76  
    77  	event.level = TRACE
    78  	expected = ""
    79  	b.LogEvent(&event)
    80  	assert.Equal(t, expected, string(written))
    81  	assert.False(t, closed)
    82  
    83  	event.level = WARN
    84  	expected = fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
    85  	b.LogEvent(&event)
    86  	assert.Equal(t, expected, string(written))
    87  	assert.False(t, closed)
    88  	written = written[:0]
    89  
    90  	event.level = ERROR
    91  	expected = fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
    92  	b.LogEvent(&event)
    93  	assert.Equal(t, expected, string(written))
    94  	assert.False(t, closed)
    95  	written = written[:0]
    96  
    97  	event.level = CRITICAL
    98  	expected = fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
    99  	b.LogEvent(&event)
   100  	assert.Equal(t, expected, string(written))
   101  	assert.False(t, closed)
   102  	written = written[:0]
   103  
   104  	b.Close()
   105  	assert.True(t, closed)
   106  }
   107  
   108  func TestBaseLoggerDated(t *testing.T) {
   109  	var written []byte
   110  	var closed bool
   111  
   112  	c := CallbackWriteCloser{
   113  		callback: func(p []byte, close bool) {
   114  			written = p
   115  			closed = close
   116  		},
   117  	}
   118  	prefix := ""
   119  	b := WriterLogger{
   120  		out:    c,
   121  		Level:  WARN,
   122  		Flags:  Ldate | Ltime | Lmicroseconds | Lshortfile | Llevel,
   123  		Prefix: prefix,
   124  	}
   125  
   126  	location, _ := time.LoadLocation("EST")
   127  
   128  	date := time.Date(2019, time.January, 13, 22, 3, 30, 115, location)
   129  
   130  	dateString := date.Format("2006/01/02 15:04:05.000000")
   131  
   132  	event := Event{
   133  		level:    WARN,
   134  		msg:      "TEST MESSAGE TEST\n",
   135  		caller:   "CALLER",
   136  		filename: "FULL/FILENAME",
   137  		line:     1,
   138  		time:     date,
   139  	}
   140  
   141  	assert.Equal(t, WARN, b.GetLevel())
   142  
   143  	expected := fmt.Sprintf("%s%s %s:%d [%s] %s", prefix, dateString, "FILENAME", event.line, strings.ToUpper(event.level.String()), event.msg)
   144  	b.LogEvent(&event)
   145  	assert.Equal(t, expected, string(written))
   146  	assert.False(t, closed)
   147  	written = written[:0]
   148  
   149  	event.level = INFO
   150  	expected = ""
   151  	b.LogEvent(&event)
   152  	assert.Equal(t, expected, string(written))
   153  	assert.False(t, closed)
   154  	written = written[:0]
   155  
   156  	event.level = ERROR
   157  	expected = fmt.Sprintf("%s%s %s:%d [%s] %s", prefix, dateString, "FILENAME", event.line, strings.ToUpper(event.level.String()), event.msg)
   158  	b.LogEvent(&event)
   159  	assert.Equal(t, expected, string(written))
   160  	assert.False(t, closed)
   161  	written = written[:0]
   162  
   163  	event.level = DEBUG
   164  	expected = ""
   165  	b.LogEvent(&event)
   166  	assert.Equal(t, expected, string(written))
   167  	assert.False(t, closed)
   168  	written = written[:0]
   169  
   170  	event.level = CRITICAL
   171  	expected = fmt.Sprintf("%s%s %s:%d [%s] %s", prefix, dateString, "FILENAME", event.line, strings.ToUpper(event.level.String()), event.msg)
   172  	b.LogEvent(&event)
   173  	assert.Equal(t, expected, string(written))
   174  	assert.False(t, closed)
   175  	written = written[:0]
   176  
   177  	event.level = TRACE
   178  	expected = ""
   179  	b.LogEvent(&event)
   180  	assert.Equal(t, expected, string(written))
   181  	assert.False(t, closed)
   182  	written = written[:0]
   183  
   184  	b.Close()
   185  	assert.True(t, closed)
   186  }
   187  
   188  func TestBaseLoggerMultiLineNoFlagsRegexp(t *testing.T) {
   189  	var written []byte
   190  	var closed bool
   191  
   192  	c := CallbackWriteCloser{
   193  		callback: func(p []byte, close bool) {
   194  			written = p
   195  			closed = close
   196  		},
   197  	}
   198  	prefix := ""
   199  	b := WriterLogger{
   200  		Level:           DEBUG,
   201  		StacktraceLevel: ERROR,
   202  		Flags:           -1,
   203  		Prefix:          prefix,
   204  		Expression:      "FILENAME",
   205  	}
   206  	b.NewWriterLogger(c)
   207  
   208  	location, _ := time.LoadLocation("EST")
   209  
   210  	date := time.Date(2019, time.January, 13, 22, 3, 30, 115, location)
   211  
   212  	event := Event{
   213  		level:    DEBUG,
   214  		msg:      "TEST\nMESSAGE\nTEST",
   215  		caller:   "CALLER",
   216  		filename: "FULL/FILENAME",
   217  		line:     1,
   218  		time:     date,
   219  	}
   220  
   221  	assert.Equal(t, DEBUG, b.GetLevel())
   222  
   223  	expected := "TEST\n\tMESSAGE\n\tTEST\n"
   224  	b.LogEvent(&event)
   225  	assert.Equal(t, expected, string(written))
   226  	assert.False(t, closed)
   227  	written = written[:0]
   228  
   229  	event.filename = "ELSEWHERE"
   230  
   231  	b.LogEvent(&event)
   232  	assert.Equal(t, "", string(written))
   233  	assert.False(t, closed)
   234  	written = written[:0]
   235  
   236  	event.caller = "FILENAME"
   237  	b.LogEvent(&event)
   238  	assert.Equal(t, expected, string(written))
   239  	assert.False(t, closed)
   240  	written = written[:0]
   241  
   242  	event = Event{
   243  		level:    DEBUG,
   244  		msg:      "TEST\nFILENAME\nTEST",
   245  		caller:   "CALLER",
   246  		filename: "FULL/ELSEWHERE",
   247  		line:     1,
   248  		time:     date,
   249  	}
   250  	expected = "TEST\n\tFILENAME\n\tTEST\n"
   251  	b.LogEvent(&event)
   252  	assert.Equal(t, expected, string(written))
   253  	assert.False(t, closed)
   254  	written = written[:0]
   255  }
   256  
   257  func TestBrokenRegexp(t *testing.T) {
   258  	var closed bool
   259  
   260  	c := CallbackWriteCloser{
   261  		callback: func(p []byte, close bool) {
   262  			closed = close
   263  		},
   264  	}
   265  
   266  	b := WriterLogger{
   267  		Level:           DEBUG,
   268  		StacktraceLevel: ERROR,
   269  		Flags:           -1,
   270  		Prefix:          prefix,
   271  		Expression:      "\\",
   272  	}
   273  	b.NewWriterLogger(c)
   274  	assert.Empty(t, b.regexp)
   275  	b.Close()
   276  	assert.True(t, closed)
   277  }