...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/log/formatter_test.go

Documentation: github.com/cybertec-postgresql/pgwatch/v3/internal/log

     1  package log_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"path"
     8  	"regexp"
     9  	"runtime"
    10  	"strings"
    11  	"testing"
    12  
    13  	formatter "github.com/cybertec-postgresql/pgwatch/v3/internal/log"
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  func ExampleFormatter_Format_default() {
    18  	l := logrus.New()
    19  	l.SetOutput(os.Stdout)
    20  	l.SetLevel(logrus.DebugLevel)
    21  	l.SetFormatter(&formatter.Formatter{
    22  		NoColors:        true,
    23  		TimestampFormat: "-",
    24  	})
    25  
    26  	l.Debug("test1")
    27  	l.Info("test2")
    28  	l.Warn("test3")
    29  	l.Error("test4")
    30  
    31  	// Output:
    32  	// - [DEBU] test1
    33  	// - [INFO] test2
    34  	// - [WARN] test3
    35  	// - [ERRO] test4
    36  }
    37  
    38  func ExampleFormatter_Format_full_level() {
    39  	l := logrus.New()
    40  	l.SetOutput(os.Stdout)
    41  	l.SetLevel(logrus.DebugLevel)
    42  	l.SetFormatter(&formatter.Formatter{
    43  		NoColors:        true,
    44  		TimestampFormat: "-",
    45  		ShowFullLevel:   true,
    46  	})
    47  
    48  	l.Debug("test1")
    49  	l.Info("test2")
    50  	l.Warn("test3")
    51  	l.Error("   test4")
    52  
    53  	// Output:
    54  	// - [DEBUG] test1
    55  	// - [INFO] test2
    56  	// - [WARNING] test3
    57  	// - [ERROR]    test4
    58  }
    59  func ExampleFormatter_Format_show_keys() {
    60  	l := logrus.New()
    61  	l.SetOutput(os.Stdout)
    62  	l.SetLevel(logrus.DebugLevel)
    63  	l.SetFormatter(&formatter.Formatter{
    64  		NoColors:        true,
    65  		TimestampFormat: "-",
    66  		HideKeys:        false,
    67  	})
    68  
    69  	ll := l.WithField("category", "rest")
    70  
    71  	l.Info("test1")
    72  	ll.Info("test2")
    73  
    74  	// Output:
    75  	// - [INFO] test1
    76  	// - [INFO] [category:rest] test2
    77  }
    78  
    79  func ExampleFormatter_Format_hide_keys() {
    80  	l := logrus.New()
    81  	l.SetOutput(os.Stdout)
    82  	l.SetLevel(logrus.DebugLevel)
    83  	l.SetFormatter(&formatter.Formatter{
    84  		NoColors:        true,
    85  		TimestampFormat: "-",
    86  		HideKeys:        true,
    87  	})
    88  
    89  	ll := l.WithField("category", "rest")
    90  
    91  	l.Info("test1")
    92  	ll.Info("test2")
    93  
    94  	// Output:
    95  	// - [INFO] test1
    96  	// - [INFO] [rest] test2
    97  }
    98  
    99  func ExampleFormatter_Format_sort_order() {
   100  	l := logrus.New()
   101  	l.SetOutput(os.Stdout)
   102  	l.SetLevel(logrus.DebugLevel)
   103  	l.SetFormatter(&formatter.Formatter{
   104  		NoColors:        true,
   105  		TimestampFormat: "-",
   106  		HideKeys:        false,
   107  	})
   108  
   109  	ll := l.WithField("component", "main")
   110  	lll := ll.WithField("category", "rest")
   111  
   112  	l.Info("test1")
   113  	ll.Info("test2")
   114  	lll.Info("test3")
   115  
   116  	// Output:
   117  	// - [INFO] test1
   118  	// - [INFO] [component:main] test2
   119  	// - [INFO] [category:rest] [component:main] test3
   120  }
   121  
   122  func ExampleFormatter_Format_field_order() {
   123  	l := logrus.New()
   124  	l.SetOutput(os.Stdout)
   125  	l.SetLevel(logrus.DebugLevel)
   126  	l.SetFormatter(&formatter.Formatter{
   127  		NoColors:        true,
   128  		TimestampFormat: "-",
   129  		FieldsOrder:     []string{"component", "category"},
   130  		HideKeys:        false,
   131  	})
   132  
   133  	ll := l.WithField("component", "main")
   134  	lll := ll.WithField("category", "rest")
   135  
   136  	l.Info("test1")
   137  	ll.Info("test2")
   138  	lll.Info("test3")
   139  
   140  	// Output:
   141  	// - [INFO] test1
   142  	// - [INFO] [component:main] test2
   143  	// - [INFO] [component:main] [category:rest] test3
   144  }
   145  
   146  func ExampleFormatter_Format_no_fields_space() {
   147  	l := logrus.New()
   148  	l.SetOutput(os.Stdout)
   149  	l.SetLevel(logrus.DebugLevel)
   150  	l.SetFormatter(&formatter.Formatter{
   151  		NoColors:        true,
   152  		TimestampFormat: "-",
   153  		FieldsOrder:     []string{"component", "category"},
   154  		HideKeys:        false,
   155  		NoFieldsSpace:   true,
   156  	})
   157  
   158  	ll := l.WithField("component", "main")
   159  	lll := ll.WithField("category", "rest")
   160  
   161  	l.Info("test1")
   162  	ll.Info("test2")
   163  	lll.Info("test3")
   164  
   165  	// Output:
   166  	// - [INFO] test1
   167  	// - [INFO][component:main] test2
   168  	// - [INFO][component:main][category:rest] test3
   169  }
   170  
   171  func ExampleFormatter_Format_no_uppercase_level() {
   172  	l := logrus.New()
   173  	l.SetOutput(os.Stdout)
   174  	l.SetLevel(logrus.DebugLevel)
   175  	l.SetFormatter(&formatter.Formatter{
   176  		NoColors:         true,
   177  		TimestampFormat:  "-",
   178  		FieldsOrder:      []string{"component", "category"},
   179  		NoUppercaseLevel: true,
   180  	})
   181  
   182  	ll := l.WithField("component", "main")
   183  	lll := ll.WithField("category", "rest")
   184  	llll := ll.WithField("category", "other")
   185  
   186  	l.Debug("test1")
   187  	ll.Info("test2")
   188  	lll.Warn("test3")
   189  	llll.Error("test4")
   190  
   191  	// Output:
   192  	// - [debu] test1
   193  	// - [info] [component:main] test2
   194  	// - [warn] [component:main] [category:rest] test3
   195  	// - [erro] [component:main] [category:other] test4
   196  }
   197  
   198  func ExampleFormatter_Format_trim_message() {
   199  	l := logrus.New()
   200  	l.SetOutput(os.Stdout)
   201  	l.SetLevel(logrus.DebugLevel)
   202  	l.SetFormatter(&formatter.Formatter{
   203  		TrimMessages:    true,
   204  		NoColors:        true,
   205  		TimestampFormat: "-",
   206  	})
   207  
   208  	l.Debug(" test1 ")
   209  	l.Info("test2 ")
   210  	l.Warn(" test3")
   211  	l.Error("   test4   ")
   212  
   213  	// Output:
   214  	// - [DEBU] test1
   215  	// - [INFO] test2
   216  	// - [WARN] test3
   217  	// - [ERRO] test4
   218  }
   219  
   220  func TestFormatter_Format_with_report_caller(t *testing.T) {
   221  	output := bytes.NewBuffer([]byte{})
   222  
   223  	l := logrus.New()
   224  	l.SetOutput(output)
   225  	l.SetLevel(logrus.DebugLevel)
   226  	l.SetFormatter(&formatter.Formatter{
   227  		NoColors:        true,
   228  		TimestampFormat: "-",
   229  	})
   230  	l.SetReportCaller(true)
   231  
   232  	l.Debug("test1")
   233  
   234  	line, err := output.ReadString('\n')
   235  	if err != nil {
   236  		t.Errorf("Cannot read log output: %v", err)
   237  	}
   238  
   239  	expectedRegExp := "- \\[DEBU\\] test1 \\(.+\\.go:[0-9]+ .+\\)\n$"
   240  	match, err := regexp.MatchString(
   241  		expectedRegExp,
   242  		line,
   243  	)
   244  	if err != nil {
   245  		t.Errorf("Cannot check regexp: %v", err)
   246  	} else if !match {
   247  		t.Errorf(
   248  			"logger.SetReportCaller(true) output doesn't match, expected: %s to find in: '%s'",
   249  			expectedRegExp,
   250  			line,
   251  		)
   252  	}
   253  }
   254  
   255  func TestFormatter_Format_with_report_caller_and_CallerFirst_true(t *testing.T) {
   256  	output := bytes.NewBuffer([]byte{})
   257  
   258  	l := logrus.New()
   259  	l.SetOutput(output)
   260  	l.SetLevel(logrus.DebugLevel)
   261  	l.SetFormatter(&formatter.Formatter{
   262  		NoColors:        true,
   263  		TimestampFormat: "-",
   264  		CallerFirst:     true,
   265  	})
   266  	l.SetReportCaller(true)
   267  
   268  	l.Debug("test1")
   269  
   270  	line, err := output.ReadString('\n')
   271  	if err != nil {
   272  		t.Errorf("Cannot read log output: %v", err)
   273  	}
   274  
   275  	expectedRegExp := "- \\(.+\\.go:[0-9]+ .+\\) \\[DEBU\\] test1\n$"
   276  	match, err := regexp.MatchString(
   277  		expectedRegExp,
   278  		line,
   279  	)
   280  
   281  	if err != nil {
   282  		t.Errorf("Cannot check regexp: %v", err)
   283  	} else if !match {
   284  		t.Errorf(
   285  			"logger.SetReportCaller(true) output doesn't match, expected: %s to find in: '%s'",
   286  			expectedRegExp,
   287  			line,
   288  		)
   289  	}
   290  }
   291  
   292  func TestFormatter_Format_with_report_caller_and_CustomCallerFormatter(t *testing.T) {
   293  	output := bytes.NewBuffer([]byte{})
   294  
   295  	l := logrus.New()
   296  	l.SetOutput(output)
   297  	l.SetLevel(logrus.DebugLevel)
   298  	l.SetFormatter(&formatter.Formatter{
   299  		NoColors:        true,
   300  		TimestampFormat: "-",
   301  		CallerFirst:     true,
   302  		CustomCallerFormatter: func(f *runtime.Frame) string {
   303  			s := strings.Split(f.Function, ".")
   304  			funcName := s[len(s)-1]
   305  			return fmt.Sprintf(" [%s:%d][%s()]", path.Base(f.File), f.Line, funcName)
   306  		},
   307  	})
   308  	l.SetReportCaller(true)
   309  
   310  	l.Debug("test1")
   311  
   312  	line, err := output.ReadString('\n')
   313  	if err != nil {
   314  		t.Errorf("Cannot read log output: %v", err)
   315  	}
   316  
   317  	expectedRegExp := "- \\[.+\\.go:[0-9]+\\]\\[.+\\(\\)\\] \\[DEBU\\] test1\n$"
   318  	match, err := regexp.MatchString(
   319  		expectedRegExp,
   320  		line,
   321  	)
   322  	if err != nil {
   323  		t.Errorf("Cannot check regexp: %v", err)
   324  	} else if !match {
   325  		t.Errorf(
   326  			"logger.SetReportCaller(true) output doesn't match, expected: %s to find in: '%s'",
   327  			expectedRegExp,
   328  			line,
   329  		)
   330  	}
   331  }
   332