slf4j,change logger or add appender at runtime
slf4j is just a facade on the top of log4j/log4j2/logback. Once you declare the slf4j logger object:
private static final Logger BUSINESS_LOGGER = LoggerFactory.getLogger("BusinessLogger");
You can add your log4j appender using log4j LogManager.
LogManager.getLogger("BusinessLogger").addAppender(consoleAppender);
So I kind of solved it with the help from alterfox.
When I called the logger using slf4j:
private static final Logger BUSINESS_LOGGER = LoggerFactory.getLogger("BusinessLogger");
It returns me an implementation of the logger adapter from slf4j which doesn't have the add appender method. So I called the logger using log4j Logger class
private static final Logger BUSINESS_LOGGER = Logger.getLogger("BusinessLogger")
Even though the class names are "Logger", they are from different package. The latter one, which is Log4J method, returns the actual Logger object from Log4J. Then the addAppender method becomes available. Well, I tried this, and it doesn't work (except it does). The thing is, I forgot to set the log level. So after adding the appender, I set the log level and then I see the logs coming through the second appender.
This may seem simple but I found when I encountered this error I had forgotten to place the binder package in my POM for my application.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>