2

I wrote the method to check the working functionality of the logger as follows,

package test;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class test1{
    private static Logger logger = Logger.getLogger(test1.class);
public static void main(String args[])
{
    System.out.println("time 1 "+System.currentTimeMillis());
    logger.log(Level.INFO,"Eror "+System.currentTimeMillis());
    System.out.println("time 2 "+System.currentTimeMillis());
    logger.log(Level.INFO,"Eror "+System.currentTimeMillis());
    logger.log(Level.INFO,"Eror "+System.currentTimeMillis());
    logger.log(Level.INFO,"Eror "+System.currentTimeMillis());
    logger.log(Level.INFO,"Eror "+System.currentTimeMillis());
    logger.log(Level.INFO,"Eror "+System.currentTimeMillis());

    System.out.println("time 3 "+System.currentTimeMillis());
    System.out.println("time 4 "+System.currentTimeMillis());
    System.out.println("time 5 "+System.currentTimeMillis());
}

}

And i got the output as follows,

time 1 1367325027239
time 2 1367325027247
Apr 30, 2013 6:00:27 PM test.test1 main
INFO: Eror 1367325027239
Apr 30, 2013 6:00:27 PM test.test1 main
INFO: Eror 1367325027247
Apr 30, 2013 6:00:27 PM test.test1 main
INFO: Eror 1367325027248
Apr 30, 2013 6:00:27 PM test.test1 main
INFO: Eror 1367325027249
Apr 30, 2013 6:00:27 PM test.test1 main
INFO: Eror 1367325027250
Apr 30, 2013 6:00:27 PM test.test1 main
INFO: Eror 1367325027250
time 3 1367325027251
time 4 1367325027251
time 5 1367325027251

My question is, will the program control waits until the logger completes its process

(i.e writing the message into the log file according to the configuration),

Or logger will execute independently after returning the control to the calling code.

(From the above code it can be seen that logger takes some time , but my thought is that within that time the file operation cannot be completed)

2
  • 1
    That seems to be completely dependent on how that logger is configured. It might well be asynchronous (and also buffered). Commented Apr 30, 2013 at 12:45
  • I havn't configured synchronous or asynchronous in properties file. Commented Apr 30, 2013 at 12:54

1 Answer 1

1

Yes, the logging "blocks" the execution of your programm flow until the logging is completed. You can use visualvm to analyse the number of used Threads with and without a logger, to see if the logger could be implemented asynchronous. Don't get irritated by the deamon threads, which are started every time.

Normaly logger and all other file-writer are using a Buffer to speed up the writing. By writing the Content to the buffer and flushing it later on to the destination, execution time can be saved.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.