Write the log in to existing common log file using slf4j

Viewed 17

I have multiple services and each one of them interacting with each other. There is common.log file where I am writing the common log in to this file. logback.xml entry for this common.log is present in to another service lets name it as Service1. I want to write the some common things in to this existing common.log file using slf4j.

I tried to write a utility class to write the log from Service2 application to common.log and using the java.util.logging package as below ,

public class LogUtil {

    private static LogUtil instance = new LogUtil();

    public static LogUtil getInstance() {
        return instance;
    }

    private static final Logger logger = Logger.getLogger(LogUtil.class.getName());

    static {
        try {
            String logFile = System.getProperty("home") + "/logs/common.log";
            FileHandler fh = new FileHandler(logFile, true);
            SimpleFormatter formatter = new SimpleFormatter();
            fh.setFormatter(formatter);
            logger.addHandler(fh);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void error(String msg) {
        logger.log(Level.SEVERE,"[Error Common] :"+msg);
    }
}

I want to do it using the slf4j api. Please suggest me some approach to write the utility class using slf4j.

0 Answers
Related