log4j2.xml和log4j.properties的指定配置路径方法

对于默认直接把配置文件放任classpath下面,这种henjiandan

首先说下log4j的配置,有两种方法:

1、在web.xml中配置(推荐):


   
   
   
   
  1. <context-param>
  2. <param-name>webAppRootKey param-name>
  3. <param-value>webapp.devportal param-value>
  4. context-param>
  5. <context-param>
  6. <param-name>log4jConfigLocation param-name>
  7. <param-value>file:/opt/config/open_portal/opendev/appconfig/log4j.properties param-value>
  8. context-param>
  9. <context-param>
  10. <param-name>log4jRefreshInterval param-name>
  11. <param-value>60000 param-value>
  12. context-param>
  13. <listener>
  14. <listener-class>org.springframework.web.util.Log4jConfigListener listener-class>
  15. listener>


2、代码配置(耦合性强):


   
   
   
   
  1. package com.cmcc.open.devportal.omae.util;
  2. import org.apache.log4j.Logger;
  3. import org.apache.log4j.PropertyConfigurator;
  4. import com.cmcc.open.base.utils.ConfigurationUtil;
  5. public class Log4jConfig {
  6. private boolean reload = true;
  7. private int interval = 60000;
  8. private static Logger log = Logger.getLogger(ConfigurationUtil.class);
  9. public Log4jConfig(boolean reload, int interval) {
  10. this.reload = reload;
  11. this.interval = interval;
  12. this.loadConfig();
  13. }
  14. public void loadConfig() {
  15. try {
  16. String log4jPath = "/opt/config/open_portal/opendev/appconfig/log4j.properties"+ "";
  17. //InputStream logis =new BufferedInputStream(new FileInputStream("D:/home/jk/platform/dev/webconfig/log4j.properties"));
  18. // 间隔特定时间,检测文件是否修改,自动重新读取配置
  19. //PropertyConfigurator.configure(logis);
  20. PropertyConfigurator.configureAndWatch(log4jPath, this.interval);
  21. log.debug( "log4j file path: " + log4jPath);
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }


配置spring文件:


   
   
   
   
  1. <bean class="com.cmcc.open.utils.Log4jConfig">
  2. <constructor-arg name="reload" value="true"/>
  3. <constructor-arg name="interval" value="60000"/>
  4. bean>


再说下log4j2.xml的配置方法,这是log4j的2版本的日志,与1.x不同,也有两种方法:

1、可运行的jar中指定(未尝试):

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
 
 
.......... //省略
 
        LoggerContext logContext = (LoggerContext) LogManager.getContext( false );
        File conFile = new File( "conf/logs/log4j2.xml" );
        logContext.setConfigLocation(conFile.toURI());
        logContext.reconfigure();
        logger.debug( "hello world...{}" , "How are you" );
 
 
........ //省略
2、在web.xml中指定:


   
   
   
   
  1. <context-param>
  2. <description>日志配置文件的路径 description>
  3. <param-name>log4jConfigLocation param-name>
  4. <param-value>file:/opt/config/open_portal/openapi/appconfig/log4j2.xml param-value>
  5. context-param>
  6. <listener>
  7. <listener-class>com.cmcc.open.common.Log4j2ConfigListener listener-class>
  8. listener>

   
   
   
   
  1. package com.cmcc.open.common;
  2. import java.util.Enumeration;
  3. import javax.servlet.ServletContextEvent;
  4. import javax.servlet.ServletContextListener;
  5. import org.apache.logging.log4j.core.config.Configurator;
  6. public class Log4j2ConfigListener implements ServletContextListener{
  7. private static final String KEY = "log4jConfigLocation";
  8. @Override
  9. public void contextDestroyed(ServletContextEvent arg0) {
  10. }
  11. @Override
  12. public void contextInitialized(ServletContextEvent arg0) {
  13. String fileName = getContextParam(arg0);
  14. Configurator.initialize( "Log4j2", fileName);
  15. }
  16. private String getContextParam(ServletContextEvent event) {
  17. Enumeration names = event.getServletContext().getInitParameterNames();
  18. while (names.hasMoreElements()){
  19. String name = names.nextElement();
  20. String value = event.getServletContext().getInitParameter(name);
  21. if(name.trim().equals(KEY)){
  22. return value;
  23. }
  24. }
  25. return null;
  26. }
  27. }








你可能感兴趣的:(转载)