java 如何让 Log4j 使用标准输出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26791074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:36:30  来源:igfitidea点击:

How to get Log4j to use stdout?

javalog4j

提问by BombSite_A

I'm currently using log4j in some code I'm working on for debugging purposes. I've been running my code with java -jar test.jar | tee file.txtto log to a file but now I want to be able to switch the file I'm logging to while it's still running, something that teecan't do. Right now I'm doing this

我目前正在一些代码中使用 log4j 进行调试。我一直在运行我的代码java -jar test.jar | tee file.txt以登录到一个文件,但现在我希望能够在它仍在运行时切换我正在登录的文件,这是tee无法做到的。现在我正在做这个

private static final Logger log = LoggerFactory.getLogger(Test.class);

public void main() {
File file = new File(/path/to/file);
System.setOut(new PrintStream(file));
System.out.println("hello world"); //This works.
log.info("hello world"); //this doesn't.

For some reason the Logger output isn't going to my file, it's going to my console, but println()works fine. Does anyone have any idea why this is happening and how to fix this?

出于某种原因,记录器输出不会进入我的文件,而是进入我的控制台,但println()工作正常。有谁知道为什么会发生这种情况以及如何解决这个问题?

Thanks.

谢谢。

Edit: Here's my log4j.xml:

编辑:这是我的 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="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <!--<param name="ConversionPattern" value="%d %-5p [%c] %m%n" />-->
            <param name="ConversionPattern" value="%d %-5p [%c:%L] %m%n" />
        </layout>
    </appender>
    <logger name="com.company">
        <level value="trace" />
    </logger>
    <logger name="org.springframework">
        <level value="debug" />
    </logger>
    <logger name="com.company.Selenium">
        <level value="debug" />
    </logger>    
    <logger name="org.springframework">
        <level value="warn" />
    </logger>
    <root>
        <priority value="warn" />
        <appender-ref ref="console" />
    </root>
</log4j:configuration>

Also I'm getting the following error when I run my project

当我运行我的项目时,我也收到以下错误

log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

采纳答案by MAD

Example taken from this link Using Log4j for debugging in Java. You might be missing BasicConfigurator.configure();

来自此链接的示例使用 Log4j 在 Java 中进行调试。您可能缺少 BasicConfigurator.configure();

public class Log4jTest {
  // Initialize logger for instance Log4jTest
  static Logger log  = Logger.getLogger(Log4jTest.class);

  public static void main (String[] args) {
     // Basic configurator to log debug messages to the console
     BasicConfigurator.configure();         

     // Add some log messages
     log.debug("This is a debug message");
     log.trace("This is a trace message");         
 }
}

回答by Naresh kumar

please add this into your log4j.properties file

请将其添加到您的 log4j.properties 文件中

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

回答by Paul Vargas

The file log4j.propertiesor log4j.xmlmust be in the default package. e.g.:

该文件log4j.propertieslog4j.xml必须在默认包中。例如:

Default package

默认包

Of if you are using Maven, in "src/main/resources":

如果您使用的是 Maven,则在“src/main/resources”中:

resources

资源