日期:2014-05-20  浏览次数:20967 次

SSH2使用hibernate对Log4j日志文件写入数据库
因为用默认的写入太繁琐所以我决定用hibernate写入,好像是spring方法注入错误我想问下是不是启动顺序有问题。希望各位大虾能够给个好点的解决方案。最好有例子只能给100分。全部奉上
log4j实现类
Java code

package com.onionportal.until.dao.imp;
import java.util.Calendar;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.onionportal.orm.SystemLog;
import com.onionportal.until.dao.SystemLogDao;
public class SystemLogDaoImp extends AppenderSkeleton implements SystemLogDao{
    private HibernateTemplate hibernateTemplate;

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }
    @Override
    protected void append(LoggingEvent logEvent) {
        SystemLog systemlog=new SystemLog();
        systemlog.setId(1);
        systemlog.setUserId(1);
        systemlog.setMessageLevel(1);
        systemlog.setCreateTime(Calendar.getInstance().getTime());
        systemlog.setStackTrace(logEvent.getThreadName());
        systemlog.setMessageBody(String.valueOf(logEvent.getMessage()));
        hibernateTemplate.find(" from SystemLog");
    }

    public void close() {
        
    }

    public boolean requiresLayout() {
        
        return false;
    }
}


Spring配置文件
Java code

    <bean id="sytemDataDao" class="com.onionportal.until.dao.imp.SystemLogDaoImp">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>



Log4j配置文件
Java code

log4j.rootLogger=debug,console,db

log4j.logger.org=ERROR
log4j.logger.com.comp=ERROR
log4j.logger.net.sf.hibernate=ERROR
log4j.logger.net.sf.hibernate.SQL=ERROR
log4j.logger.net.sf.hibernate.type=ERROR 
log4j.logger.net.sf.ehcache=ERROR
log4j.logger.org.springframework=ERROR
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%n  Level: %p%n   Time: %d%n Method: %l%nMessage: %m%n
log4j.appender.db=com.onionportal.until.dao.imp.SystemLogDaoImp
log4j.appender.db.BufferSize=100
log4j.appender.db.layout=org.apache.log4j.PatternLayout


错误日志
Java code

Unable to load configuration. - Class: com.onionportal.until.dao.imp.SystemLogDaoImp
File: SystemLogDaoImp.java
Method: append
Line: 36 - com/onionportal/until/dao/imp/SystemLogDaoImp.java:36:-1
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418)
    at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3693)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:761)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:741)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1023)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:448)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: Error loading configuration file struts-default.xml - Class: com.onionportal.until.dao.imp.SystemLogDaoImp
File: SystemLogDaoImp.java
Method: append
Line: 36 - com/onionportal/until/dao/imp/SystemLogDaoImp.java:36:-1
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadDocuments(XmlConfigurationProvider.java:147)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.init(XmlConfigurationProvider.java:110)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:168)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55)
    ... 31 more
Caused by: java.lang.NullPointerException
    at com.onionportal.until.dao.imp.SystemLogDaoImp.append(SystemLogDaoImp.java:36)
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
    at org.apache.log4j.Category.callAppenders(Category.java:206)
    at org.apache.log4j.Category.forcedLog(Category.java:391)
    at org.apache.log4j.Category.log(Category.java:856)
    at org.apache.commons.logging.impl.Log4JLogger.debug(Log4JLogger.java:177)
    at com.opensymphony.xwork2.util.logging.commons.CommonsLogger.debug(CommonsLogger.java:57)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadConfigurationFiles(XmlConfigurationProvider.java:858)
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadDocuments(XmlConfigurationProvider.java:143)
    ... 34 more
2011-6-8 1:59:00 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter struts2
java.lang.NullPointerException
    at com.onionportal.until.dao.imp.SystemLogDaoImp.append(SystemLogDaoImp.java:36)
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
    at org.apache.log4j.Category.callAppenders(Category.java:206)
    at org.apache.log4j.Category.forcedLog(Category.java:391)
    at org.apache.log4j.Category.log(Category.java:856)
    at org.apache.commons.logging.impl.Log4JLogger.error(Log4JLogger.java:257)
    at com.opensymphony.xwork2.util.logging.commons.CommonsLogger.error(CommonsLogger.java:27)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:430)
    at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:69)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)