在 Java 中初始化和使用 Log4J

发布于 2024-10-11 17:41:40 字数 143 浏览 6 评论 0原文

我需要知道如何在java中初始化和使用Log4j...... 当我尝试使用它时,出现以下错误

log4j:WARN No Appender can be find for logger (Main)。 log4j:WARN 请正确初始化 log4j 系统。

I need to know how to initialise and use Log4j in java....
When i try to use it i get the following error

log4j:WARN No appenders could be found for logger (Main).
log4j:WARN Please initialize the log4j system properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

情域 2024-10-18 17:41:40

您可以将 log4j.properties 放入 src 文件夹中,系统会自动检查 log4j 配置。默认配置可能如下所示。

### Root Logger
log4j.rootLogger=DEBUG, Console

### Console Appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d (%t) %-5p %c: %m%n

否则,您可以通过编程方式调用默认配置。

 import com.foo.Bar;

 // Import log4j classes.
 import org.apache.log4j.Logger;
 import org.apache.log4j.BasicConfigurator;

 public class MyApp {

   // Define a static logger variable so that it references the
   // Logger instance named "MyApp".
   static Logger logger = Logger.getLogger(MyApp.class);

   public static void main(String[] args) {

     // Set up a simple configuration that logs on the console.
     BasicConfigurator.configure();

     logger.info("Entering application.");
     Bar bar = new Bar();
     bar.doIt();
     logger.info("Exiting application.");
   }
 }

摘自 Apache log4j 的简短介绍文档。在提出多余的问题之前,您应该先查找类似的问题

考虑切换到 SLF4J 这使您可以轻松切换日志记录实现。

You could place a log4j.properties into your src folder, this is automatically checked for log4j configuration. A default configuration could look like this.

### Root Logger
log4j.rootLogger=DEBUG, Console

### Console Appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d (%t) %-5p %c: %m%n

Otherwise you could call the default configuration programmatically.

 import com.foo.Bar;

 // Import log4j classes.
 import org.apache.log4j.Logger;
 import org.apache.log4j.BasicConfigurator;

 public class MyApp {

   // Define a static logger variable so that it references the
   // Logger instance named "MyApp".
   static Logger logger = Logger.getLogger(MyApp.class);

   public static void main(String[] args) {

     // Set up a simple configuration that logs on the console.
     BasicConfigurator.configure();

     logger.info("Entering application.");
     Bar bar = new Bar();
     bar.doIt();
     logger.info("Exiting application.");
   }
 }

Taken from the short introduction document of Apache log4j. You should look for similiar questions before asking them redudant.

Consider to switch to SLF4J which enables you the possibilities to switch the logging implementation easily.

椒妓 2024-10-18 17:41:40

首先,阅读以下内容:http://logging.apache.org/log4j/1.2/manual。 html

不久,当您运行 JVM 时,会缺少以下属性:

-Dlog4j.configuration=mylog4jconfig.xml

但是:您的问题表明您刚刚开始使用 log4j。请注意,它的作者 Ceki Gülcü 已经过时了,他为 java 实现了新的记录器并建议人们使用它。请参阅 Logback (http://logback.qos.ch/) 和 SLF4J

First, read this: http://logging.apache.org/log4j/1.2/manual.html

Shortly the following property is missing when you are running JVM:

-Dlog4j.configuration=mylog4jconfig.xml

BUT: your question shows that you are just starting using log4j. Please note that it is already obsolete by its author, Ceki Gülcü, that implemented new logger for java and recommends people to use it. See Logback (http://logback.qos.ch/) and SLF4J

看轻我的陪伴 2024-10-18 17:41:40

您需要在类路径中包含 log4j.properties 文件。

You need to have the log4j.properties file in your classpath.

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