Results 1 to 4 of 4
Thread: Logger and file lock
- 01-14-2010, 11:44 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 7
- Rep Power
- 0
[solved] Logger and file lock
Hi again,
I have a problem with the Logger object.
I have a single file "log.txt" and many classes write to it via a logger object. I notice that if the log file is locked, than the application create a new log.txt.1 file and write to it.
Why the new "writer" doesn't wait until the lock is gone? It would be better to have a single log file instead of many.
Another issue is that in a big for loop I have lots of objects writing the log. So when the app reach log.txt.99 an exception i raised (java.io.IOException: Couldn't get lock for log.txt).
In the API I didn't found any way for synchronizing the log, do I have to do it by myself??
Just a note I think may be useful, for avoiding repeate the same code for retrieving a new Logger in every class, I created another class "LoadDefault" with this method:
Could this be the problem?Java Code:public Logger getLogger(String className) { Logger newLogger = Logger.getLogger(className); FileHandler fh = null; try { // append to the default log file fh = new FileHandler("log.txt", true); fh.setFormatter(new SimpleFormatter()); } catch (IOException ex) { Logger.getLogger(MailManager.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(MailManager.class.getName()).log(Level.SEVERE, null, ex); } newLogger.addHandler(fh); newLogger.info("##########################################################"); return newLogger; }Last edited by segolas; 01-14-2010 at 02:10 PM. Reason: solved
- 01-14-2010, 02:10 PM #2
Member
- Join Date
- Jan 2010
- Posts
- 7
- Rep Power
- 0
the solution is to use Log4j
- 01-14-2010, 03:05 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
I would agree that the Log4j.jar is a much better for logging and would recommend using that instead.
But with regards to your current code try making the FileHandler a variable outside the method so you can use the same instance of it for each logger
- 01-14-2010, 04:02 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
3x wrong password will lock the system
By ashin in forum SWT / JFaceReplies: 0Last Post: 07-11-2009, 04:03 PM -
Java Logger ( How to control it better)
By itaipee in forum New To JavaReplies: 3Last Post: 03-03-2009, 10:35 PM -
lock a file
By flaca in forum New To JavaReplies: 3Last Post: 08-19-2008, 03:00 PM -
Help with key Logger in java
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:14 AM -
object lock question
By simon in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks