nuclei/v2/internal/tracelog/tracelog.go

77 lines
1.8 KiB
Go
Raw Normal View History

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
2020-10-22 10:50:34 +00:00
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
2020-10-22 10:50:34 +00:00
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"`
2020-10-22 10:46:33 +00:00
Type string `json:"type"`
}
// Request writes a log the requests trace log
2020-10-22 10:50:34 +00:00
func (f *FileLogger) Request(templateID, url, requestType string, err error) {
request := &JSONRequest{
2020-10-22 10:46:33 +00:00
ID: templateID,
URL: url,
2020-10-22 10:50:34 +00:00
Type: requestType,
}
if err != nil {
request.Error = err.Error()
} else {
request.Error = "none"
}
f.mutex.Lock()
defer f.mutex.Unlock()
2020-10-22 10:50:34 +00:00
//nolint:errcheck // We don't need to do anything here
f.encoder.Encode(request)
}