本文介绍了是否可以通过编程方式找到回溯日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自动附加日志文件以支持电子邮件会很有用。我可以通过编程方式设置路径(如),但我我更喜欢让用户通过 logback.xml 以熟悉的方式配置日志记录。那么,我可以找到logback用于记录的文件吗?

It would be useful to automatically attach log files to support emails. I could set the path programmatically (as in Setting Logback Appender path programmatically), but I'd prefer to let users configure logging in the familiar way via logback.xml. So, can I find the files logback uses for logging?

推荐答案

您可以获取特定上下文中所有appender的列表。为此:

You can get the list of all appenders in a certain context. To do this:

LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
for (Logger logger : context.getLoggerList()) {
        for (Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext();) {
            Appender<ILoggingEvent> appender = index.next();
        }
    }

这会遍历所有记录器中所有appender的列表对于当前的背景。

This iterates over the list of all appenders in all loggers for the current context.

这篇关于是否可以通过编程方式找到回溯日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:53