Implement per command logger
This commit is contained in:
38
main.go
38
main.go
@@ -118,6 +118,30 @@ func main() {
|
|||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
var fileMutex sync.Mutex
|
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?
|
// This aggregation is great but what if one modification replaces the whole entire file?
|
||||||
// Shit......
|
// Shit......
|
||||||
// TODO: Add "Isolate" field to modifications which makes them run alone
|
// TODO: Add "Isolate" field to modifications which makes them run alone
|
||||||
@@ -144,7 +168,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex)
|
fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex, commandLoggers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to run other commands for file %q: %v", file, err)
|
logger.Error("Failed to run other commands for file %q: %v", file, err)
|
||||||
return
|
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
|
// Aggregate all the modifications and execute them
|
||||||
modifications := []utils.ReplaceCommand{}
|
modifications := []utils.ReplaceCommand{}
|
||||||
for _, command := range association.Commands {
|
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)
|
newModifications, err := processor.ProcessRegex(fileDataStr, command, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fileDataStr, fmt.Errorf("failed to process file %q with command %q: %w", file, command.Regex, err)
|
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
|
count = 0
|
||||||
}
|
}
|
||||||
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications))
|
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications))
|
||||||
|
|
||||||
|
cmdLogger.Debug("Command %q generated %d modifications", command.Name, len(newModifications))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(modifications) == 0 {
|
if len(modifications) == 0 {
|
||||||
|
Reference in New Issue
Block a user