无法在 Java 中锁定 .log 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5700752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:22:31  来源:igfitidea点击:

couldn't get lock for .log file in Java

javalogging

提问by NARU

I am using Logger from Restlet frameworkwith 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:

我正在使用Restlet 框架中的Logger和 FileHandler 在生产模式下记录我的应用程序。但是,我总是收到“无法为记录器创建 FileHandler:无法为 test.log 锁定”的例外情况。我该如何解决这个问题?这是代码:

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?

除了 .log 文件之外,还创建了许多其他文件,例如“.log.1、.log.2 .....”。有人知道为什么吗?

采纳答案by Abhishek

You should provide full Class Names. Logger& FileHandlerare 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.

您应该提供完整的类名。Logger&FileHandler是模棱两可的。但是,我猜您正在使用某种记录器,可能是 Log4j 和 RollingFileAppender,这就是为什么您的文件正在旋转,即 xxx.log.1 和 xxx.log.2。您的文件正被某些其他进程/应用程序使用,这就是您无法锁定该文件的原因。

回答by nagesh

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.

我也有同样的错误,但是当我检查文件的路径时,它是错误的,所以在更正路径后它工作正常。只需检查路径是否正确。

回答by Song.L

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();

对于“已经创建了许多其他文件,如“.log.1、.log.2 .....”的问题,您必须删除日志文件处理程序并在不需要它后将其关闭。这是代码供您参考。 log.removeHandler(fileHandler); fileHandler.close();

回答by Bahman_Aries

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.

对我来说,记录器对即将创建日志文件的目录没有写权限。因此,我只是将路径更改为保证完全访问的某个地方(例如FileHandler aFileHandler = new FileHandler("D:\\test.log");,问题消失了。

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 directoryor somewhere which has no write access.

我猜在可能的情况下,因为我没有指定任何特定的文件路径(例如FileHandler("test.log");,在使用 tomcat 部署我的 Web 服务之后,日志文件试图在Catalina base directory没有写访问权限的地方或地方创建。