系统日志管理

  • (1)分析
    记录用户的访问的Controller与ip等信息
    属于监控功能
  • Day39项目saas-export项目-系统日志管理**_java

只要查看与保存功能

  • (2)数据组成

//设置登录用户信息 ​​seesion获取​​​ //设置企业信息
//IP地址 ​​request获取​​ //设置记录时间
//执行的方法名称 ​​新方法获取​​ //执行的类名称

Day39项目saas-export项目-系统日志管理**_xml_02

系统日志管理

两个功能,一个是分页显示,一个是保存日志
》SysLog
》TestSysLogService
》ISysLogService,SysLogServiceImpl
》 ISysLogDao,ISysLogDao.xml
》SysLogController,xxx.jsp

SysLog

public class SysLog {
private String id ;
private String userName ;
private String ip ;
private Date time ;
private String method ;
private String action ;
private String companyId ;
private String companyName ;

TestSysLogService

分页查询
添加

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
public class TestSysLogService {
private static final Logger l = LoggerFactory.getLogger(TestSysLogService.class);
@Autowired
ISysLogService iSysLogService;
//增*删改查*
@Test
public void test01(){
//分页列表
//页面上显示分页列表,就要求业务方法中提供查询PageInfo的方法
PageInfo<SysLog> pi= iSysLogService.findByPage(1,3,"1");
l.info("pi = "+pi);
}
@Test
public void test02(){

//将一个表单数据保存在javaBean中,再将javaBean存到数据库
SysLog sysLog = new SysLog();
//设置登录用户信息
sysLog.setUserName("老玉");
//设置企业信息
sysLog.setCompanyId("1");
sysLog.setCompanyName("吉首大学");
//IP地址
sysLog.setIp("192.168.10.11");
//设置记录时间
sysLog.setTime(new Date());
//执行的方法名称
sysLog.setMethod("toList");
//执行的类名称
sysLog.setAction("com.wzx.web.company.CompanyController");

sysLog.setCompanyId("1");
sysLog.setCompanyName("吉首大学");
iSysLogService.saveSysLog(sysLog);

}


}

ISysLogService,SysLogServiceImpl

public interface ISysLogService {
PageInfo<SysLog> findByPage(int curr, int pageSize, String companyId);
void saveSysLog(SysLog sysLog);
}
@Service
public class SysLogServiceImpl implements ISysLogService {
//service调用dao
@Autowired
ISysLogDao iSysLogDao;
@Override
public PageInfo<SysLog> findByPage(int curr, int pageSize, String companyId) {
//设置参数
PageHelper.startPage(curr,pageSize);
//调用全查
List<SysLog> list = iSysLogDao.findAll(companyId);
//包装成PageInfo
PageInfo<SysLog> pi = new PageInfo<>(list);
return pi;
}

@Override
public void saveSysLog(SysLog sysLog) {
String uuid= UUID.randomUUID().toString();
sysLog.setId(uuid);
iSysLogDao.save(sysLog);
}
}

ISysLogDao,ISysLogDao.xml

public interface ISysLogDao {
List<SysLog> findAll(String companyId);
void save(SysLog sysLog);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--namespace: 需要映射的Dao接口类型-->
<mapper namespace="com.wzx.dao.system.syslog.ISysLogDao">
<!-- 配置映射 字段-->
<resultMap id="syslogMap" type="sysLog">
<id column="id" property="id" />
<result column="user_name" property="userName" />
<result column="ip" property="ip" />
<result column="time" property="time" />
<result column="method" property="method" />
<result column="ACTION" property="action" />
<result column="company_id" property="companyId" />
<result column="company_name" property="companyName" />
</resultMap>
<!-- List<SysLog> findAll(String companyId);-->
<select id="findAll" parameterType="string" resultMap="syslogMap">
select * from st_sys_log where company_id =#{companyId} order by time desc
</select>
<!-- void save(SysLog sysLog);-->
<insert id="save" parameterType="sysLog">
insert into st_sys_log
(
id ,
user_name ,
ip ,
time ,
method ,
ACTION ,
company_id ,
company_name
)
values
(
#{id },
#{userName },
#{ip },
#{time },
#{method },
#{action },
#{companyId },
#{companyName }
)
</insert>
</mapper>

SysLogController,xxx.jsp

@Controller
@RequestMapping("/system/syslog")
public class SysLogController extends BaseController {
private static final Logger l = LoggerFactory.getLogger(SysLogController.class);

@Autowired
ISysLogService iSysLogService;



@RequestMapping(path = "/toList", method = {RequestMethod.GET, RequestMethod.POST})
public String toList(@RequestParam(defaultValue = "1") int curr, @RequestParam(defaultValue = "3") int pageSize) {
//调查询分页列表的方法
PageInfo<SysLog> pi = iSysLogService.findByPage(curr, pageSize, getLoginCompanyId());
//将pi添加到页面
request.setAttribute("pi", pi);
return "system/syslog/log-list";
}
}
<c:forEach items="${pi.list}" var="log"  varStatus="st">
<tr>
<td>${st.count }</td>
<td>${log.userName }</td>
<td>${log.ip}</td>
<td>${log.time}</td>
<td>${log.action}.${log.method}</td>

</tr>
</c:forEach>

系统日志管理 优化日志列表

  • (1)分析
    日志记录多,生成的页号按钮多
  • (2)实现
    控制循环次数
    并进行边界判断

Day39项目saas-export项目-系统日志管理**_java_03


Day39项目saas-export项目-系统日志管理**_spring_04

<c:forEach begin="${pi.pageNum-5 <= 0 ? 1:pi.pageNum-5 }" end="${pi.pageNum+5>pi.pages?pi.pages:pi.pageNum+5}"
var="i">
<li class="paginate_button ${pi.pageNum==i ? 'active':''}"><a href="javascript:goPage(${i})">${i}</a></li>
</c:forEach>