summaryrefslogtreecommitdiff
path: root/plugins/logger
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-03-15 13:43:45 +0300
committerValery Piashchynski <[email protected]>2021-03-15 13:54:36 +0300
commit2d52428ac2ecafb84731e49a51bff2502b924585 (patch)
treeb2b0db47390741ff15884ec81b06626e4020fdb5 /plugins/logger
parent1ebe7fe688f0e7d80aa57c2a45b5b202f540ddd2 (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')
-rw-r--r--plugins/logger/interface.go16
-rw-r--r--plugins/logger/std_log_adapter.go31
2 files changed, 38 insertions, 9 deletions
diff --git a/plugins/logger/interface.go b/plugins/logger/interface.go
index 876629a9..5bb2143b 100644
--- a/plugins/logger/interface.go
+++ b/plugins/logger/interface.go
@@ -1,14 +1,12 @@
package logger
-type (
- // Logger is an general RR log interface
- Logger interface {
- Debug(msg string, keyvals ...interface{})
- Info(msg string, keyvals ...interface{})
- Warn(msg string, keyvals ...interface{})
- Error(msg string, keyvals ...interface{})
- }
-)
+// Logger is an general RR log interface
+type Logger interface {
+ Debug(msg string, keyvals ...interface{})
+ Info(msg string, keyvals ...interface{})
+ Warn(msg string, keyvals ...interface{})
+ Error(msg string, keyvals ...interface{})
+}
// With creates a child logger and adds structured context to it
type WithLogger interface {
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))
+}