Beispiel #1
0
// HandleLog implements log.Handler.
func (h *Handler) HandleLog(e *log.Entry) error {
	h.mu.Lock()
	defer h.mu.Unlock()

	if h.batch == nil {
		h.batch = &batch.Batch{
			Elastic: h.Client,
			Index:   index(),
			Type:    "log",
		}
	}

	// append system info
	e.WithFields(log.Fields{
		"host": hostname,
		"pid":  pid,
	})

	h.batch.Add(e)

	if h.batch.Size() >= h.BufferSize {
		go h.flush(h.batch)
		h.batch = nil
	}

	return nil
}
Beispiel #2
0
// HandleLog implements log.Handler.
func (h *Handler) HandleLog(e *log.Entry) error {
	h.mu.Lock()
	defer h.mu.Unlock()

	if h.batch == nil {
		h.batch = &batch.Batch{
			Elastic: h.Client,
			Index:   h.indexName(),
			Type:    "log",
		}
	}

	// Map fields
	for k, v := range e.Fields {
		switch t := v.(type) {
		case []byte: // Convert []byte to HEX-string
			e.Fields[k] = fmt.Sprintf("%X", t)
		}
	}

	e.Timestamp = e.Timestamp.UTC()

	if h.Hostname != "" {
		e.Fields["hostname"] = h.Hostname
	}

	h.batch.Add(e)

	if h.batch.Size() >= h.BufferSize {
		go h.flush(h.batch)
		h.batch = nil
	}

	return nil
}