A class I wrote in charge of logging uses FileWriter to write into a file defined by the developer.
A colleague mentioned that I should check the thread safety of this logger, since it will be used by multi-threaded web frameworks, namely Spring boot
.
The critical section, which writes to file, looks something like this:
public void write(String s) {
FileWriter fw;
try {
fw = new FileWriter(new File(outputFilePath), true);
fw.write(s);
fw.close();
} catch (IOException ex) {
throw ex;
}
}
outputFilePath
is set upon construction. So all threads are writing to the same file concurrently.
The construction of this class uses Factory Method
which looks like this:
Audit auditLogger = LoggingFactory.createAuditLogger(outputfilepath);
I remember from college that 'Appending to tables/files is thread safe', but this claim is too broad to make me feel comfortable about this code.
How do I approach questions of thread safety? And what about this one in particular?
Thanks