github.com/phuslu/log@v1.0.100/multi_test.go (about)

     1  package log
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  func TestMultiWriter(t *testing.T) {
    12  	w := &MultiWriter{
    13  		InfoWriter:    &FileWriter{Filename: "file-info.log"},
    14  		WarnWriter:    &FileWriter{Filename: "file-warn.log"},
    15  		ErrorWriter:   &FileWriter{Filename: "file-error.log"},
    16  		ConsoleWriter: &ConsoleWriter{ColorOutput: true},
    17  		ConsoleLevel:  ErrorLevel,
    18  	}
    19  
    20  	for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
    21  		_, err := wlprintf(w, ParseLevel(level), `{"ts":1234567890,"level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
    22  		if err != nil {
    23  			t.Errorf("test json mutli writer error: %+v", err)
    24  		}
    25  		_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
    26  		if err != nil {
    27  			t.Errorf("test json mutli writer error: %+v", err)
    28  		}
    29  		_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277+08:00","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
    30  		if err != nil {
    31  			t.Errorf("test json mutli writer error: %+v", err)
    32  		}
    33  	}
    34  
    35  	if err := w.Close(); err != nil {
    36  		t.Errorf("test close mutli writer error: %+v", err)
    37  	}
    38  
    39  	matches, _ := filepath.Glob("file-*.*.log")
    40  	for i := range matches {
    41  		err := os.Remove(matches[i])
    42  		if err != nil {
    43  			t.Fatalf("os remove %s error: %+v", matches[i], err)
    44  		}
    45  	}
    46  }
    47  
    48  type errorWriter struct {
    49  	io.WriteCloser
    50  }
    51  
    52  var errorWriterOK = errors.New("errorWriter return OK")
    53  
    54  func (ew errorWriter) Write(p []byte) (n int, err error) {
    55  	n, _ = ew.WriteCloser.Write(p)
    56  	err = errorWriterOK
    57  	return
    58  }
    59  
    60  func (ew errorWriter) Close() (err error) {
    61  	ew.WriteCloser.Close()
    62  	err = errorWriterOK
    63  	return
    64  }
    65  
    66  func TestMultiWriterError(t *testing.T) {
    67  	file, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0644)
    68  	if err != nil {
    69  		t.Errorf("open null file error: %+v", err)
    70  	}
    71  
    72  	w := &MultiWriter{
    73  		InfoWriter:    IOWriter{errorWriter{file}},
    74  		WarnWriter:    IOWriter{errorWriter{file}},
    75  		ErrorWriter:   IOWriter{errorWriter{file}},
    76  		ConsoleWriter: &ConsoleWriter{ColorOutput: true},
    77  		ConsoleLevel:  TraceLevel,
    78  	}
    79  
    80  	for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
    81  		_, err := wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
    82  		if err == nil {
    83  			t.Errorf("test json error writer error: %+v", err)
    84  		}
    85  	}
    86  
    87  	if err := w.Close(); err != nil {
    88  		t.Errorf("test close error writer error: %+v", err)
    89  	}
    90  }
    91  
    92  func TestMultiWriterEntry(t *testing.T) {
    93  	w := &MultiWriter{
    94  		ConsoleWriter: &ConsoleWriter{
    95  			ColorOutput: true,
    96  		},
    97  		ConsoleLevel: InfoLevel,
    98  	}
    99  
   100  	var err error
   101  	for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
   102  		_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
   103  		if err != nil {
   104  			t.Errorf("test json mutli writer error: %+v", err)
   105  		}
   106  	}
   107  
   108  	if err := w.Close(); err != nil {
   109  		t.Errorf("test close mutli writer error: %+v", err)
   110  	}
   111  }
   112  
   113  func TestMultiEntryWriter(t *testing.T) {
   114  	w := &MultiEntryWriter{
   115  		&FileWriter{Filename: "file-1.log"},
   116  		&FileWriter{Filename: "file-2.log"},
   117  		&ConsoleWriter{ColorOutput: true},
   118  	}
   119  
   120  	for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
   121  		_, err := wlprintf(w, ParseLevel(level), `{"ts":1234567890,"level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
   122  		if err != nil {
   123  			t.Errorf("test json mutli writer error: %+v", err)
   124  		}
   125  		_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
   126  		if err != nil {
   127  			t.Errorf("test json mutli writer error: %+v", err)
   128  		}
   129  		_, err = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277+08:00","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
   130  		if err != nil {
   131  			t.Errorf("test json mutli writer error: %+v", err)
   132  		}
   133  	}
   134  
   135  	if err := w.Close(); err != nil {
   136  		t.Errorf("test close mutli writer error: %+v", err)
   137  	}
   138  
   139  	matches, _ := filepath.Glob("file-*.*.log")
   140  	for i := range matches {
   141  		err := os.Remove(matches[i])
   142  		if err != nil {
   143  			t.Fatalf("os remove %s error: %+v", matches[i], err)
   144  		}
   145  	}
   146  }
   147  
   148  type errorEntryWriter struct {
   149  	io.WriteCloser
   150  }
   151  
   152  var errorEntryWriterOK = errors.New("errorEntryWriter return OK")
   153  
   154  func (ew errorEntryWriter) Write(p []byte) (n int, err error) {
   155  	n, _ = ew.WriteCloser.Write(p)
   156  	err = errorEntryWriterOK
   157  	return
   158  }
   159  
   160  func (ew errorEntryWriter) Close() (err error) {
   161  	ew.WriteCloser.Close()
   162  	err = errorEntryWriterOK
   163  	return
   164  }
   165  
   166  func TestMultiEntryWriterError(t *testing.T) {
   167  	file, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0644)
   168  	if err != nil {
   169  		t.Errorf("open null file error: %+v", err)
   170  	}
   171  
   172  	w := &MultiEntryWriter{
   173  		IOWriter{errorEntryWriter{file}},
   174  		IOWriter{errorEntryWriter{file}},
   175  		&ConsoleWriter{ColorOutput: true},
   176  	}
   177  
   178  	for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
   179  		_, err := wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello json mutli writer"}`+"\n", level)
   180  		if err == nil {
   181  			t.Errorf("test json error writer error: %+v", err)
   182  		}
   183  	}
   184  
   185  	if err := w.Close(); err != nil {
   186  		t.Errorf("test close error writer error: %+v", err)
   187  	}
   188  }