关于将资源文件加载到 Java 项目的约定

发布于 2024-11-16 13:32:53 字数 1908 浏览 7 评论 0原文

鉴于我在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2024-11-23 13:32:53

尝试使用 DOMConfigurator,因此:

// Load log4j config file...
String path = "/path/to/log4j.xml");
DOMConfigurator.configure(path);

// Start Logging
LOG = Logger.getLogger(Program.class);

(从我当前项目中提取的代码,所以我知道它可以工作);-)

除了上面使用 DOMConfigurator 之外,您还可以选择设置一个观察周期,Log4J将每隔 xx 毫秒轮询 xml 文件是否有更改,然后重新加载更改:

// Load log4j config file...
String path = "/path/to/log4j.xml");
DOMConfigurator.configureAndWatch(path, 30000); // 30sec poll

// Start Logging
LOG = Logger.getLogger(Program.class);

HTH

Try using the DOMConfigurator, thus:

// Load log4j config file...
String path = "/path/to/log4j.xml");
DOMConfigurator.configure(path);

// Start Logging
LOG = Logger.getLogger(Program.class);

(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:

// Load log4j config file...
String path = "/path/to/log4j.xml");
DOMConfigurator.configureAndWatch(path, 30000); // 30sec poll

// Start Logging
LOG = Logger.getLogger(Program.class);

HTH

︶ ̄淡然 2024-11-23 13:32:53

您可以尝试设置 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文