Esempio n. 1
0
// HandleLog is the default http log handler
func HandleLog(h HTTP, entries <-chan *log.Entry) {
	var e *log.Entry
	var b []byte
	var reader *bytes.Reader

	formatter := h.FormatFunc()(h)
	remoteHost := h.RemoteHost()
	httpClient := stdhttp.Client{}

	req, _ := stdhttp.NewRequest(h.Method(), remoteHost, nil)
	req.Header = h.Headers()

	for e = range entries {

		b = formatter(e)

		reader = bytes.NewReader(b)
		req.Body = ioutil.NopCloser(reader)
		req.ContentLength = int64(reader.Len())

		resp, err := httpClient.Do(req)
		if err != nil {
			log.Error("Could not post data to %s: %v\n", remoteHost, err)
			goto END
		}

		if resp.StatusCode < 200 || resp.StatusCode >= 299 {
			bt, _ := ioutil.ReadAll(resp.Body)
			log.Error("Received HTTP %d during POST request to %s body: %s\n", resp.StatusCode, remoteHost, string(bt))
		}

	END:
		e.Consumed()
	}
}
Esempio n. 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)
	}
}
Esempio n. 3
0
func TestSetFilenameColor(t *testing.T) {
	buff := new(bytes.Buffer)

	cLog := New()
	cLog.SetWriter(buff)
	cLog.SetDisplayColor(true)
	cLog.SetBuffersAndWorkers(3, 1)
	cLog.SetTimestampFormat("MST")
	cLog.SetFilenameDisplay(log.Llongfile)

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

	log.Error("error")
	if !strings.Contains(buff.String(), "log/handlers/console/console_test.go:270 error") {
		t.Errorf("Expected '%s' Got '%s'", "log/handlers/console/console_test.go:270 error", buff.String())
	}
	buff.Reset()
}
Esempio n. 4
0
func main() {

	cLog := console.New()

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

	// Trace
	defer log.Trace("trace").End()

	log.Debug("debug")
	log.Info("info")
	log.Notice("notice")
	log.Warn("warn")
	log.Error("error")
	// log.Panic("panic") // this will panic
	log.Alert("alert")
	// log.Fatal("fatal") // this will call os.Exit(1)

	// logging with fields can be used with any of the above
	log.WithFields(log.F("key", "value")).Info("test info")
}