0

I have a function that save all the error in a errormessage list

public class Util {
    private List<String> errorMessages = new ArrayList<>();

    public void outputResult(String content) {
        logger.error(content);
        errorMessages.add(content);
    }
}

and my compare function add all the error message to the list,

public void compare(Config source, Config target) {
    if (source.getId() != target.getId()) {
        util.outputResult("id not equal");
    }
    // ...
}

And in my main function, I call this compare function and want to save all the error message in a txt or some other file in my current directory

public class MyClass {
    public void main() {
        compare();

        // writeToFile
    }
}

This is what I'm doing right now, I convert ByteArrayOutputStream to a string and print it, there a txt file generated but is empty, and I don't want to a string, I want each error message in the list be printed, how can I do that?

ByteArrayOutputStream errorMessages = new ByteArrayOutputStream();

try (FileWriter w = new FileWriter(pathToReport)) {
    w.write(errorMessages.toString());
}
File errorMessagesFile = new File(pathToReport);

errorMessagesFile.writeText(errorMessages.toString());
EXEC1
  • 39
  • 4

1 Answers1

0

What logger library that you are using? If you use sl4j, you can couple it with log4j by configuring properly to just log the error messages into the file that you have specified in the configuration. I've done some lookup and I find this stackoverflow: where-does-the-slf4j-log-file-get-saved answer provided a template for you to follow on with this setup.