From 5a317038402daf1a35e775dd3e0f7bcb5a96f0f6 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 29 Mar 2025 17:27:52 +0100 Subject: [PATCH] Implement per command logger --- main.go | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index c5ac0f8..fc7f13e 100644 --- a/main.go +++ b/main.go @@ -118,6 +118,30 @@ func main() { startTime := time.Now() var fileMutex sync.Mutex + // Create a map to store loggers for each command + commandLoggers := make(map[string]*logger.Logger) + for _, command := range commands { + // Create a named logger for each command + cmdName := command.Name + if cmdName == "" { + // If no name is provided, use a short version of the regex pattern + if len(command.Regex) > 20 { + cmdName = command.Regex[:17] + "..." + } else { + cmdName = command.Regex + } + } + + // Parse the log level for this specific command + cmdLogLevel := logger.ParseLevel(command.LogLevel) + + // Create a logger with the command name as a field + commandLoggers[command.Name] = logger.WithField("command", cmdName) + commandLoggers[command.Name].SetLevel(cmdLogLevel) + + logger.Debug("Created logger for command %q with log level %s", cmdName, cmdLogLevel.String()) + } + // This aggregation is great but what if one modification replaces the whole entire file? // Shit...... // TODO: Add "Isolate" field to modifications which makes them run alone @@ -144,7 +168,7 @@ func main() { return } - fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex) + fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex, commandLoggers) if err != nil { logger.Error("Failed to run other commands for file %q: %v", file, err) return @@ -230,11 +254,17 @@ func main() { } } -func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, fileMutex *sync.Mutex) (string, error) { +func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, fileMutex *sync.Mutex, commandLoggers map[string]*logger.Logger) (string, error) { // Aggregate all the modifications and execute them modifications := []utils.ReplaceCommand{} for _, command := range association.Commands { - logger.Info("Processing file %q with command %q", file, command.Regex) + // Use command-specific logger if available, otherwise fall back to default logger + cmdLogger := logger.DefaultLogger + if cmdLog, ok := commandLoggers[command.Name]; ok { + cmdLogger = cmdLog + } + + cmdLogger.Info("Processing file %q with command %q", file, command.Regex) newModifications, err := processor.ProcessRegex(fileDataStr, command, file) if err != nil { return fileDataStr, fmt.Errorf("failed to process file %q with command %q: %w", file, command.Regex, err) @@ -248,6 +278,8 @@ func RunOtherCommands(file string, fileDataStr string, association utils.FileCom count = 0 } stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications)) + + cmdLogger.Debug("Command %q generated %d modifications", command.Name, len(newModifications)) } if len(modifications) == 0 {