Add logger

This commit is contained in:
David Stotijn
2022-02-27 14:28:28 +01:00
parent d2858a2be4
commit 2ddf2a77e8
7 changed files with 271 additions and 57 deletions

21
pkg/db/badger/logger.go Normal file
View File

@ -0,0 +1,21 @@
package badger
import (
"github.com/dgraph-io/badger/v3"
"go.uber.org/zap"
)
// Interface guard.
var _ badger.Logger = (*Logger)(nil)
type Logger struct {
*zap.SugaredLogger
}
func NewLogger(l *zap.SugaredLogger) *Logger {
return &Logger{l}
}
func (l *Logger) Warningf(template string, args ...interface{}) {
l.Warnf(template, args)
}

87
pkg/log/log.go Normal file
View File

@ -0,0 +1,87 @@
package log
import (
"fmt"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Logger interface {
Debugw(msg string, v ...interface{})
Infow(msg string, v ...interface{})
Errorw(msg string, v ...interface{})
}
func NewZapLogger(debug, pretty bool) (*zap.Logger, error) {
var config zap.Config
if debug {
config = zap.Config{
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Development: debug,
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
} else {
config = zap.NewProductionConfig()
}
if pretty {
config.Encoding = "console"
config.EncoderConfig = zapcore.EncoderConfig{
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: zapcore.OmitKey,
FunctionKey: zapcore.OmitKey,
MessageKey: "M",
StacktraceKey: zapcore.OmitKey,
ConsoleSeparator: " ",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalColorLevelEncoder,
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006/01/02 15:04:05"))
},
EncodeName: func(loggerName string, enc zapcore.PrimitiveArrayEncoder) {
// Print logger name in cyan (ANSI code 36).
enc.AppendString(fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(36), "["+loggerName+"]"))
},
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
if debug {
config.EncoderConfig.CallerKey = "C"
config.EncoderConfig.StacktraceKey = "S"
}
}
return config.Build()
}
type NopLogger struct{}
func (nop NopLogger) Debugw(_ string, _ ...interface{}) {}
func (nop NopLogger) Infow(_ string, _ ...interface{}) {}
func (nop NopLogger) Errorw(_ string, _ ...interface{}) {}
func NewNopLogger() NopLogger {
return NopLogger{}
}

View File

@ -7,10 +7,11 @@ import (
"crypto/x509"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"github.com/dstotijn/hetty/pkg/log"
)
type contextKey int
@ -22,15 +23,22 @@ const ReqLogIDKey contextKey = 0
type Proxy struct {
certConfig *CertConfig
handler http.Handler
logger log.Logger
// TODO: Add mutex for modifier funcs.
reqModifiers []RequestModifyMiddleware
resModifiers []ResponseModifyMiddleware
}
type Config struct {
CACert *x509.Certificate
CAKey crypto.PrivateKey
Logger log.Logger
}
// NewProxy returns a new Proxy.
func NewProxy(ca *x509.Certificate, key crypto.PrivateKey) (*Proxy, error) {
certConfig, err := NewCertConfig(ca, key)
func NewProxy(cfg Config) (*Proxy, error) {
certConfig, err := NewCertConfig(cfg.CACert, cfg.CAKey)
if err != nil {
return nil, err
}
@ -39,12 +47,17 @@ func NewProxy(ca *x509.Certificate, key crypto.PrivateKey) (*Proxy, error) {
certConfig: certConfig,
reqModifiers: make([]RequestModifyMiddleware, 0),
resModifiers: make([]ResponseModifyMiddleware, 0),
logger: cfg.Logger,
}
if p.logger == nil {
p.logger = log.NewNopLogger()
}
p.handler = &httputil.ReverseProxy{
Director: p.modifyRequest,
ModifyResponse: p.modifyResponse,
ErrorHandler: errorHandler,
ErrorHandler: p.errorHandler,
}
return p, nil
@ -103,7 +116,8 @@ func (p *Proxy) modifyResponse(res *http.Response) error {
func (p *Proxy) handleConnect(w http.ResponseWriter) {
hj, ok := w.(http.Hijacker)
if !ok {
log.Printf("[ERROR] handleConnect: ResponseWriter is not a http.Hijacker (type: %T)", w)
p.logger.Errorw("ResponseWriter is not a http.Hijacker.",
"type", fmt.Sprintf("%T", w))
writeError(w, http.StatusServiceUnavailable)
return
@ -113,7 +127,8 @@ func (p *Proxy) handleConnect(w http.ResponseWriter) {
clientConn, _, err := hj.Hijack()
if err != nil {
log.Printf("[ERROR] Hijacking client connection failed: %v", err)
p.logger.Errorw("Hijacking client connection failed.",
"error", err)
writeError(w, http.StatusServiceUnavailable)
return
@ -121,18 +136,21 @@ func (p *Proxy) handleConnect(w http.ResponseWriter) {
defer clientConn.Close()
// Secure connection to client.
clientConn, err = p.clientTLSConn(clientConn)
tlsConn, err := p.clientTLSConn(clientConn)
if err != nil {
log.Printf("[ERROR] Securing client connection failed: %v", err)
p.logger.Errorw("Securing client connection failed.",
"error", err,
"remoteAddr", clientConn.RemoteAddr().String())
return
}
clientConnNotify := ConnNotify{clientConn, make(chan struct{})}
clientConnNotify := ConnNotify{tlsConn, make(chan struct{})}
l := &OnceAcceptListener{clientConnNotify.Conn}
err = http.Serve(l, p)
if err != nil && !errors.Is(err, ErrAlreadyAccepted) {
log.Printf("[ERROR] Serving HTTP request failed: %v", err)
p.logger.Errorw("Serving HTTP request failed.",
"error", err)
}
<-clientConnNotify.closed
@ -150,12 +168,13 @@ func (p *Proxy) clientTLSConn(conn net.Conn) (*tls.Conn, error) {
return tlsConn, nil
}
func errorHandler(w http.ResponseWriter, r *http.Request, err error) {
func (p *Proxy) errorHandler(w http.ResponseWriter, r *http.Request, err error) {
if errors.Is(err, context.Canceled) {
return
}
log.Printf("[ERROR]: Proxy error: %v", err)
p.logger.Errorw("Failed to proxy request.",
"error", err)
w.WriteHeader(http.StatusBadGateway)
}

View File

@ -8,7 +8,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
@ -16,6 +15,7 @@ import (
"github.com/oklog/ulid"
"github.com/dstotijn/hetty/pkg/log"
"github.com/dstotijn/hetty/pkg/proxy"
"github.com/dstotijn/hetty/pkg/scope"
"github.com/dstotijn/hetty/pkg/search"
@ -74,6 +74,7 @@ type service struct {
activeProjectID ulid.ULID
scope *scope.Scope
repo Repository
logger log.Logger
}
type FindRequestsFilter struct {
@ -85,13 +86,21 @@ type FindRequestsFilter struct {
type Config struct {
Scope *scope.Scope
Repository Repository
Logger log.Logger
}
func NewService(cfg Config) Service {
return &service{
repo: cfg.Repository,
scope: cfg.Scope,
s := &service{
repo: cfg.Repository,
scope: cfg.Scope,
logger: cfg.Logger,
}
if s.logger == nil {
s.logger = log.NewNopLogger()
}
return s
}
func (svc *service) FindRequests(ctx context.Context) ([]RequestLog, error) {
@ -129,7 +138,8 @@ func (svc *service) RequestModifier(next proxy.RequestModifyFunc) proxy.RequestM
body, err = ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("[ERROR] Could not read request body for logging: %v", err)
svc.logger.Errorw("Failed to read request body for logging.",
"error", err)
return
}
@ -142,6 +152,9 @@ func (svc *service) RequestModifier(next proxy.RequestModifyFunc) proxy.RequestM
ctx := context.WithValue(req.Context(), LogBypassedKey, true)
*req = *req.WithContext(ctx)
svc.logger.Debugw("Bypassed logging: no active project.",
"url", req.URL.String())
return
}
@ -151,6 +164,9 @@ func (svc *service) RequestModifier(next proxy.RequestModifyFunc) proxy.RequestM
ctx := context.WithValue(req.Context(), LogBypassedKey, true)
*req = *req.WithContext(ctx)
svc.logger.Debugw("Bypassed logging: request doesn't match any scope rules.",
"url", req.URL.String())
return
}
@ -166,10 +182,15 @@ func (svc *service) RequestModifier(next proxy.RequestModifyFunc) proxy.RequestM
err := svc.repo.StoreRequestLog(req.Context(), reqLog)
if err != nil {
log.Printf("[ERROR] Could not store request log: %v", err)
svc.logger.Errorw("Failed to store request log.",
"error", err)
return
}
svc.logger.Debugw("Stored request log.",
"reqLogID", reqLog.ID.String(),
"url", reqLog.URL.String())
ctx := context.WithValue(req.Context(), proxy.ReqLogIDKey, reqLog.ID)
*req = *req.WithContext(ctx)
}
@ -203,7 +224,11 @@ func (svc *service) ResponseModifier(next proxy.ResponseModifyFunc) proxy.Respon
go func() {
if err := svc.storeResponse(context.Background(), reqLogID, &clone); err != nil {
log.Printf("[ERROR] Could not store response log: %v", err)
svc.logger.Errorw("Failed to store response log.",
"error", err)
} else {
svc.logger.Debugw("Stored response log.",
"reqLogID", reqLogID.String())
}
}()