summaryrefslogtreecommitdiff
path: root/plugins/logger/std_log_adapter.go
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/logger/std_log_adapter.go')
-rw-r--r--plugins/logger/std_log_adapter.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/plugins/logger/std_log_adapter.go b/plugins/logger/std_log_adapter.go
new file mode 100644
index 00000000..484cc23e
--- /dev/null
+++ b/plugins/logger/std_log_adapter.go
@@ -0,0 +1,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))
+}