假设我有
public class Something {
private static final Logger log = LogManager.getLogger(Something.class);
public static void doSomething() {
log.info("Doing something.");
}
}
public class Foo {
private static final Logger log = LogManager.getLogger(Foo.class);
public static void doFoo() {
log.info("Doing Foo.");
Something.doSomething();
}
}
public class Bar {
private static final Logger log = LogManager.getLogger(Bar.class);
public static void doBar() {
log.info("Doing Bar.");
Something.doSomething();
}
}
是否可以将Log4j2配置为基于调用堆栈写入不同的日志文件?
这样,将在调用Foo.doFoo()
之后记录的所有语句写入Foo.log,并将在调用Bar.doBar()
之后记录的所有语句写入Bar.log。
以下内容
class Test {
public static void main(String[] args) {
Foo.doFoo();
Bar.doBar();
}
}
将使用以下内容生成Foo.log
Doing Foo.
Doing something.
和Bar.log与
Doing Bar.
Doing something.