NARU NARU - 1 month ago 12
Java Question

couldn't get lock for .log file in Java

I am using Logger from Restlet framework with FileHandler to log my application in production mode. However, I always get the Excetption "Unable to create a FileHandler for the Logger: Couldn't get lock for test.log". How can I solve this?
Here is the code:

FileHandler aFileHandler = new FileHandler("test.log");

Formatter aFormatter = new SimpleFormatter();

aFileHandler.setFormatter(aFormatter);

aLogger.setLevel(Level.ALL);

aLogger.addHandler(aFileHandler);


This log file is used by several by more than one process at the same time.

And except the .log file, a lot of other files like ".log.1, .log.2 ....." have been created. Does anybody know why?

Answer

You should provide full Class Names. Logger & FileHandler are ambiguous. However I guess you are using some kind of logger maybe Log4j and a RollingFileAppender which is why your files are getting rotated i.e. xxx.log.1 & xxx.log.2. Your file is being used by some other process/application which is why you are not able to get a lock on that file.