Beispiel #1
0
func TestBadWorkerCountAndCustomFormatFunc(t *testing.T) {

	addr, err := net.ResolveUDPAddr("udp", ":2004")
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}

	conn, err := net.ListenUDP("udp", addr)
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}
	defer conn.Close()

	sLog, err := New("udp", "127.0.0.1:2004", "", nil)
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}

	sLog.SetDisplayColor(true)
	sLog.SetBuffersAndWorkers(3, 0)
	sLog.SetTimestampFormat("2006")
	sLog.SetFormatFunc(func(s *Syslog) Formatter {
		return func(e *log.Entry) []byte {
			return []byte(e.Message)
		}
	})

	log.RegisterHandler(sLog, log.AllLevels...)

	log.Debug("debug")
	if s := hasString(conn); s != "debug" {
		log.Errorf("Expected '%s' Got '%s'", "debug", s)
	}
}
Beispiel #2
0
func TestSetFilenameColor(t *testing.T) {
	addr, err := net.ResolveUDPAddr("udp", ":2006")
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}

	conn, err := net.ListenUDP("udp", addr)
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}
	defer conn.Close()

	sLog, err := New("udp", "127.0.0.1:2006", "", nil)
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}

	sLog.SetDisplayColor(true)
	sLog.SetBuffersAndWorkers(3, 1)
	sLog.SetTimestampFormat("MST")
	sLog.SetFilenameDisplay(log.Llongfile)

	log.RegisterHandler(sLog, log.AllLevels...)

	log.Error("error")
	if s := hasString(conn); !strings.Contains(s, "log/handlers/syslog/syslog_test.go:367 error") {
		t.Errorf("Expected '%s' Got '%s'", "log/handlers/syslog/syslog_test.go:367 error", s)
	}
}
Beispiel #3
0
func TestBadAddress(t *testing.T) {
	sLog, err := New("udp", "255.255.255.67", "", nil)
	if err == nil {
		log.Errorf("Expected '%v' Got '%v'", "not nil", err)
	}

	if sLog != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, sLog)
	}
}
Beispiel #4
0
func TestCustomFormatFunc(t *testing.T) {

	buff := new(bytes.Buffer)
	cLog := New()
	cLog.SetWriter(buff)
	cLog.SetTimestampFormat("2006")
	cLog.SetBuffersAndWorkers(3, 2)
	cLog.SetFormatFunc(func(c *Console) Formatter {

		var b []byte

		return func(e *log.Entry) []byte {
			b = b[0:0]
			b = append(b, e.Message...)
			return b
		}
	})

	log.RegisterHandler(cLog, log.AllLevels...)

	log.Debug("debug")
	if buff.String() != "debug" {
		log.Errorf("Expected '%s' Got '%s'", "debug", buff.String())
	}
	buff.Reset()
}
Beispiel #5
0
func TestSyslogTLS(t *testing.T) {

	// setup server

	addr, err := net.ResolveTCPAddr("tcp", ":2022")
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}

	cnn, err := net.ListenTCP("tcp", addr)
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}
	defer cnn.Close()

	tlsConfig := &tls.Config{
		Certificates: make([]tls.Certificate, 1),
	}

	tlsConfig.Certificates[0], err = tls.X509KeyPair([]byte(publicKey), []byte(privateKey))
	if err != nil {
		log.Errorf("Expected '%v' Got '%v'", nil, err)
	}

	conn := tls.NewListener(cnn, tlsConfig)

	var msg string
	var m sync.Mutex

	go func() {
		client, err := conn.Accept()
		if err != nil {
			log.Errorf("Expected '%v' Got '%v'", nil, err)
		}

		b := make([]byte, 1024)

		read, err := client.Read(b)
		if err != nil {
			log.Errorf("Expected '%v' Got '%v'", nil, err)
		}

		m.Lock()
		defer m.Unlock()
		msg = string(b[0:read])
	}()

	// setup client log

	pool := x509.NewCertPool()
	pool.AppendCertsFromPEM([]byte(publicKey))

	config := &tls.Config{
		RootCAs: pool,
	}

	sLog, err := New("tcp+tls", "127.0.0.1:2022", "", config)
	if err != nil {
		log.Fatalf("Expected '%v' Got '%v'", nil, err)
	}

	//sLog.SetDisplayColor(true)
	// sLog.SetBuffersAndWorkers(3, 0)
	// sLog.SetTimestampFormat("2006")
	sLog.SetFormatFunc(func(s *Syslog) Formatter {
		return func(e *log.Entry) []byte {
			return []byte(e.Message)
		}
	})

	log.RegisterHandler(sLog, log.AllLevels...)

	log.Debug("debug")
	time.Sleep(500 * time.Millisecond)

	m.Lock()
	defer m.Unlock()

	if msg != "debug" {
		log.Errorf("Expected '%s' Got '%s'", "debug", msg)
	}
}