Aravindhan
Aravindhan

Reputation: 3626

Java logger executing method

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)

Upvotes: 2

Views: 687

Answers (1)

Simulant
Simulant

Reputation: 20112

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.

Upvotes: 1

Related Questions