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

     1  //go:build linux
     2  // +build linux
     3  
     4  package log
     5  
     6  import (
     7  	"net"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  // journalctl -o verbose -f
    14  func TestJournalWriter(t *testing.T) {
    15  	w := &JournalWriter{}
    16  
    17  	for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
    18  		_, _ = 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 journal writer"}`+"\n", level)
    19  	}
    20  
    21  	_, _ = wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
    22  	_, _ = wlprintf(w, InfoLevel, "a long long long long message.\n")
    23  	w.Close()
    24  }
    25  
    26  func TestJournalWriterError(t *testing.T) {
    27  	const sockname = "/tmp/go-tmp-null.sock"
    28  
    29  	conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: sockname, Net: "unixgram"})
    30  	if err != nil {
    31  		t.Errorf("listen error: %+v", err)
    32  		return
    33  	}
    34  	defer os.Remove(sockname)
    35  
    36  	go func() {
    37  		var data [512]byte
    38  		for {
    39  			buf := data[:]
    40  			n, uaddr, err := conn.ReadFromUnix(buf)
    41  			if err != nil {
    42  				t.Logf("listen: error: %v\n", err)
    43  			} else {
    44  				t.Logf("listen: received %v bytes from %+v\n", n, uaddr)
    45  			}
    46  			time.Sleep(10 * time.Millisecond)
    47  		}
    48  	}()
    49  
    50  	w := &JournalWriter{
    51  		JournalSocket: sockname,
    52  	}
    53  
    54  	for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
    55  		_, _ = 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 journal writer"}`+"\n", level)
    56  	}
    57  
    58  	_, _ = wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
    59  	_, _ = wlprintf(w, InfoLevel, "a long long long long message.\n")
    60  	w.Close()
    61  }