2

*1。我创建了 IMPLogger 接口 -> 必须由 Logger 实现类继承。2.创建MPLoggerImpl

    public class MPLoggerImpl implements IMPLogger
    `{
    private Logger logger;

     private static String MDC_CLASS = "Class";
     private static String MDC_METHOD = "Method";
     private static String MDC_USER = "User";

     private String loggerName = null;
     private LogConfigLocator logConfigLocator = null;

     public MPLoggerImpl(String loggerName, LogConfigLocator logConfigLocator)
     {
     this.loggerName = loggerName;
     this.logConfigLocator = logConfigLocator;
     loadLogInstance();
     }

    public MPLoggerImpl(String loggerName)
    {
    this.loggerName = loggerName;
    loadLogInstance();
    }

   public MPLoggerImpl(Logger logger)
   {
    this.logger = logger;
   }

   private void loadLogInstance()
   {
   //PropertyConfigurator.configure(loggerConfigFilePath);

  if (!AppRepositorySelector.isAppRepositoryInitialized())
   {
     AppRepositorySelector.init(logConfigLocator.getLog4jXMLPath());
   }

   logger = Logger.getLogger(loggerName);
   }


    public void log( int level, String msg )
   {
    log(level, msg, null);
   }



   public void log( int level, String curClass, String method, String msg, Throwable   throwable )
   {
  addFieldValueToLogMessage(MDC_CLASS, curClass);
  addFieldValueToLogMessage(MDC_METHOD, method);

    log(level, msg, throwable);


    resetMDC();
   }

 public void log( int level, String curClass, String method, String msg )
 {
  addFieldValueToLogMessage(MDC_CLASS, curClass);
  addFieldValueToLogMessage(MDC_METHOD, method);

  log(level, msg);

  /**
   * To remove all the key/values set so as to not affect the next log message. 
   */
  resetMDC();
}


  public void log(int level, String user, String curClass, String method, String msg)
  {
  addFieldValueToLogMessage(MDC_CLASS, curClass);
  addFieldValueToLogMessage(MDC_METHOD, method);
  addFieldValueToLogMessage(MDC_USER, user);

  log(level, msg);
  /**
   * To remove all the key/values set so as to not affect the next log message. 
   */
  resetMDC();
  }

   public void log(int level, String user, String curClass, String method, String msg, 
  {
  addFieldValueToLogMessage(MDC_CLASS, curClass);
  addFieldValueToLogMessage(MDC_METHOD, method);
  addFieldValueToLogMessage(MDC_USER, user);

  log(level, msg, excep);   

  /**
   * To remove all the key/values set so as to not affect the next log message. 
   */
  resetMDC();
 }

  public void log( int level, String msg, Throwable throwable )
 {
  switch (level)
  {
     case IMPLogger.TRACE:
        logger.trace(msg);
        break;
     case IMPLogger.DEBUG:
        if (throwable != null)
        {
           logger.debug(msg, throwable);
        }
        else
        {
           logger.debug(msg);
        }
        break;
     case IMPLogger.INFO:
        if (throwable != null)
        {
           logger.info(msg, throwable);
        }
        else
        {
           logger.info(msg);
        }
        break;
     case IMPLogger.WARN:
        if (throwable != null)
        {
           logger.warn(msg, throwable);
        }
        else
        {
           logger.warn(msg);
        }
        break;
     case IMPLogger.ERROR:
        if (throwable != null)
        {
           logger.error(msg, throwable);
        }
        else
        {
           logger.error(msg);
        }
        break;
     case IMPLogger.FATAL:
        if (throwable != null)
        {
           logger.fatal(msg, throwable);
        }
        else
        {
           logger.fatal(msg);
        }
        break;
     default:
        //System.out.println("Invalid Level, Please specify the level from the       CDMLogger interface.");
        break;
  }

}

  public void addFieldValueListToLogMessage( Map<String, Object> mdcMap )
 {
  if (mdcMap != null && mdcMap.size() > 0)
  {
     Iterator<String> itr = mdcMap.keySet().iterator();
     while (itr.hasNext())
     {
        String key = itr.next();
        Object value = mdcMap.get(key);
        MDC.put(key, value);
     }
   }
 }

  public void removeFieldValueForLogMessage( String key )
 {
  MDC.remove(key);
 }
   /**
   * Removes all the Keys values which will be reflected in the next log message call.
  * 
   */
   public void resetMDC()
  {
   Enumeration<String> keyItr = MDC.getContext().keys();

    while (keyItr.hasMoreElements())
  {
     MDC.remove(keyItr.nextElement());
  }
  }

    public void reloadLogger()
   {
   loadLogInstance();
   }`

    3.created LogConfigLocator
   public class LogConfigLocator
   {

   private String log4jXMLPath = null;
    public LogConfigLocator(String log4jXMLPath )
  {
   this.log4jXMLPath = log4jXMLPath;
  }



public String getLog4jXMLPath()
{
   return log4jXMLPath;
}

public void setLog4jXMLPath(String log4jXMLPath)
{
   this.log4jXMLPath = log4jXMLPath;
  }
  } `

4.最后我创建的 Apprepositry 选择器

 public class AppRepositorySelector implements RepositorySelector
'{
private static boolean initialized = false;
private static Object guard = LogManager.getRootLogger();


 public static synchronized void init(String log4jXMLPath) //throws ServletException
 {
   try
  {
     if (!initialized) // set the global RepositorySelector
     {
        defaultRepository = LogManager.getLoggerRepository();
        RepositorySelector theSelector = new AppRepositorySelector();
        LogManager.setRepositorySelector(theSelector, guard);
        initialized = true;
     }
     Hierarchy hierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
     //loadLog4JConfig(servletContext, hierarchy);
     loadLog4JConfig(hierarchy, log4jXMLPath);
     ClassLoader loader = Thread.currentThread().getContextClassLoader();
     repositories.put(loader, hierarchy);
  }
  catch (Exception e)
  {
     System.out.println("Exception while initializing AppRepositorySelector.");
     e.printStackTrace();
  }
 }

 public static synchronized void removeFromRepository()
{
  repositories.remove(Thread.currentThread().getContextClassLoader());
 }
     private static void loadLog4JConfig (Hierarchy hierarchy, String log4jXMLPath) throws Exception
   {
  try
  {
     InputStream log4JConfig = new FileInputStream(log4jXMLPath);
     Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(log4JConfig);
     DOMConfigurator conf = new DOMConfigurator();
     conf.doConfigure(doc.getDocumentElement(), hierarchy);
   }
  catch (Exception e)
   {
     throw new Exception(e);
  }

}

   public static boolean isAppRepositoryInitialized()
   {
  return initialized;
  }
   private AppRepositorySelector()
 {
   }
   public LoggerRepository getLoggerRepository()
   {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
  LoggerRepository repository = (LoggerRepository) repositories.get(loader);
  if (repository == null)
  {
     return defaultRepository;
  }
  else
  {
     return repository;
  }
  }
   }`
enter code here

但是在 My AppRepositorySelector 下面的行中抛出异常 LogManager.setRepositorySelector(theSelector, guard); 堆栈跟踪是

  18:17:11,189 ERROR [stderr] (MSC service thread 1-2)   java.lang.IllegalArgumentException: Attempted to reset the LoggerFactory without possessing the guard.
   18:17:11,190 ERROR [stderr] (MSC service thread 1-2)   at org.apache.log4j.LogManager.setRepositorySelector(LogManager.java:164)
  18:17:11,191 ERROR [stderr] (MSC service thread 1-2)   at com.mportal.logger.api.AppRepositorySelector.init(AppRepositorySelector.java:73)
  18:17:11,192 ERROR [stderr] (MSC service thread 1-2)   at com.mportal.logger.api.MPLoggerImpl.loadLogInstance(MPLoggerImpl.java:64)
  18:17:11,192 ERROR [stderr] (MSC service thread 1-2)   at com.mportal.logger.api.MPLoggerImpl.<init>(MPLoggerImpl.java:38)
  18:17:11,192 ERROR [stderr] (MSC service thread 1-2)   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 18:17:11,193 ERROR [stderr] (MSC service thread 1-2)   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
  18:17:11,194 ERROR [stderr] (MSC service thread 1-2)   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
 18:17:11,194 ERROR [stderr] (MSC service thread 1-2)   at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
 18:17:11,195 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:84)
  18:17:11,197 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
  18:17:11,198 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:237)
   18:17:11,198 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:893)
    18:17:11,199 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:803)
    18:17:11,200 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:437)
     18:17:11,200 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:404)
      18:17:11,201 ERROR [stderr] (MSC service thread 1-2)   at java.security.AccessController.doPrivileged(Native Method)
      18:17:11,201 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:375)
      18:17:11,202 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:263)
     18:17:11,204 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:170)
    18:17:11,205 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:260)
    18:17:11,205 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:184)
    18:17:11,206 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:163)
   18:17:11,206 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
   18:17:11,207 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
  18:17:11,207 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1210)
    18:17:11,208 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:978)
     18:17:11,209 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:462)
   18:17:11,209 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:404)
   18:17:11,210 ERROR [stderr] (MSC service thread 1-2)   at java.security.AccessController.doPrivileged(Native Method)
    18:17:11,210 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:375)
    18:17:11,211 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:263)
    18:17:11,211 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:170)
    18:17:11,212 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:260)
    18:17:11,212 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:184)
   18:17:11,215 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:163)
   18:17:11,215 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:430)
 18:17:11,216 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
 18:17:11,218 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
  18:17:11,220 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:254)
  18:17:11,221 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:198)
  18:17:11,224 ERROR [stderr] (MSC service thread 1-2)   at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
 18:17:11,225 ERROR [stderr] (MSC service thread 1-2)   at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392)
18:17:11,225 ERROR [stderr] (MSC service thread 1-2)   at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850)
18:17:11,226 ERROR [stderr] (MSC service thread 1-2)   at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90)
18:17:11,227 ERROR [stderr] (MSC service thread 1-2)   at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
 18:17:11,227 ERROR [stderr] (MSC service thread 1-2)   at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
18:17:11,228 ERROR [stderr] (MSC service thread 1-2)   at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
18:17:11,228 ERROR [stderr] (MSC service thread 1-2)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
18:17:11,229 ERROR [stderr] (MSC service thread 1-2)   at java.lang.Thread.run(Thread.java:662)

Spring xml 是 Log4J xml 是 </logger -->

请任何人帮我解决这个问题*

4

0 回答 0