views:

653

answers:

2

Hi,

I have an application that scans a set of files. And this scanning take place parallely. I mean if I upload 5 files, five process will be created and start executing the job parallely. Now For logging the exceptions for the jobs running, there is only one log file. The problem that is coming is, the logger creates a lot of logs files as -

mylogfile.log(actual log file)

mylogfile.log.1

mylogfile.log.2

mylogfile.log.3

mylogfile.log.4

mylogfile.log.5
...

with their respective lock files. Below is the code snippet that I used:

  public static Logger getLogger(final String className) {
 final Logger logger = Logger.getLogger(className);
 FileHandler fh;
 try {
  // This block configure the logger with handler and formatter
  fh = new FileHandler("mylogfile.log", true);
  logger.addHandler(fh);
  logger.setLevel(Level.ALL);
  logger.setUseParentHandlers(false);
  fh.setFormatter(new SimpleFormatter());
 } catch (final SecurityException e) {
  //   }
 } catch (final IOException e) {
  //
 }
 return logger;
}

How do I make sure that only one log file should be used to write the exception of all the jobs running parallely..??

Thanks,

Anish

A: 

This does not have much to do with parallel calls. As well from single thread with each call to your getLogger (even with same string as argument) you create a new FileHandler and add it to existing logger.Maybe you do not want to do all of the setup each time when you call your getLogger-method.

//now we will create new Filehandler and set it to logger
getLogger("identifier").log(Level.SEVERE, "Log to   one file");
//now we have already one filehandler, but lets add one more
getLogger("identifier").log(Level.SEVERE, "Log to   two files");
//now we have already two filehandlers, but lets add one more
getLogger("identifier").log(Level.SEVERE, "Log to three files");

Configure logging once and after that you can get reference to logger via java.util.loggingLogger.getLogger. Overview from Sun gives more ideas how to configure logging via properties files.

Mikko Maunu
A: 

You can use a singlton locator class that you can retrieve the logger from, that would force your application to use one logger (which means you'll have one log file)

class LoggerLocator {

    private static LoggerLocator locator = new LoggerLocator();
    private Logger logger;

    /**
     * 
     */
    private LoggerLocator() {
     initLogger();
    }

    /**
     * @return
     */
    public static LoggerLocator getLocator(){
     return locator;
    }

    /**
     * @return
     */
    public Logger getLogger() {
     return logger;
    }

    /**
     * 
     */
    private void initLogger() {
     logger = Logger.getLogger("My General Logger");
     FileHandler fh;
     try {
      // This block configure the logger with handler and formatter
      fh = new FileHandler("mylogfile.log", true);
      logger.addHandler(fh);
      logger.setLevel(Level.ALL);
      logger.setUseParentHandlers(false);
      fh.setFormatter(new SimpleFormatter());
     } catch (final SecurityException e) {
      // }
     } catch (final IOException e) {
      //
     }
    }
}

In your jobs you will get the logger by the following call:

Logger logger = LoggerLocator.getLocator().getLogger();
Ahmad