Reputation: 19
I'm trying to create a Java program that when executed, a log file records the end result. I tried this set of codes:
LogManager lm = LogManager.getLogManager();
Logger logger = Logger.getLogger("NetSuite SQL Log");
lm.addLogger(logger);
logger.setLevel(Level.INFO);
FileHandler fh;
try {
fh = new FileHandler("NetSuiteSQL Sync Log.txt");
lm.addLogger(logger);
logger.setLevel(Level.INFO);
fh.setFormatter(new SimpleFormatter());
logger.addHandler(fh);
logger.log(Level.INFO, "record event");
fh.close();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
and it does record but changes the date and time, like this:
Apr 20, 2015 4:43:38 PM com.netsuite.jdbc.main.Main main
INFO: No new record.
The result I'm hoping for is a log file with all previous and current logs recorded, like this:
Apr 20, 2015 4:43:38 PM com.netsuite.jdbc.main.Main main
INFO: No new record.
Apr 20, 2015 5:04:38 PM com.netsuite.jdbc.main.Main main
INFO: No new record.
Is there a set of codes that can result this kind of logging?
Upvotes: 0
Views: 464
Reputation: 3225
Change this fh = new FileHandler("NetSuiteSQL Sync Log.txt")
to fh = new FileHandler("NetSuiteSQL Sync Log.txt", true)
As per documentation
public FileHandler(String pattern,
boolean append)
Initialize a FileHandler to write to the given filename, with optional append. FileHandler Document
Upvotes: 1
Reputation: 2877
Most likely you are overwriting the file with each restart of you program.
You might want to switch to an appending FileHandler
:
fh = new FileHandler("NetSuiteSQL Sync Log.txt");
Documentation is Here
Upvotes: 0