Failed to get lock for .log file in Java

I am using Logger from Restlet with FileHandler to register my application in production mode. However, I always get Excetption "Unable to create FileHandler for Logger: Failed to 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 more than one process at a time.

And besides the .log file, many other files have been created, such as ".log.1, .log.2 .....". Does anyone know why?

+4
source share
4 answers

You must provide the full class names. Logger and FileHandler are mixed. However, I assume that you are using some kind of logger, possibly Log4j and RollingFileAppender, so your files are rotated, i.e. xxx.log.1 and xxx.log.2. Your file is being used by some other process / application, so you cannot get a lock on this file.

+2
source

I also got the same error, but when I checked the path to the file, it was wrong, therefore, correcting the path, it worked fine. Just check the path if it is correct.

+2
source

For the question about the "large number of other files of the type" .log.1, .log.2 ..... "were created", you should remove the log file handler and close it after you do not need it. Here is the code for your reference. log.removeHandler(fileHandler); fileHandler.close();

0
source

For me, the logbook did not have the right to write to the directory in which the log file should be created. So I just changed the path to where full access was guaranteed (for example, FileHandler aFileHandler = new FileHandler("D:\\test.log"); and the problem disappeared.

I assume that in the case where I did not specify any specific file path (for example, FileHandler("test.log"); after deploying my web service using tomcat, the log file tried to be created in the Catalina base directory or somewhere that does not have write access.

0
source

All Articles