如何通过FileHandler为Logger文件创建目录 [英] How to create directories for Logger files through FileHandler

查看:959
本文介绍了如何通过FileHandler为Logger文件创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在每天创建每个目录的目录中创建日志,但是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规范,不存在的目录是和/或应该被视为无效。这意味着您的日志应该出现在用户主目录中。这在 JDK-6244047中描述:除非存在,否则无法指定记录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:


配置:默认情况下,每个FileHandler都使用LogManager配置属性后面的
进行初始化。如果属性不是
定义的(或具有无效值),则使用指定的默认值

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指定处理程序的默认级别
    (默认为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 config 选项或子类FileHandler。

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

另请参阅: JDK-6258319:FileHandler文件没有例外%h,但%h不存在

这篇关于如何通过FileHandler为Logger文件创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆