2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-07-24 18:11:17 +00:00
|
|
|
"net"
|
2019-07-20 10:26:52 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-05-06 17:36:34 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/metric"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// StructuredLogger defines a simple wrapper around zerolog logger.
|
|
|
|
// It implements chi.middleware.LogFormatter interface
|
2019-07-20 10:26:52 +00:00
|
|
|
type StructuredLogger struct {
|
|
|
|
Logger *zerolog.Logger
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// StructuredLoggerEntry defines a log entry.
|
|
|
|
// It implements chi.middleware.LogEntry interface
|
2019-07-20 10:26:52 +00:00
|
|
|
type StructuredLoggerEntry struct {
|
2019-07-30 18:51:29 +00:00
|
|
|
// The zerolog logger
|
2019-07-20 10:26:52 +00:00
|
|
|
Logger *zerolog.Logger
|
2019-07-30 18:51:29 +00:00
|
|
|
// fields to write in the log
|
2022-05-19 17:49:51 +00:00
|
|
|
fields map[string]any
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// NewStructuredLogger returns a chi.middleware.RequestLogger using our StructuredLogger.
|
|
|
|
// This structured logger is called by the chi.middleware.Logger handler to log each HTTP request
|
2019-07-20 10:26:52 +00:00
|
|
|
func NewStructuredLogger(logger *zerolog.Logger) func(next http.Handler) http.Handler {
|
|
|
|
return middleware.RequestLogger(&StructuredLogger{logger})
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// NewLogEntry creates a new log entry for an HTTP request
|
2019-07-20 10:26:52 +00:00
|
|
|
func (l *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry {
|
|
|
|
scheme := "http"
|
|
|
|
if r.TLS != nil {
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
2022-05-19 17:49:51 +00:00
|
|
|
fields := map[string]any{
|
2021-07-24 18:11:17 +00:00
|
|
|
"local_addr": getLocalAddress(r),
|
2019-07-20 10:26:52 +00:00
|
|
|
"remote_addr": r.RemoteAddr,
|
|
|
|
"proto": r.Proto,
|
|
|
|
"method": r.Method,
|
|
|
|
"user_agent": r.UserAgent(),
|
|
|
|
"uri": fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)}
|
|
|
|
|
|
|
|
reqID := middleware.GetReqID(r.Context())
|
|
|
|
if reqID != "" {
|
|
|
|
fields["request_id"] = reqID
|
|
|
|
}
|
|
|
|
|
|
|
|
return &StructuredLoggerEntry{Logger: l.Logger, fields: fields}
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Write logs a new entry at the end of the HTTP request
|
2023-03-22 18:02:54 +00:00
|
|
|
func (l *StructuredLoggerEntry) Write(status, bytes int, _ http.Header, elapsed time.Duration, _ any) {
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.HTTPRequestServed(status)
|
2023-08-20 17:01:16 +00:00
|
|
|
var ev *zerolog.Event
|
|
|
|
if status >= http.StatusInternalServerError {
|
|
|
|
ev = l.Logger.Error()
|
|
|
|
} else if status >= http.StatusBadRequest {
|
|
|
|
ev = l.Logger.Warn()
|
|
|
|
} else {
|
|
|
|
ev = l.Logger.Debug()
|
|
|
|
}
|
|
|
|
ev.
|
2019-11-11 14:20:00 +00:00
|
|
|
Timestamp().
|
|
|
|
Str("sender", "httpd").
|
|
|
|
Fields(l.fields).
|
|
|
|
Int("resp_status", status).
|
|
|
|
Int("resp_size", bytes).
|
|
|
|
Int64("elapsed_ms", elapsed.Nanoseconds()/1000000).
|
2020-10-29 18:23:33 +00:00
|
|
|
Send()
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Panic logs panics
|
2022-05-19 17:49:51 +00:00
|
|
|
func (l *StructuredLoggerEntry) Panic(v any, stack []byte) {
|
2019-11-11 14:20:00 +00:00
|
|
|
l.Logger.Error().
|
|
|
|
Timestamp().
|
|
|
|
Str("sender", "httpd").
|
|
|
|
Fields(l.fields).
|
|
|
|
Str("stack", string(stack)).
|
|
|
|
Str("panic", fmt.Sprintf("%+v", v)).
|
2020-10-29 18:23:33 +00:00
|
|
|
Send()
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2021-07-24 18:11:17 +00:00
|
|
|
|
|
|
|
func getLocalAddress(r *http.Request) string {
|
|
|
|
if r == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
localAddr, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr)
|
|
|
|
if ok {
|
|
|
|
return localAddr.String()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|