简体   繁体   English

无法在Java中获取.log文件的锁定

[英]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. 我正在使用来自Restlet框架的 Logger和FileHandler在生产模式下记录我的应用程序。 However, I always get the Excetption "Unable to create a FileHandler for the Logger: Couldn't get lock for test.log". 但是,我总是得到Excetption“无法为Logger创建FileHandler:无法获取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. 除了.log文件外,还创建了许多其他文件,如“.log.1,.log.2 .....”。 Does anybody know why? 有人知道为什么吗?

You should provide full Class Names. 您应该提供完整的班级名称。 Logger & FileHandler are ambiguous. LoggerFileHandler含糊不清。 However I guess you are using some kind of logger maybe Log4j and a RollingFileAppender which is why your files are getting rotated ie xxx.log.1 & xxx.log.2. 但是我想你正在使用某种记录器,可能是Log4j和RollingFileAppender,这就是你的文件被轮换的原因,即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. 对于“很多其他文件”的问题,例如“.log.1,。log.2 .....”已经创建“,你必须删除日志文件处理程序并在你不需要之后关闭它。 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 (eg 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 directory or somewhere which has no write access. 我猜这可能是因为我没有指定任何特定的文件路径(如FileHandler("test.log"); ,在使用tomcat部署我的Web服务后,日志文件试图在Catalina base directory上创建Catalina base directory或没有写访问权限的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.io.IOException:无法获取锁定 - java.io.IOException: Couldn't get lock for Sonarqube:作业失败,无法删除锁定文件:.././.sonar_lock java.nio.file.NoSuchFileException - Sonarqube: Job fails with Couldn't delete lock file: .././.sonar_lock java.nio.file.NoSuchFileException 无法启动glassfish服务器,因为无法锁定/opt/glassfishv3/glassfish/domains/domain1/logs/server.log - Failed to start glassfish server because Couldn't get lock for /opt/glassfishv3/glassfish/domains/domain1/logs/server.log java - 版本无法执行 java 文件 - java -version couldn't execute java file 无法将整数文件写入Java中的文件 - Couldn't write integer file into a file in java 无法从文本字段Java获取文本 - Couldn't get text from the textfield Java Java创建日志文件java0.log并将文件锁定在/ root文件夹中 - Java creating logfile java0.log and lock file in /root folder init()中的Atomikos错误:无法获取/var/lib/tomcat6/./tm.out的锁定 - Atomikos Error in init(): Couldn't get lock for /var/lib/tomcat6/./tm.out Windows 10 上的 gradle 测试:无法读取文件内容:executionHistory.lock - gradle test on Windows 10: Couldn't read file content: executionHistory.lock JNA通过Java在dll文件中找不到指定的过程 - JNA couldn't find the specified procedure in dll file through java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM