summaryrefslogtreecommitdiff
path: root/plugins/logger/std_log_adapter.go
blob: 484cc23e88b0fa8a90bcc48a534d03b9acd637fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package logger

import (
	"unsafe"
)

// StdLogAdapter can be passed to the http.Server or any place which required standard logger to redirect output
// to the logger plugin
type StdLogAdapter struct {
	log Logger
}

// Write io.Writer interface implementation
func (s *StdLogAdapter) Write(p []byte) (n int, err error) {
	s.log.Error("server internal error", "message", toString(p))
	return len(p), nil
}

// NewStdAdapter constructs StdLogAdapter
func NewStdAdapter(log Logger) *StdLogAdapter {
	logAdapter := &StdLogAdapter{
		log: log,
	}

	return logAdapter
}

// unsafe, but lightning fast []byte to string conversion
func toString(data []byte) string {
	return *(*string)(unsafe.Pointer(&data))
}