Skip to content

Commit

Permalink
write jsonl and raw data on sr flag
Browse files Browse the repository at this point in the history
  • Loading branch information
RamanaReddy0M committed Sep 21, 2023
1 parent ff3a9d1 commit d96406b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
12 changes: 4 additions & 8 deletions internal/runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,11 @@ func ParseOptions() (*Options, error) {
gologger.Info().Msgf("Current proxify version %v %v", version, updateutils.GetVersionDescription(version, latestVersion))
}
}

// if output directory is not set OR on export mode default to jsonl output
if options.OutputDirectory == "" || options.Elastic.Addr != "" || options.Kafka.Addr != "" || options.OutputFile != "" {
options.OutputJsonl = true
if options.OutputFile == "" {
options.OutputFile = "proxify_logs.jsonl"
}
options.OutputJsonl = true
// if OutputFile is not set, set it to default
if options.OutputFile == "" {
options.OutputFile = "proxify_logs.jsonl"
}

return options, nil
}

Expand Down
19 changes: 13 additions & 6 deletions pkg/logger/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Client struct {
// New creates and returns a new client for file based logging
func New(option *Options) (*Client, error) {
client := &Client{options: option}
if option.OutputFolder != "" && !option.OutputJsonl {
if option.OutputFolder != "" {
if err := fileutil.CreateFolder(option.OutputFolder); err != nil {
return client, err
}
Expand All @@ -43,17 +43,24 @@ func New(option *Options) (*Client, error) {

// Store writes the log to the file
func (c *Client) Save(data types.OutputData) error {
var err error
logFile := fmt.Sprintf("%s.%s", data.Name, "txt")
logFile = filepath.Join(c.options.OutputFolder, logFile)
if c.options.OutputJsonl {
logFile = c.options.OutputFile
if c.options.OutputFolder != "" {
err = c.writeToFile(filepath.Join(c.options.OutputFolder, logFile), string(data.RawData))
}
if c.options.OutputFile != "" {
err = c.writeToFile(c.options.OutputFile, data.DataString)
}
return err
}

func (c *Client) writeToFile(filepath, content string) error {
// if it's a response and file doesn't exist skip
f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(filepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
// write to file
fmt.Fprint(f, data.DataString)
fmt.Fprint(f, content)
return f.Close()
}
8 changes: 5 additions & 3 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (l *Logger) AsyncWrite() {

if l.options.MaxSize > 0 {
outputdata.DataString = stringsutil.Truncate(outputdata.DataString, l.options.MaxSize)
outputdata.RawData = []byte(stringsutil.Truncate(string(outputdata.RawData), l.options.MaxSize))
}

for _, store := range l.Store {
Expand All @@ -149,7 +150,7 @@ func (l *Logger) LogRequest(req *http.Request, userdata types.UserData) error {
l.jsonLogMap.Store(userdata.ID, outputData)
}
if (!l.options.OutputJsonl) && (l.options.OutputFolder != "" || l.options.Kafka.Addr != "" || l.options.Elastic.Addr != "") {
l.asyncqueue <- types.OutputData{Data: reqdump, Userdata: userdata}
l.asyncqueue <- types.OutputData{RawData: reqdump, Userdata: userdata}
}

if l.options.Verbosity >= types.VerbosityVeryVerbose {
Expand Down Expand Up @@ -180,6 +181,7 @@ func (l *Logger) LogResponse(resp *http.Response, userdata types.UserData) error
if err != nil {
return err
}
var data []byte
if l.options.OutputJsonl {
defer l.jsonLogMap.Delete(userdata.ID)
outputData := types.HTTPRequestResponseLog{}
Expand All @@ -194,13 +196,13 @@ func (l *Logger) LogResponse(resp *http.Response, userdata types.UserData) error
if err := fillJsonResponseData(resp, &outputData); err != nil {
return err
}
respdump, err = json.Marshal(outputData)
data, err = json.Marshal(outputData)
if err != nil {
return err
}
}

l.asyncqueue <- types.OutputData{Data: respdump, Userdata: userdata}
l.asyncqueue <- types.OutputData{RawData: respdump, Data: data, Userdata: userdata}

if l.options.Verbosity >= types.VerbosityVeryVerbose {
contentType := resp.Header.Get("Content-Type")
Expand Down
1 change: 1 addition & 0 deletions pkg/types/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type UserData struct {

type OutputData struct {
Userdata UserData
RawData []byte
Data []byte
DataString string
Name string
Expand Down

0 comments on commit d96406b

Please sign in to comment.