Results 1 to 4 of 4
  1. #1
    segolas is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default [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:

    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;
    
        }
    Could this be the problem?
    Last edited by segolas; 01-14-2010 at 02:10 PM. Reason: solved

  2. #2
    segolas is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    the solution is to use Log4j

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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

  4. #4
    segolas is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    mmm I think you're right!

    but now I've modify all of my code to use log4j... maybe I'll try tomorrow (right now I'm going to throw my case out of the window!!)

Similar Threads

  1. 3x wrong password will lock the system
    By ashin in forum SWT / JFace
    Replies: 0
    Last Post: 07-11-2009, 04:03 PM
  2. Java Logger ( How to control it better)
    By itaipee in forum New To Java
    Replies: 3
    Last Post: 03-03-2009, 10:35 PM
  3. lock a file
    By flaca in forum New To Java
    Replies: 3
    Last Post: 08-19-2008, 03:00 PM
  4. Help with key Logger in java
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:14 AM
  5. object lock question
    By simon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •