javaloggingamazon-ec2slf4jslf4j-api

Log4j prints error stacktrace on local but not on server


I have a Java application running on Amazon EC2 instance. And I am using Apache log4j logging framework to maintain ERROR as well as DEBUG logs for my application.

But recently I'm not able to log error stack trace using logger.error(String Message, Throwable t).

I tried to log alternatively like this,

final Logger logger = LoggerFactory.getLogger(MyClass.class);

StringWriter sWriter = new StringWriter();
e.printStackTrace(new PrintWriter(sWriter));
logger.error("Exception occured while fetching count from DB", sWriter);

But .log file only displays the message Exception occurred while fetching count from DB

My log4j.properties file looks like this

# Root logger
log4j.rootLogger=INFO, RollingAppender

# LOG4J daily rolling log files configuration
log4j.rootLogger=${loggingType}, RollingAppender
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=${logFile}
log4j.appender.RollingAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=%d [%p] %c %M - %m%n

If I run the application locally, I can see error print stack trace but same does not work on my Amazon EC2 instance.

log4j version is 1.7.2

JDK version is 1.7.0_151

Any help is appreciated. Thank You.


Solution

  • Try using

    final Logger logger = LoggerFactory.getLogger(getClass()); 
    

    instead of

    final Logger logger = LoggerFactory.getLogger(MyClass.class);
    

    The first one allows you to use the name of the actual class in all classes throughout the inheritance hierarchy.