log4j插入数据库,并生成HTML

log4j插入数据库,并生成HTML(一)
博客分类: Struts1.x
log4jHTMLSQLMySQLJDBC
上周接到了一个模块。需要实现Log4j监控 管理员的操作,并且存入数据库中,同时要在JSP页面显示结果,并生成HTML文件。每300条显示生成一条记录。
在经过自己的努力和师父等朋友们的 指导下,完成了。
今天贴出来,和大家分享,有好东西大家开源下,日后学习,都会用的上。
首先,先看log4j插入数据库的操作:

首先项目中导入Log4j的jar,配置好他的log4j.properties文件
Java代码 
log4j.rootLogger=WARN,DATABASE  
# imporin jdbc.jar  
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender  
# connection    
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test  
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver  
log4j.appender.DATABASE.user=root  
log4j.appender.DATABASE.password=viken  
# sql  
log4j.appender.DATABASE.sql=INSERT INTO log4j (Username,Class,Method,CreateTime,LogLevel,MSG) VALUES('%X{userName}','%C,%L','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m')  
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout 

log4j.rootLogger=WARN,DATABASE
# imporin jdbc.jar
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
# connection 
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
log4j.appender.DATABASE.user=root
log4j.appender.DATABASE.password=viken
# sql
log4j.appender.DATABASE.sql=INSERT INTO log4j (Username,Class,Method,CreateTime,LogLevel,MSG) VALUES('%X{userName}','%C,%L','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m')
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout

ps:sql 语句:Java代码 
log4j.appender.DATABASE.sql=INSERT INTO log4j (Username,Class,Method,CreateTime,LogLevel,MSG) VALUES('%X{userName}','%C,%L','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m') 

log4j.appender.DATABASE.sql=INSERT INTO log4j (Username,Class,Method,CreateTime,LogLevel,MSG) VALUES('%X{userName}','%C,%L','%M','%d{yyyy-MM-dd HH:mm:ss}','%p','%m')


接下来时在实现类中加入log4j调用。
Java代码 
import org.apache.log4j.Logger;  
import org.apache.log4j.MDC;   
 
public class DaoImp implements UserDao {  
    private static Logger log=Logger.getLogger(DaoImp.class);  
    public void Loign(User us) {  
          
    System.out.println("-----------"+us.getUsername()+"登陆啦"+"------------");  
    MDC.put("userName", us.getUsername());  
    log.warn("管理员登陆啦~!!!");  
      
    } 

import org.apache.log4j.Logger;
import org.apache.log4j.MDC;

public class DaoImp implements UserDao {
 private static Logger log=Logger.getLogger(DaoImp.class);
 public void Loign(User us) {
  
 System.out.println("-----------"+us.getUsername()+"登陆啦"+"------------");
 MDC.put("userName", us.getUsername());
 log.warn("管理员登陆啦~!!!");
 
 }

以上就完成了log4j的操作部分。

接下来 我们来查询他:

Java代码 
public List<Log4j> listall(int pageCount,int begin,int end) {  
        List<Log4j> list=new ArrayList();  
        Connection con=null;  
        Statement stmt=null;  
        ResultSet rs=null;  
        DB db=new DB();  
        String sql="select * from log4j order by CreateTime desc limit "+begin+","+end;  
      
        try {  
            stmt=db.getConnection().createStatement();  
            rs=stmt.executeQuery(sql);  
            while(rs.next()){  
                Log4j lo=new Log4j();  
                lo.setId(rs.getInt("id"));  
                lo.setUsername(rs.getString("Username"));  
                lo.setMethod(rs.getString("Method"));  
                lo.setClasss(rs.getString("Class"));  
                lo.setLogLevel(rs.getString("LogLevel"));  
                lo.setCreateTime(rs.getString("CreateTime"));  
                lo.setMessage(rs.getString("MSG"));  
                list.add(lo);  
            }  
        } catch (SQLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
        return list;  
    }  
      
    public int getPages(int pageCount) {  
 
        int pages = 0;  
        int allrecorders = 0;  
        DB db=new DB();  
        String sql="select count(id) as count from log4j order by CreateTime desc";  
        Statement stmt=null;  
        ResultSet rs = null;  
          
        try {  
            stmt=db.getConnection().createStatement();  
            rs=stmt.executeQuery(sql);  
            if(rs.next())  
                allrecorders = rs.getInt("count");  
        }catch(Exception e)  
        {  
            e.printStackTrace();  
        }  
        if(pageCount>0)  
        {  
                pages =  (allrecorders+pageCount-1)/pageCount ;  
              
        }  
        return pages;  
    }  
     

你可能感兴趣的:(log4j)