简体   繁体   English

Log4j具有带log4j.properties的两个输出?

[英]Log4j with two outputs with log4j.properties?

I want to use the Log4j API but I encountered a little problem with the configuration. 我想使用Log4j API,但是在配置上遇到了一些问题。

This is my LogWriter Class: 这是我的LogWriter类别:

public class LogWriter {
    static Logger logger = Logger.getLogger(LogWriter.class);

    public LogWriter(String logFilePath) {
        File outFile = new File(logFilePath);
        try {
            PrintStream outStream = new PrintStream(new FileOutputStream(outFile));
            System.setOut(outStream));    
            System.setErr(outStream);
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException("File not Found");
        }
        BasicConfigurator.configure();
    }

    public void write(String infoString) {
        logger.info(infoString);
    }
}

Now this works fine, but I can't write into the Logger-File and the Console at the same time (probably because of System.setOut ) 现在这可以正常工作,但是我不能同时写入Logger-File和Console(可能是因为System.setOut

I added a log4j.properties File that looks like this: 我添加了一个log4j.properties文件,如下所示:

# Root logger option
log4j.rootLogger=INFO, console

# Direct log messages to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p- %m%n

Now my question is, how can I make my LogWriter and the log4j.properties File work together, so that the Output comes into the Logger-File and the Console? 现在我的问题是,如何使我的LogWriter和log4j.properties文件一起工作,以便输出进入Logger-File和Console? What do I have to change in my LogWriter? 我必须在LogWriter中进行哪些更改?

Before installing your LogWriter , you must keep copies of the original references stored in System.out and System.err somewhere. 在安装LogWriter之前,必须将存储在System.outSystem.err中的原始引用的副本保留在某个位置。 Use them to write a new appender. 使用它们编写新的追加程序。 Configure log4j to use this appender in log4j.appender.console . 将log4j配置为在log4j.appender.console使用此附加程序。

Background: The default log4j ConsoleAppender just writes to System.out . 背景:默认的log4j ConsoleAppender仅写入System.out If you change it, log4j will use whatever you supply. 如果更改它,log4j将使用您提供的任何内容。 There is no way to configure ConsoleAppender to write somewhere else. 无法将ConsoleAppender配置为在其他地方编写。 In your LogWriter , it's not possible to distinguish between output coming from log4j and System.out.println() . LogWriter ,无法区分来自log4j的输出和System.out.println()

So the only solution is to write another appender which writes to the original System.out . 因此,唯一的解决方案是编写另一个写入原始System.out附加程序。

That said, it might be smarter to use the original ConsoleAppender and use a FileAppender instead of your own LogWriter . 也就是说,使用原始的ConsoleAppender并使用FileAppender代替您自己的LogWriter可能更聪明。 Just make sure you never use System.out directly anywhere in your code. 只要确保您绝对不要在代码中的任何地方直接使用System.out

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM