简体   繁体   English

仅使用一个Logger将log4j记录到两个不同的文件中

[英]log4j logging to two different files with one Logger only

I have read though all post here and though my question repeats the one of others, non of the answers really anwers mine: 我已经阅读了所有帖子,尽管我的问题重复了另一个问题,但没有一个答案真的使我感到困惑:

I have quite a large project with a logger in almost every class. 我有一个很大的项目,几乎每个班级都有一个记录器。 I wont to keep using only this one Logger and I want it to to create - a log file with INFO and ERROR - a separate log file with ERROR only 我不会只使用一个Logger,而是希望它创建-具有INFO和ERROR的日志文件-仅具有ERROR的单独的日志文件

This is the code is use to log: 这是用于登录的代码:

static Logger log = Logger.getLogger(TestLogger.class);

public static void main(String[] args) {

    log.debug("debug");
    log.info("info");
    log.error("error");

}

and this is how I thought I cound configure it, leaving out the layout stuff. 这就是我以为我对它进行配置的方式,而忽略了布局的内容。

log4j.rootLogger=INFO, stdout, file
log4j.logger.errors = ERROR, stdout, file, file2

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

... ...

log4j.appender.file=metaobjects.io.CustomFileAppender
log4j.appender.file.File=c:/temp/execution%timestamp/test.log

... ...

log4j.appender.file2=metaobjects.io.CustomFileAppender
log4j.appender.file2.File=c:/temp/execution%timestamp/error.log

Is there any way to get what I want or do I need two Logger objects in every class? 有什么办法可以得到我想要的,还是每个类中都需要两个Logger对象?

You will need to create multiple appenders with a different threshold. 您将需要创建多个具有不同阈值的追加程序。

Keep in mind debug will consume error and would consume info logging. 请记住, debug会消耗error并且会消耗info日志记录。

try to look into this tutorial https://veerasundar.com/blog/2009/08/log4j-tutorial-additivity-what-and-why/ 尝试查看本教程https://veerasundar.com/blog/2009/08/log4j-tutorial-additivity-what-and-why/

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

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