问题描述
我正在尝试在每天创建每个目录的目录中创建日志,但是fileHandler没有创建目录,而是抛出异常无法锁定C:\dir_date \ Logging.txt (这里dir_date不存在,我正在尝试创建登录到此目录)。我可以通过fileHandler创建目录吗?
I am trying to create logs in directories where each directories are created day wise, but fileHandler is not creating directories rather its throwing exception Couldn't get lock for C:\dir_date\Logging.txt (here dir_date is not present and i am trying to create log into this directory). Can i create directories through "fileHandler " ?
FileHandler fileTxt;
fileTxt = new FileHandler("C:\\ff\\Logging.txt");
如果不存在,log4J可以创建偶数目录,这不是通过fileHandler实现的吗?
log4J can create even directories if not present ,isn't this possible through fileHandler ?
推荐答案
julFileHandler无法创建目录。根据API规范,不存在的目录是和/或应该被视为无效。这意味着您的日志应该出现在用户主目录中。这在 :
The j.u.l.FileHandler can't create directories. According to the API spec, nonexistent directories are and or should be treated as invalid. Which means your logs should appear in the user home directory instead. This described in JDK-6244047: impossible to specify directories to logging FileHandler unless they exist:
- java .util.logging.FileHandler.level指定处理程序的默认级别
(默认为Level.ALL)。
< snip>
- java.util.logging.FileHandler.pattern指定一个生成输出文件名的
的模式。请参阅下文了解详情。
(默认为%h / java%u.log)。
根据上面的规范措辞,如果FileHandler.pattern属性
指定一个不可用的值,然后它是无效的。如果指定了无效值
,则API应该使用默认值。在
中,应该使用%h / java%u.log。
Based on the spec wording above, if the "FileHandler.pattern" property specifies an unusable value, then it is invalid. If an invalid value is specified, then the API is supposed to use the default value. In this case "%h/java%u.log" should be used.
如果您需要创建目录,那么您可以使用LogManager 选项或子类FileHandler。
If you need to create directories then you can use the LogManager config option or subclass the FileHandler.
另请参阅:
这篇关于如何通过FileHandler为Logger文件创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!