package base.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.com.pubwin.police.po.User;
import cn.com.pubwin.police.service.SysLogService;
import cn.com.pubwin.police.vo.SysLogVo;
/**
* 系统日志记录
* @author ZhengLing
*
*/
public class LogUtils {
private static Logger logger = LoggerFactory.getLogger(LogUtils.class);
private static SysLogService sysLogService=(SysLogService)SpringFactoryUtils.getInstance().getBean("sysLogService");
/**
* 插入系统日志
* @param moduleCode 模块代码
* @param operateContent 操作内容
* @param operateType 操作类型 (见FinalPropertiVo类中关于operType的定义)
*/
public static void insertSysLog(String moduleCode, String operateContent, String operateType) throws Exception{
SysLogVo sysLogVo = new SysLogVo();
sysLogVo.setModuleCode(moduleCode);
sysLogVo.setOperateContent(operateContent);
sysLogVo.setOperateType(operateType);
try {
sysLogService.saveSysLog(sysLogVo);
} catch (Exception e) {
logger.error("插入系统日志时,出现错误:",e);
}
}
/**
* 当用户退出时,插入系统日志
* @param user 用户
*/
public static void insertSysLogUserLogout(User user){
sysLogService.saveSysLogUserLogout(user);
}
}
----insertSysLogUserLogout(User user) 使用说明:
---//当用户推出,活着关闭浏览器时记录 用户退出的日志
package cn.com.pubwin.police.listener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import base.util.LogUtils;
import cn.com.pubwin.police.po.User;
public class SessionListener implements HttpSessionListener {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private int stat= 0;
@Override
public void sessionCreated(HttpSessionEvent event) {
// TODO Auto-generated method stub
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
//当用户推出,活着关闭浏览器时记录 用户退出的日志
User loginUser =null;
try {
try {
loginUser=(User)event.getSession().getAttribute("user");
logger.info("用户:"+loginUser);
} catch (Exception e) {
}
if(loginUser!=null){
LogUtils.insertSysLogUserLogout(loginUser);
}
} catch (Exception e) {
logger.error("用户注销,插入系统日志失败:",e);
}
}
}
----注意:SessionListener 要在web.xml文件中配置,相同启动后会一直监控用户的状态
部分配置如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
cn.com.pubwin.police.listener.SessionListener
</listener-class>
</listener>
--- insertSysLog(String moduleCode, String operateContent, String operateType) 使用说明:
/**
* 统计信息按日按月查看
*
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/ajaxOfflineTmAndNumberByCompany")
public String ajaxOfflineTmAndNumberByCompany(HttpServletRequest request, HttpServletResponse response) {
StringBuffer sb = new StringBuffer();
try {
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
List<String> list = DateUtil.getPreMonthByFormat(new Date(), 7, "yyyy-MM");
String type = request.getParameter("type");// 0.月数据 1.日数据
String date = request.getParameter("date"); // 页面返回的时间
String companyId = user.getCompanyId() + "";
if (type.equals("0")) {
if (StringUtils.isEmpty(date)) {
date = list.get(0).replace("-", "");
} else {
date = date.replace("-", "");
}
if (StringUtils.isNotEmpty(companyId)) {
OfflineVo monthOfflineVo = new OfflineVo();
monthOfflineVo.setOutlineNumberCount(0);
monthOfflineVo.setOutlineTimeCount(0);
List<OfflineVo> offlineVoList = this.reportService.getOfflineVoByCompany(date.replace("-", ""), 1);
for (OfflineVo offlineVo : offlineVoList) {
if (offlineVo.getCode().toString().equals(companyId.toString())) {
monthOfflineVo = offlineVo;
break;
}
}
String radit = "0%";
if (monthOfflineVo != null) {
sb.append(monthOfflineVo.getOutlineTimeCount().toString() + ","
+ monthOfflineVo.getOutlineNumberCount() + "," + radit);
}
}
} else if (type.equals("1")) {
if (StringUtils.isEmpty(date)) {
date = DateUtil.formatDate(DateUtil.getYesterdayEnd(new Date()), "yyyyMMdd");
} else {
date = date.replace("-", "");
}
String radit = "0%";
if (companyId != null) {
OfflineVo dayOfflineVo = new OfflineVo();
dayOfflineVo.setOutlineNumberCount(0);
dayOfflineVo.setOutlineTimeCount(0);
List<OfflineVo> offlineVoList = this.reportService.getOfflineVoByCompany(date.replace("-", ""), 1);
for (OfflineVo offlineVo : offlineVoList) {
if (offlineVo.getCode().toString().equals(companyId.toString())) {
dayOfflineVo = offlineVo;
break;
}
}
if (dayOfflineVo != null) {
sb.append(dayOfflineVo.getOutlineTimeCount() + "," + dayOfflineVo.getOutlineNumberCount() + ","
+ radit);
}
}
}
response.getWriter().print(sb.toString());
// 开始记录操作日志
String searchStr = null;
if (type.equals("0"))
searchStr = "按月查看";
else
searchStr = "按日查看";
String operateContent = "厂商离线统计查询开始>>查询条件:" + searchStr + ",时间:" + date + "";
LogUtils.insertSysLog("companyInfoReport", operateContent, FinalPropertiVo.operType_select);
} catch (Exception e) {
try {
response.getWriter().print("0,0,0%");
} catch (IOException e1) {
logger.error("", e1);
}
logger.error(e.getMessage(), e);
}
return null;
}