Skip to content

Commit 54f22fc

Browse files
committed
updated enum subcommand to produce structured logs
1 parent b857f3e commit 54f22fc

File tree

2 files changed

+44
-61
lines changed

2 files changed

+44
-61
lines changed

cmd/amass/enum.go

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,7 @@ func runEnumCommand(clArgs []string) {
146146
createOutputDirectory(cfg)
147147

148148
dir := config.OutputDirectory(cfg.Dir)
149-
// Setup logging
150-
var logfile string
151-
if args.Filepaths.LogFile != "" {
152-
logfile = args.Filepaths.LogFile
153-
}
154-
l := selectLogger(dir, logfile)
155-
149+
l := selectLogger(dir, args.Filepaths.LogFile)
156150
// Create the client that will provide a connection to the engine
157151
url := "http://localhost:4000/graphql"
158152
if cfg.EngineAPI != nil && cfg.EngineAPI.URL != "" {
@@ -226,6 +220,47 @@ loop:
226220
progress.Finish()
227221
}
228222

223+
func writeLogMessage(l *slog.Logger, message string) {
224+
logstr := channelJSONLog(message)
225+
if logstr == "" {
226+
return
227+
}
228+
229+
record, err := format.JSONLogToRecord(logstr)
230+
if err != nil {
231+
return
232+
}
233+
234+
ctx := context.Background()
235+
if l.Handler().Enabled(ctx, record.Level) {
236+
l.Handler().Handle(ctx, record)
237+
}
238+
}
239+
240+
func channelJSONLog(data string) string {
241+
j := make(map[string]interface{})
242+
if err := json.Unmarshal([]byte(data), &j); err != nil {
243+
return ""
244+
}
245+
246+
var logstr string
247+
// check that this is a log message sent over the sub channel
248+
if p, found := j["payload"]; !found {
249+
return ""
250+
} else if pmap, ok := p.(map[string]interface{}); !ok {
251+
return ""
252+
} else if d, found := pmap["data"]; !found {
253+
return ""
254+
} else if dmap, ok := d.(map[string]interface{}); !ok {
255+
return ""
256+
} else if lm, found := dmap["logMessages"]; !found {
257+
return ""
258+
} else if lmstr, ok := lm.(string); ok {
259+
logstr = lmstr
260+
}
261+
return logstr
262+
}
263+
229264
func argsAndConfig(clArgs []string) (*config.Config, *enumArgs) {
230265
args := enumArgs{
231266
AltWordList: stringset.New(),
@@ -316,59 +351,6 @@ func argsAndConfig(clArgs []string) (*config.Config, *enumArgs) {
316351
return cfg, &args
317352
}
318353

319-
func writeLogMessage(l *slog.Logger, logstr string) {
320-
j := make(map[string]interface{})
321-
if err := json.Unmarshal([]byte(logstr), &j); err != nil {
322-
return
323-
}
324-
// check that this is a log message sent over the subscription channel
325-
if p, found := j["payload"]; !found {
326-
return
327-
} else if pmap, ok := p.(map[string]interface{}); !ok {
328-
return
329-
} else if d, found := pmap["data"]; !found {
330-
return
331-
} else if dmap, ok := d.(map[string]interface{}); !ok {
332-
return
333-
} else if lm, found := dmap["logMessages"]; !found {
334-
return
335-
} else if lmmap, ok := lm.(map[string]interface{}); !ok {
336-
return
337-
} else {
338-
// set our json map to the log message within the channel data
339-
j = lmmap
340-
}
341-
delete(j, slog.TimeKey)
342-
delete(j, slog.SourceKey)
343-
344-
var level slog.Level
345-
if val, found := j[slog.LevelKey]; !found {
346-
return
347-
} else if str, ok := val.(string); !ok {
348-
return
349-
} else if level.UnmarshalText([]byte(str)) != nil {
350-
return
351-
}
352-
delete(j, slog.LevelKey)
353-
354-
var msg string
355-
if val, found := j[slog.MessageKey]; !found {
356-
return
357-
} else if str, ok := val.(string); !ok {
358-
return
359-
} else {
360-
msg = str
361-
}
362-
delete(j, slog.MessageKey)
363-
364-
var pc uintptr
365-
ctx := context.Background()
366-
r := slog.NewRecord(time.Now(), level, msg, pc)
367-
if l.Handler().Enabled(ctx, level) {
368-
l.Handler().Handle(ctx, r)
369-
}
370-
}
371-
372354
// Obtain parameters from provided input files
373355
func processEnumInputFiles(args *enumArgs) error {
374356
getList := func(fp []string, name string, s *stringset.Set) error {

cmd/amass/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ func setupFileLogger(dir, logfile string) *slog.Logger {
334334

335335
f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
336336
if err != nil {
337-
fmt.Fprintf(os.Stderr, "Failed to open log file: %v", err)
337+
fmt.Fprintf(os.Stderr, "Failed to open the log file: %v", err)
338+
return nil
338339
}
339340

340341
return slog.New(slog.NewJSONHandler(f, nil))

0 commit comments

Comments
 (0)