• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating Log file

 
Simon Knight
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - I'm trying to log actions from an application using the code below:



I'm having a problem though... I want the code to ADD text to the file each time the method is called, but it is overwriting the file instead. How can I get it to add a line rather than overwrite, without loading in the entire file and re-persisting it each time?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Simon,

How can I get it to add a line rather than overwrite, without loading in the entire file and re-persisting it each time?


The short answer would be: Use one of the append() methods of FileWriter instead of write()

But I would recommend you to take a look at the Apache log4j project! This gives you a flexible and configurable logger out of the box. With it you can built hierarchical loggers and configure it to do some logging to standard output or to a file or whatever you like.

Apache log4j

Another alternative is the logger from the JDK package "java.util.logging" which is already included in modern JDKs.


Marco
 
Simon Knight
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm - noted, but that would probably mean making a lot of changes to my code... It seems to me that I already have a method that works, other than the fact that it overwrites my log file, instead of appending the next line to the end of it. I have changed my filewrite method to append() instead of write, but it still blanks the file each time. Is there any easy way of rectifying this?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I think I should have looked more carefully at your code

Try using this overloaded constructor of the FileWriter class:

If the second argument is "true" the given file is opened for appending. This should work as you expect...

Anyway if all parts of your code regarding logging use your writeAudit() method it wouldn't be very hard to encapsulate one of my proposed you loggers within this method. Eventually this will be the easier way.
[ March 16, 2008: Message edited by: Marco Ehrentreich ]
 
Simon Knight
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And likewise I should check the API more thoroughly ;-)

Thanks for your help - I'll bear the loggers proper in mind for next time!

Simon.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And likewise I should check the API more thoroughly ;-)

This sounds like a good idea But no problem at all. Sometimes asking someone is the faster way...

And I forgot to mention that my first answer was bullshit. In fact it doesn't matter if you use the append() or write() methods of the FileWriter class as long as you open the file for appending! Perhaps I should think more thoroughly before answering questions here

Marco
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marco Ehrentreich:
Perhaps I should think more thoroughly before answering questions here
Well, yes, I don't disagree with that. On the other hand you learned something, too, and that's not a bad thing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic