linkedin / dr-elephant

Dr. Elephant is a job and flow-level performance monitoring and tuning tool for Apache Hadoop and Apache Spark
Apache License 2.0
1.35k stars 859 forks source link

Log Exception object directly #669

Closed astahlman closed 4 years ago

astahlman commented 4 years ago

Instead of logging the Exception type and stack-trace separately, use Logger.error(String msg, Throwable t) to log the message and stack-trace in a single ERROR entry.

Here's a disposable test whose output illustrates the difference:

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.junit.Test;

public class LogTest {

  private static final Logger logger = Logger.getLogger(ElephantRunner.class);
  Object nullObj = null;

  @Test
  public void logExceptionGoodWay() {
    try {
      nullObj.toString();
    } catch (Exception e) {
      logger.error("Failure in .toString()!", e);
    }
  }

  @Test
  public void logExceptionBadWay() {
    try {
      nullObj.toString();
    } catch (Exception e) {
      logger.error(e.getMessage());
      logger.error(ExceptionUtils.getStackTrace(e));
    }
  }
}

Before

Note that the frst log statement is empty:

03-24-2020 14:40:34 ERROR [main] com.linkedin.drelephant.ElephantRunner :
03-24-2020 14:40:34 ERROR [main] com.linkedin.drelephant.ElephantRunner : java.lang.NullPointerException
    at com.linkedin.drelephant.LogTest.logExceptionBadWay(LogTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ...

After

03-24-2020 14:40:34 ERROR [main] com.linkedin.drelephant.ElephantRunner : Failure in .toString()!
java.lang.NullPointerException
    at com.linkedin.drelephant.LogTest.logExceptionGoodWay(LogTest.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ...