0

We are using slf4jlogger to log files. After the completion of the processes the log file is automatically saved in a restricted location. Since, I don't have access to download this file later, I want to create the log file manually following the slf4jlogger.

I have the following code:

test class

package com.worksap.company.hue.controller.com.ifx.inputengine.imports.sample;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class test {

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

    @Test
    public void testFunction() {
        test2 t = new test2();
        StringBuilder execLog = new StringBuilder("");
        logger.info("Starting execution" + System.getProperty("line.separator"));
        execLog.append("Starting execution" + System.getProperty("line.separator"));
        t.executeProcess(execLog);

        logger.info("ending exection");
        execLog.append(String.format("ending execution" + System.getProperty("line.separator")));

        System.out.println(execLog);

    }
}

test2 class

package com.worksap.company.hue.controller.com.ifx.inputengine.imports.sample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class test2 {

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

    public void executeProcess(StringBuilder execLog) {
        logger.info("execution step");
        execLog.append(String.format("execution step" + System.getProperty("line.separator")));
    }

}

I then want to push the execLog content to AWS S3 so that I can have access to the log files later. I have the following questions:

  1. Is this a good way to reproduce the logs or is there a better way to do it?
  2. How can I avoid repetition of the same string content?

e.g.

logger.info("Starting execution" + System.getProperty("line.separator"));
        execLog.append("Starting execution" + System.getProperty("line.separator"));
kosta
  • 4,302
  • 10
  • 50
  • 104
  • shouldn't you be looking into a custom appender first (to log to a `StringBuilder`) and than how to bind multiple appenders to slf4j? – Eugene May 22 '17 at 05:14
  • Could you please elaborate? – kosta May 22 '17 at 05:24
  • slf4j supports multiple appenders, i.e. you could log same thing to multiple places. here is an example : http://stackoverflow.com/questions/2488558/logback-to-log-different-messages-to-two-files – Eugene May 22 '17 at 05:56

0 Answers0