-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13b2261
commit 7a03301
Showing
5 changed files
with
185 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package bfflog | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/google/martian/v3" | ||
"github.com/google/martian/v3/parse" | ||
"github.com/imranismail/bff/log" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
// Logger is a modifier that logs requests and responses. | ||
type Logger struct{} | ||
|
||
type loggerJSON struct { | ||
Scope []parse.ModifierType `json:"scope"` | ||
} | ||
|
||
func init() { | ||
parse.Register("log.Logger", loggerFromJSON) | ||
} | ||
|
||
// NewLogger returns a logger that logs requests and responses, optionally | ||
// logging the body. Log function defaults to martian.Infof. | ||
func NewLogger() *Logger { | ||
return &Logger{} | ||
} | ||
|
||
// ModifyRequest logs the request | ||
func (l *Logger) ModifyRequest(req *http.Request) error { | ||
ctx := martian.NewContext(req) | ||
if ctx.SkippingLogging() { | ||
return nil | ||
} | ||
|
||
hdrs := zerolog.Dict() | ||
|
||
for key, val := range req.Header { | ||
hdrs.Str(key, strings.Join(val, ", ")) | ||
} | ||
|
||
log.Logger.Zlog.Info().Str("path", req.URL.Path).Str("method", req.Method).Str("scheme", req.URL.Scheme).Str("host", req.Host).Dict("headers", hdrs).Msg(fmt.Sprintf("Request to %s", req.URL)) | ||
|
||
return nil | ||
} | ||
|
||
// ModifyResponse logs the response | ||
func (l *Logger) ModifyResponse(res *http.Response) error { | ||
ctx := martian.NewContext(res.Request) | ||
if ctx.SkippingLogging() { | ||
return nil | ||
} | ||
|
||
hdrs := zerolog.Dict() | ||
|
||
for key, val := range res.Header { | ||
hdrs.Str(key, strings.Join(val, ", ")) | ||
} | ||
|
||
log.Logger.Zlog.Info().Str("path", res.Request.URL.Path).Str("method", res.Request.Method).Str("scheme", res.Request.URL.Scheme).Str("host", res.Request.Host).Dict("headers", hdrs).Msg(fmt.Sprintf("Response from %s", res.Request.URL)) | ||
|
||
return nil | ||
} | ||
|
||
// loggerFromJSON builds a logger from JSON. | ||
// | ||
// Example JSON: | ||
// { | ||
// "log.Logger": { | ||
// "scope": ["request", "response"] | ||
// } | ||
// } | ||
func loggerFromJSON(b []byte) (*parse.Result, error) { | ||
msg := &loggerJSON{} | ||
if err := json.Unmarshal(b, msg); err != nil { | ||
return nil, err | ||
} | ||
|
||
l := NewLogger() | ||
|
||
return parse.NewResult(l, msg.Scope) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package log | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
mlog "github.com/google/martian/v3/log" | ||
"github.com/rs/zerolog" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var Logger = NewLogger() | ||
|
||
func NewLogger() logger { | ||
return logger{Zlog: zerolog.New(os.Stderr).With().Timestamp().Logger()} | ||
} | ||
|
||
type logger struct { | ||
Zlog zerolog.Logger | ||
} | ||
|
||
func (l *logger) Infof(format string, args ...interface{}) { | ||
l.Zlog.Info().Msgf(format, args...) | ||
} | ||
|
||
func (l *logger) Debugf(format string, args ...interface{}) { | ||
l.Zlog.Debug().Msgf(format, args...) | ||
} | ||
|
||
func (l *logger) Errorf(format string, args ...interface{}) { | ||
l.Zlog.Error().Msgf(format, args...) | ||
} | ||
|
||
func (l *logger) Configure() { | ||
level, err := zerolog.ParseLevel(viper.GetString("verbosity")) | ||
|
||
if err != nil { | ||
l.Zlog.Fatal().Msgf("Failed to parse verbosity: %v", err) | ||
} | ||
|
||
var output io.Writer | ||
|
||
if viper.GetBool("pretty") { | ||
output = zerolog.NewConsoleWriter() | ||
} else { | ||
output = os.Stderr | ||
} | ||
|
||
l.Zlog = l.Zlog.Level(level).Output(output) | ||
mlog.SetLogger(l) | ||
} | ||
|
||
func Infof(format string, args ...interface{}) { | ||
Logger.Infof(format, args...) | ||
} | ||
|
||
func Debugf(format string, args ...interface{}) { | ||
Logger.Debugf(format, args...) | ||
} | ||
|
||
func Errorf(format string, args ...interface{}) { | ||
Logger.Errorf(format, args...) | ||
} | ||
|
||
func Configure() { | ||
Logger.Configure() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters