2

Following is my logback.xml config in my springboot application.

<property name="LOG_DIR" value="${LOG_DIR}" />
<property name="LOG_FILE" value="${LOG_FILE}" />

<appender name="ROLLING_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_DIR}/${LOG_FILE}</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>${LOG_DIR}/${LOG_FILE}_old.%i.log</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>100</maxIndex>
    </rollingPolicy>

    <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>

</appender>

<root level="info">
    <appender-ref ref="ROLLING_LOG" />
</root>

Application is started with the following command

java -DLOG_DIR=/logs -DLOG_FILE=my.log -jar target/my.jar

I use @Sl4j in all my classes. All the logs are printed in the log file, except for exception's stacktrace (e.printStackTrace)., it is printed in console instead.

Not a duplicate post, gone through almost all the posts couldn't find proper answer

Appreciate any help

Thanks

Raaghu
  • 453
  • 3
  • 17
  • 1
    e.printStackTrace always print on standard output, it doesn't use any logger. Use the logger last arg if you want to add it to log. – JEY May 09 '18 at 14:53
  • 1
    do you log the exception with your logger in the catch? `LOGGER.error(e.getMessage,e)` – Dirk Deyne May 09 '18 at 14:55
  • @Dirk if it is in my code we could write in that way. suppose it is in some library, then? – Raaghu May 09 '18 at 15:12

2 Answers2

4

As stated in the Javadoc:

Prints this throwable and its backtrace to the standard error stream.

So unless you log the exception through SLF4J, the stacktrace will not be printed in your file.

If you do not control the call to printStackTrace, you can redirect the output of the java-command to append to a file.

java -DLOG_DIR=/logs -DLOG_FILE=my.log -jar target/my.jar >> console.log 2>&1

The above will append all text to a file console.log and redirect standard error stream to standard output stream.

TomVW
  • 1,510
  • 13
  • 26
  • awesome work around, very helpful. Those who want to know '2>&1' -- https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean?answertab=active#tab-top – Raaghu May 09 '18 at 15:46
0

To print the StackTrace as in log file, the following method can be used.

Maven dependency in pom.xml

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>
String stacktrace = ExceptionUtils.getStackTrace(e); 

Using Core Java

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
sw.toString() 
Muhammad Tariq
  • 3,318
  • 5
  • 38
  • 42