0

I'm using java FileHandler to append to a file, and occasionally rotate it. The data consists of JSON strings, one on each line. The file is consumed by another process, and I want the FileHandler to only write the JSON string I pass into the publish method, without any xml metadata. How is this accomplished? I've scoured the internet for information about XMLFormatter and FileHandler, but all I've found are content farm tutorials that barely touch on the basics. Right now, I get the following written to the file:

<record>
  <date>2016-01-06T15:05:54</date>
  <millis>1452121554535</millis>
  <sequence>14</sequence>
  <level>INFO</level>
  <thread>29</thread>
  <message>MyMessage</message>
</record>

And all I want written is MyMessage.

Any help appreciated.

worker1138
  • 2,071
  • 5
  • 29
  • 36

1 Answers1

0

If you don't want to use a custom formatter then you can use the java.util.logging.SimpleFormatter and set the format property to %5$s which will just write out the message part of the log record.

java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format = %5$s

It won't work if you require another SimpleFormatter with a different pattern like a ConsoleHandler or if your raw data trips up MessageFormat patterns.

jmehrens
  • 10,580
  • 1
  • 38
  • 47