开发者

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?


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.


I too got the same error, but when I checked the path of the file, it was wrong so after correcting the path it worked fine. Just check the path if it is correct.


For question about "a lot of other files like ".log.1, .log.2 ....." have been created", you have to remove the log filehandler and close it after you dont need it. Here is the code for your reference. log.removeHandler(fileHandler); fileHandler.close();


For me, The logger didn't have write access to the directory in which the log file is about to be created. Therefore I just changed the path to somewhere that a full access was guaranteed (e.g. FileHandler aFileHandler = new FileHandler("D:\\test.log"); and the issue went away.

I'm guessing that in may case because I hadn't had specified any particular file path (like FileHandler("test.log");, after deploying my web service using tomcat, the log file was trying to be created in Catalina base directory or somewhere which has no write access.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜