使用 log4j 记录器未在日志文件中获取 INFO 日志

发布于 2024-12-11 05:22:16 字数 2808 浏览 0 评论 0原文

我开始使用 java 和 log4j。我的测试程序需要帮助。

$pwd

/home/test/log4j

$ls

Log4jTest.class Log4jTest.java log_file logger.xml run.sh

$cat Log4jTest.java 
  import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jTest
{
public static void main(String args[])
{
Logger _log;
String loggerConfig = System.getenv ("logger_config");
 if (loggerConfig != null && !loggerConfig.trim ().equals (""))
  {   
    DOMConfigurator.configureAndWatch (loggerConfig);
  }   
else
{

System.out.println("no logger_config found");
    System.exit (-1);
}
 System.out.println("Logger is " + Log4jTest.class.getName ());
   _log = Logger.getLogger (Log4jTest.class.getName ());

 _log.info("logging to logger. Good!");
}
}

$ cat logger.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
            <param name="Target" value="System.out" />
            <param name="threshold" value="OFF" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern"
                            value="%d{yyyy-MM-dd HH:mm:ss,SSS}{GMT+0} [%t] %-5p %c{1} - %m%n" />
            </layout>
    </appender>

    <appender name="log4j_test" class="org.apache.log4j.RollingFileAppender">
            <param name="File" value="/home/test/log4j/log_file" />
            <param name="append" value="true" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern"
                            value="%d{yyyy-MM-dd HH:mm:ss,SSS}{GMT+0} [%t] %-5p %c{1} - %m%n" />
            </layout>
    </appender>

    <logger name="Log4jTest"
            additivity="false">
            <level value="INFO" />
            <appender-ref ref="log4j_test" />
    </logger>

    <root>
            <level value="INFO" />
            <appender-ref ref="log4j_test" />
    </root>

$export logger_config=/home/test/log4j/log_file

$javac -cp log4j__log4j.jar Log4jTest.java 的路径

$java -cp log4j__log4j.jar 的路径:。 Log4jTest

Logger为Log4jTest

$ cat log_file

问题:

(1) 是否需要指定consoleappender? (2) root 里有什么? (3) root 和 loggers 中的 level_value 有何作用? (4) 我期待看到: 记录到记录器。好的!

log_file 中

出了什么问题?

I am starting on java and log4j. I need help with my test program.

$pwd

/home/test/log4j

$ls

Log4jTest.class Log4jTest.java log_file logger.xml run.sh

$cat Log4jTest.java 
  import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jTest
{
public static void main(String args[])
{
Logger _log;
String loggerConfig = System.getenv ("logger_config");
 if (loggerConfig != null && !loggerConfig.trim ().equals (""))
  {   
    DOMConfigurator.configureAndWatch (loggerConfig);
  }   
else
{

System.out.println("no logger_config found");
    System.exit (-1);
}
 System.out.println("Logger is " + Log4jTest.class.getName ());
   _log = Logger.getLogger (Log4jTest.class.getName ());

 _log.info("logging to logger. Good!");
}
}

$ cat logger.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
            <param name="Target" value="System.out" />
            <param name="threshold" value="OFF" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern"
                            value="%d{yyyy-MM-dd HH:mm:ss,SSS}{GMT+0} [%t] %-5p %c{1} - %m%n" />
            </layout>
    </appender>

    <appender name="log4j_test" class="org.apache.log4j.RollingFileAppender">
            <param name="File" value="/home/test/log4j/log_file" />
            <param name="append" value="true" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern"
                            value="%d{yyyy-MM-dd HH:mm:ss,SSS}{GMT+0} [%t] %-5p %c{1} - %m%n" />
            </layout>
    </appender>

    <logger name="Log4jTest"
            additivity="false">
            <level value="INFO" />
            <appender-ref ref="log4j_test" />
    </logger>

    <root>
            <level value="INFO" />
            <appender-ref ref="log4j_test" />
    </root>

$export logger_config=/home/test/log4j/log_file

$javac -cp path to log4j__log4j.jar Log4jTest.java

$java -cp path to log4j__log4j.jar:. Log4jTest

Logger is Log4jTest

$ cat log_file

Question:

(1) Is it necessary to specify console appender?
(2) what goes in root?
(3) What does level_value in root as well as loggers do?
(4) I was expecting to see:
logging to logger. Good!

in log_file

What is going wrong?

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

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

发布评论

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

评论(2

旧竹 2024-12-18 05:22:16

至少需要一个附加器。否则 Log4J 不知道在哪里打印日志消息。有各种附加程序:控制台、文件、JMS、JDBC。每个人都知道将消息发送到不同的目的地。

您无法在控制台上看到日志消息,因为控制台附加程序已关闭:

将 OFF 更改为 INFO,我希望您能看到日志。

Log4J 具有记录器的分层架构。层次结构中的每一层都可能阻止您的消息。额外的过滤是在appenders层上完成的。您可以在这里阅读更多信息: http://logging.apache.org/log4j/1.2/manual .html

但请注意:log4j 已被其创建者弃用。看一下 logback 和 slf4j。

At least one appender is needed. Otherwise Log4J does not know where to print your log messages. There are various appenders: Console, file, JMS, JDBC. Each one knows to send messages to different destinations.

You cannot see log messages on console because console appender is off:

<param name="threshold" value="OFF" />

Change OFF to INFO and I hope you will see your log.

Log4J has hierarchical architecture of loggers. Each layer in hierarchy can block your message. Additional filtering is done on appenders layer. You can read more here: http://logging.apache.org/log4j/1.2/manual.html

But pay attention: log4j is deprecated by its creator. Take a look on logback and slf4j.

剩余の解释 2024-12-18 05:22:16

不,如果您不使用控制台记录器,则无需定义它。

不过,在我看来,没有控制台记录器几乎总是一个坏主意;大多数 IDE 都希望默认记录器能够转到控制台,许多应用程序服务器也是如此。当然,这完全取决于您的需求。

一般来说,您需要一个按类记录器,即 Logger.getLogger(Log4jTest.class)。在配置文件中设置该记录器的阈值。

另请注意,您可以在类路径上有一个 log4j.xml 文件,配置将自动获取,从而避免手动配置步骤。

最后,现在使用特定日志记录实现的“本机”接口相对不常见,在实际实现中使用 slf4j、logback 或 commons-logger 更常见。并不是说您不能,只是我不确定您应该

No, you don't need to define a console logger if you're not using it.

IMO it's almost always a bad idea to not have a console logger, though; most IDEs expect the default logger to go to the console, as do many app servers, etc. Depends entirely upon your needs, of course.

In general you'll want to have a per-class logger, which is just Logger.getLogger(Log4jTest.class). Set the threshold of that logger in the config file.

Also note that you can just have a log4j.xml file on the classpath and the configuration will be picked up automatically, avoiding the manual config steps.

Lastly, it's relatively unusual these days to use the "native" interface of a particular logging implementation, it's more common to use slf4j, logback, or commons-logger around the actual implementation. Not saying you can't, just that I'm not sure you should.

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