Niek de Klein Niek de Klein - 1 year ago 68
Java Question

How to log plain text in Java? SimpleFormatter also writes XML

I am writing logfiles using SimpleFormatter. These are written in XML instead of plain text. The answers I found for writing it as plain text all only say to use the simple formatter (e.g. This is my custom record formatter, which extends from SimpleFormatter:

class CustomRecordFormatter extends SimpleFormatter {
public String format(final LogRecord r) {
StringBuilder sb = new StringBuilder();
if (null != r.getThrown()) {
sb.append("Throwable occurred: "); //$NON-NLS-1$
Throwable t = r.getThrown();
PrintWriter pw = null;
try {
StringWriter sw = new StringWriter();
pw = new PrintWriter(sw);
} finally {
if (pw != null) {
try {
} catch (Exception e) {
// ignore
return sb.toString();

and this is how I set up the logger

public class DeconvolutionLogger {
static private FileHandler outfilePath;
protected final static Logger log = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
static public void setup(String outputDir) throws IOException {
// get the global logger to configure it
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
setOutfilePath(new FileHandler(outputDir+"/DeconvolutionLog_"+dateFormat.format(date)+".txt"));
CustomRecordFormatter customFormatter = new CustomRecordFormatter();
ConsoleHandler consoleHandler = new ConsoleHandler();


What am I missing that it does not write to plain text?

Answer Source

The formatter of outfilePath is stil XMLFormatter.
You should call outfilePath.setFormatter(customFormatter); to change it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download