Skip to content

Commit

Permalink
feat: add network logger
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Jun 23, 2022
1 parent 5fa77f8 commit 07083cf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/murphysecurity/murphysec/utils/must"
"github.com/murphysecurity/murphysec/version"
"github.com/pkg/errors"
"go.uber.org/zap"
"io"
"mime"
"net/http"
"net/http/httputil"
"net/url"
"os"
"reflect"
Expand All @@ -32,6 +34,31 @@ var machineId = version.MachineId()

var C *Client

type _LoggingMiddleware struct {
Transport http.RoundTripper
}

func (t *_LoggingMiddleware) RoundTrip(request *http.Request) (resp *http.Response, e error) {
var dump []byte
dump, e = httputil.DumpRequestOut(request, true)
if e != nil {
logger.Logger.Desugar().Error("dump request out failed", zap.Error(e))
} else {
logger.Logger.Desugar().Debug("http request", zap.ByteString("dump", dump))
}
resp, e = t.Transport.RoundTrip(request)
if e != nil {
return
}
dump, e = httputil.DumpResponse(resp, true)
if e != nil {
logger.Logger.Desugar().Error("dump response out failed", zap.Error(e))
} else {
logger.Logger.Desugar().Debug("http response", zap.ByteString("dump", dump))
}
return
}

type Client struct {
client *http.Client
baseUrl string
Expand All @@ -48,6 +75,9 @@ func NewClient(baseUrl string) *Client {
c.Timeout = time.Duration(int64(time.Second) * int64(i))
}
cl := &Client{client: c, baseUrl: baseUrl}
if logger.NetworkLog {
cl.client.Transport = &_LoggingMiddleware{Transport: http.DefaultTransport}
}
return cl
}

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func rootCmd() *cobra.Command {
c.PersistentFlags().BoolVar(&logger.DisableLogFile, "no-log-file", false, "do not write log file")
c.PersistentFlags().StringVar(&logger.CliLogFilePathOverride, "write-log-to", "", "specify log file path")
c.PersistentFlags().StringVar(&logger.ConsoleLogLevelOverride, "log-level", "silent", "specify log level, must be silent|error|warn|info|debug")
c.PersistentFlags().BoolVar(&logger.NetworkLog, "network-log", false, "print network data")
c.PersistentFlags().StringVar(&conf.APITokenCliOverride, "token", "", "specify API token")
c.PersistentFlags().StringVar(&CliServerAddressOverride, "server", "", "specify server address")
c.PersistentFlags().String("ide", "", "hidden")
Expand Down
1 change: 1 addition & 0 deletions logger/log_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

var CliLogFilePathOverride string
var DisableLogFile bool
var NetworkLog bool

var defaultLogFile = filepath.Join(must.A(homedir.Dir()), ".murphysec", "logs", fmt.Sprintf("%d.log", time.Now().UnixMilli()))

Expand Down

0 comments on commit 07083cf

Please sign in to comment.