java中filehandler,如何通过FileHandler为Logger文件创建目录

博客围绕Java中FileHandler创建日志目录展开。用户尝试按日创建目录记录日志,FileHandler却无法创建目录并抛出异常。经分析,j.u.l.FileHandler不能创建目录,若指定无效值会使用默认值。若需创建目录,可使用LogManager配置选项或继承FileHandler。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 can create even directories if not present ,isn't this possible through fileHandler ?

解决方案

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:

Configuration: By default each FileHandler is initialized using the

following LogManager configuration properties. If properties are not

defined (or have invalid values) then the specified default values

are used.

java.util.logging.FileHandler.level specifies the default level

for the Handler (defaults to Level.ALL).

java.util.logging.FileHandler.pattern specifies a pattern for

generating the output file name. See below for details.

(Defaults to "%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.

If you need to create directories then you can use the LogManager config option or subclass the FileHandler.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值