【问题标题】:Is it possible to print extra custom fileds along with the StackTrace while using log4j使用 log4j 时是否可以打印额外的自定义字段以及堆栈跟踪
【发布时间】:2013-06-26 16:35:48
【问题描述】:

我有这个程序,如下所示,现在它只打印堆栈跟踪。 我的问题是,是否可以获得堆栈跟踪以及自定义字段,在我的情况下,我需要 1090099

请告诉我这是否可能??

package com;
import org.apache.log4j.Logger;
public class Test {

    private static final Logger logger = Logger.getLogger(Test.class);
    public static void main(String args[]) {
        try {
    String accountid = "1090099";
            String desc = null;
            System.out.println(desc.toUpperCase());
            } 
            catch (Exception t) 
        {
            logger.fatal("Exception inside the Test program  ", t);
        }
    }
}

2013-06-26 21:44:29,723[main] FATAL(Test.java:<main>:16)- Exception inside the Test program
java.lang.NullPointerException
    at com.Test.main(Test.java:12)

【问题讨论】:

    标签: java error-handling log4j stack-trace exception-logging


    【解决方案1】:

    您必须手动将其包含在您正在记录的消息中。但在我看来,您真正要寻找的是MDC(映射的诊断上下文),一种将值存储在线程本地“上下文”中的方法,然后可以用来区分与不同的日志消息相关的应用程序级实体。

    package com;
    import org.apache.log4j.*;
    public class Test {
    
        private static final Logger logger = Logger.getLogger(Test.class);
        public static void main(String args[]) {
            MDC.put("accountid", "1090099");
            try {
                String desc = null;
                System.out.println(desc.toUpperCase());
                } 
                catch (Exception t) 
            {
                logger.fatal("Exception inside the Test program  ", t);
            } finally {
                MDC.remove("accountid");
            }
        }
    }
    

    然后,您将在附加程序的布局模式中的某处包含%X{accountid},它会在每条日志消息中包含适当的 MDC 条目,包括由您调用的第三方代码记录的那些。

    【讨论】:

    • 与公认的答案相比,我认为可以公平地说 OP 并不是真的在寻找 MDC ...:P 还是很好的信息!
    【解决方案2】:

    我将创建自己的异常类,其中包含用于保存附加信息的成员,以及显示它们的合适的 toString() 方法。将原始 Exception 包装在您的自定义 Exception 中,并添加您想要保留的信息。

    【讨论】:

      【解决方案3】:

      是的,只要值在范围内,您就可以打印它。

      String accountid = null;
      try {
           accountid = "1090099";
           String desc = null;
           System.out.println(desc.toUpperCase());
       } catch (Exception t) {
           logger.fatal("Exception inside the Test program  " + accountid, t);
       } 
      

      另外,我建议您使用 logger.debug 而不是 system.out.println 进行其他日志记录调用...

      【讨论】:

        【解决方案4】:

        您即将实现这一目标。

        在您的情况下,您必须在try 块之外声明accountid,然后您可以将accountid 与您的Exception inside the Test program 消息一起附加`

        【讨论】:

          【解决方案5】:
          String accountid = ""; 
          
          try {
                  accountid = "1090099";
                  String desc = null;
                  System.out.println(desc.toUpperCase());
              } 
                  catch (Exception t) 
              {
                  logger.fatal("Exception inside the Test program.\nAccount ID: " + accountid, t);
              }
          

          【讨论】:

            猜你喜欢
            • 2011-03-16
            • 2011-01-18
            • 1970-01-01
            • 1970-01-01
            • 2015-12-26
            • 1970-01-01
            • 2018-07-06
            • 1970-01-01
            • 2010-09-25
            相关资源
            最近更新 更多