关于将资源文件加载到 Java 项目的约定
鉴于我在尝试通过 log4j.xml
应用外部 log4j 配置时遇到问题,我现在有兴趣采用有关将资源文件加载到 Java 项目的约定。
尽管代码可以显示消息而没有警告或错误,但我怀疑配置并未真正应用,因为更改 ConversionPattern
对控制台输出没有任何影响。
Program.java
package the.project.path;
import java.io.File;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;
class Program {
static final Logger logger = Logger.getLogger("SampleLogger");
static final File config = new File("the.project.path/conf/log4j.xml");
static final String message = "The quick brown fox jumps over the lazy dog.";
public static void main(String[] args) {
if (config.exists()) {
PropertyConfigurator.configure(config.getPath());
} else {
BasicConfigurator.configure();
}
try {
logger.debug(message);
logger.info(message);
logger.warn(message);
logger.error(message);
logger.fatal(message);
} catch (Exception exception) {
System.out.println(exception.toString());
}
}
}
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="SampleConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!--
TTCC is a message format used by log4j.
TTCC is acronym for Time Thread Category Component.
It uses the following pattern: %r [%t] %-5p %c %x - %m%n
-->
<param name="ConversionPattern" value="[%t] [%-5p] - %m%n" />
</layout>
</appender>
<root>
<appender-ref ref="SampleConsoleAppender" />
</root>
</log4j:configuration>
任何建议将不胜感激。提前非常感谢。
Given that I'm having issues trying to apply an external log4j configuration via log4j.xml
I'm now interested in adopting a convention regarding the loading of resource files to Java projects.
Despite the code works displaying the message without warnings or errors, I suspect the configuration is not being really applied as changing the ConversionPattern
makes no difference to the console output.
Program.java
package the.project.path;
import java.io.File;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;
class Program {
static final Logger logger = Logger.getLogger("SampleLogger");
static final File config = new File("the.project.path/conf/log4j.xml");
static final String message = "The quick brown fox jumps over the lazy dog.";
public static void main(String[] args) {
if (config.exists()) {
PropertyConfigurator.configure(config.getPath());
} else {
BasicConfigurator.configure();
}
try {
logger.debug(message);
logger.info(message);
logger.warn(message);
logger.error(message);
logger.fatal(message);
} catch (Exception exception) {
System.out.println(exception.toString());
}
}
}
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="SampleConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!--
TTCC is a message format used by log4j.
TTCC is acronym for Time Thread Category Component.
It uses the following pattern: %r [%t] %-5p %c %x - %m%n
-->
<param name="ConversionPattern" value="[%t] [%-5p] - %m%n" />
</layout>
</appender>
<root>
<appender-ref ref="SampleConsoleAppender" />
</root>
</log4j:configuration>
Any advice will be really appreciated. Thanks much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 DOMConfigurator,因此:
(从我当前项目中提取的代码,所以我知道它可以工作);-)
除了上面使用 DOMConfigurator 之外,您还可以选择设置一个观察周期,Log4J将每隔 xx 毫秒轮询 xml 文件是否有更改,然后重新加载更改:
HTH
Try using the DOMConfigurator, thus:
(code ripped from my current project, so I know it works) ;-)
As well as the above use of DOMConfigurator, you can optionally set a watch period, whereby Log4J will poll the xml file for changes, every xx millis, and then reload the changes:
HTH
您可以尝试设置 -Dlog4j.debug=true 以查看哪些选项应用于 log4j。
它不会回答您的资源加载问题,但通常了解这一点会很有帮助。
You can try setting -Dlog4j.debug=true to see which options are being applied to log4j.
It doesn't answer your resource loading question, but it is helpful to know that usually.