summaryrefslogtreecommitdiff
path: root/plugins/logger/std_log_adapter.go
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-03-15 13:43:45 +0300
committerGitHub <[email protected]>2021-03-15 13:43:45 +0300
commit102e473110b9be0924193aeecd4b946a7053737e (patch)
treeb2b0db47390741ff15884ec81b06626e4020fdb5 /plugins/logger/std_log_adapter.go
parentc07594a931bd322fd5e7310357f1ab3f4be9c0c5 (diff)
parentbb6acdaf8cf55a4df746c9d03f2c677fa85ea6ee (diff)
Merge pull request #592 from spiral/missed_logger_for_the_http_server
🆕 new(http plugin): HTTP/HTTPS/FCGI servers errors redirected to the RR logger under the Error log level
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))
+}