-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #378 from projectdiscovery/iceman-add-trace-log
Added trace log feature to write execution log
- Loading branch information
Showing
6 changed files
with
123 additions
and
4 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
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
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,76 @@ | ||
package tracelog | ||
|
||
import ( | ||
"os" | ||
"sync" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
// Log is an interface for logging trace log of all the requests | ||
type Log interface { | ||
// Close closes the log interface flushing data | ||
Close() | ||
// Request writes a log the requests trace log | ||
Request(templateID, url, requestType string, err error) | ||
} | ||
|
||
// NoopLogger is a noop logger that simply does nothing | ||
type NoopLogger struct{} | ||
|
||
// Close closes the log interface flushing data | ||
func (n *NoopLogger) Close() {} | ||
|
||
// Request writes a log the requests trace log | ||
func (n *NoopLogger) Request(templateID, url, requestType string, err error) {} | ||
|
||
// FileLogger is a trace logger that writes request logs to a file. | ||
type FileLogger struct { | ||
encoder *jsoniter.Encoder | ||
file *os.File | ||
mutex *sync.Mutex | ||
} | ||
|
||
// NewFileLogger creates a new file logger structure | ||
func NewFileLogger(path string) (*FileLogger, error) { | ||
file, err := os.Create(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &FileLogger{file: file, encoder: jsoniter.NewEncoder(file), mutex: &sync.Mutex{}}, nil | ||
} | ||
|
||
// Close closes the log interface flushing data | ||
func (f *FileLogger) Close() { | ||
f.mutex.Lock() | ||
defer f.mutex.Unlock() | ||
|
||
f.file.Close() | ||
} | ||
|
||
// JSONRequest is a trace log request written to file | ||
type JSONRequest struct { | ||
ID string `json:"id"` | ||
URL string `json:"url"` | ||
Error string `json:"error"` | ||
Type string `json:"type"` | ||
} | ||
|
||
// Request writes a log the requests trace log | ||
func (f *FileLogger) Request(templateID, url, requestType string, err error) { | ||
request := &JSONRequest{ | ||
ID: templateID, | ||
URL: url, | ||
Type: requestType, | ||
} | ||
if err != nil { | ||
request.Error = err.Error() | ||
} else { | ||
request.Error = "none" | ||
} | ||
|
||
f.mutex.Lock() | ||
defer f.mutex.Unlock() | ||
//nolint:errcheck // We don't need to do anything here | ||
f.encoder.Encode(request) | ||
} |
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