You can load and unload a specific Log4j 2 configuration file for a given EJB. How? Use @PreDestroy and @PostConstruct. This gives you separately deployable EJBs with separate Log4j configurations. Ka-Pow!
For example:
public class MySessionEJB implements SessionBean { private static final String LOGGER_CONFIG = "/path/to/log4j2.xml"; private static final String LOGGER_CONTEXT_NAME = "MySessionEJB"; private static LoggerContext logctx; private static Logger logger; @PostConstruct @TransactionAttribute(value=TransactionAttributeType.NOT_SUPPORTED) private void postConstruct() { logctx = Configurator.initialize(LOGGER_CONTEXT_NAME, LOGGER_CONFIG); logger = logctx.getLogger("com.whatever.myejb"); } @PreDestroy @TransactionAttribute(value=TransactionAttributeType.NOT_SUPPORTED) private void preDestroy() { Configurator.shutdown(logctx); } }
Happy Coding,
Gary Gregory
Advertisements