package com.ruoyi.framework.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.utils.IpUtils; /**
* 处理并记录日志文件
*
* @author ruoyi
*/
public class LogUtils
{
public static final Logger ERROR_LOG = LoggerFactory.getLogger("sys-error");
public static final Logger ACCESS_LOG = LoggerFactory.getLogger("sys-access"); /**
* 记录访问日志 [username][jsessionid][ip][accept][UserAgent][url][params][Referer]
*
* @param request
* @throws Exception
*/
public static void logAccess(HttpServletRequest request) throws Exception
{
String username = getUsername();
String jsessionId = request.getRequestedSessionId();
String ip = IpUtils.getIpAddr(request);
String accept = request.getHeader("accept");
String userAgent = request.getHeader("User-Agent");
String url = request.getRequestURI();
String params = getParams(request); StringBuilder s = new StringBuilder();
s.append(getBlock(username));
s.append(getBlock(jsessionId));
s.append(getBlock(ip));
s.append(getBlock(accept));
s.append(getBlock(userAgent));
s.append(getBlock(url));
s.append(getBlock(params));
s.append(getBlock(request.getHeader("Referer")));
getAccessLog().info(s.toString());
} /**
* 记录异常错误 格式 [exception]
*
* @param message
* @param e
*/
public static void logError(String message, Throwable e)
{
String username = getUsername();
StringBuilder s = new StringBuilder();
s.append(getBlock("exception"));
s.append(getBlock(username));
s.append(getBlock(message));
ERROR_LOG.error(s.toString(), e);
} /**
* 记录页面错误 错误日志记录 [page/eception][username][statusCode][errorMessage][servletName][uri][exceptionName][ip][exception]
*
* @param request
*/
public static void logPageError(HttpServletRequest request)
{
String username = getUsername(); Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
String message = (String) request.getAttribute("javax.servlet.error.message");
String uri = (String) request.getAttribute("javax.servlet.error.request_uri");
Throwable t = (Throwable) request.getAttribute("javax.servlet.error.exception"); if (statusCode == null)
{
statusCode = 0;
} StringBuilder s = new StringBuilder();
s.append(getBlock(t == null ? "page" : "exception"));
s.append(getBlock(username));
s.append(getBlock(statusCode));
s.append(getBlock(message));
s.append(getBlock(IpUtils.getIpAddr(request))); s.append(getBlock(uri));
s.append(getBlock(request.getHeader("Referer")));
StringWriter sw = new StringWriter(); while (t != null)
{
t.printStackTrace(new PrintWriter(sw));
t = t.getCause();
}
s.append(getBlock(sw.toString()));
getErrorLog().error(s.toString()); } public static String getBlock(Object msg)
{
if (msg == null)
{
msg = "";
}
return "[" + msg.toString() + "]";
} protected static String getParams(HttpServletRequest request) throws Exception
{
Map<String, String[]> params = request.getParameterMap();
return JSON.marshal(params);
} protected static String getUsername()
{
return (String) SecurityUtils.getSubject().getPrincipal();
} public static Logger getAccessLog()
{
return ACCESS_LOG;
} public static Logger getErrorLog()
{
return ERROR_LOG;
}
}

ruoyi LogUtils的更多相关文章

  1. LogUtils.java

    package com.xdsjs.save.utils; /** * 日志相关管理类 * Created by xdsjs on 2015/10/13. */ import android.util ...

  2. LogUtils日志管理工具

    public class LogUtils { public static final int VERBOSE = 1; public static final int DEBUG = 2; publ ...

  3. Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件

    Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件 在开发中,我们常常用打印log的方式来调试我们的应用.在Java中我们常常使用方法System.out ...

  4. 利用 Ruoyi 开发自己的业务管理系统__测试结构完成

    前言铺垫不多说 (1)Ruoyi这个平台不错:如果你觉得你比Ruoyi的作者牛逼,你就不用看我这个文章了,你可以走了,因为我自认为比Ruoyi的作者要烂: (2)必须已经成功搭建Ruoyi,并能在自己 ...

  5. 竟然把Ruoyi在我自己的Eclipse编译成功,并能跑通了。。。。服了我自己了

    前几天,下载最新ECLISPSE2019压缩包,解压缩成功,没提示不是免费:eclipse-jee-2019-12-R-win32-x86_64.zip然后我配置好了maven于是我1月2日晚一时兴起 ...

  6. ruoyi ShiroUtils

    package com.ruoyi.framework.util; import org.apache.shiro.SecurityUtils; import org.apache.shiro.cry ...

  7. ruoyi BeanUtils

    package com.ruoyi.common.utils.bean; import java.lang.reflect.Method; import java.util.ArrayList; im ...

  8. ruoyi HttpUtils

    package com.ruoyi.common.utils.http; import java.io.BufferedReader; import java.io.IOException; impo ...

  9. ruoyi IpUtils

    package com.ruoyi.common.utils; import java.net.InetAddress; import java.net.UnknownHostException; i ...

随机推荐

  1. Typecho博客添加版权说明

    版权声明是指作品权利人对自己创作作品的权利的一种口头或书面声明,一般版权声明应该包括权利归属.作品使用准许方式.责任追究等方面的内容.诸如平时看文章时最后会有一个严禁转载的说明,其实这就是版权声明. ...

  2. LCT(1)

    LCT(Link-Cut Tree,动态树)是一个支持动态修改树的结构的数据结构,其基本操作有 \(\texttt{access}\) , \(\texttt{findroot}\) , \(\tex ...

  3. CSU 1126 DFS前缀和

    在一棵树上找影响最小的某个点,某个点的影响是等于其他点到他的距离*其他点的权值 的和 我一开始也找不到什么好的方法,只能想到每个点暴力去判断,但是这样肯定会超时(10^5个点),又有点想用类似前缀和, ...

  4. POJ 3311 Hie with the Pie 最短路+状压DP

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11243   Accepted: 5963 ...

  5. 在mac电脑的terminal里该如何运行c语言

    若要在 Mac 的终端中编译并运行 C 源代码,你首先需要安装 Command Line Tools,里面包含有 GCC 编译器.安装方法为: 打开终端,输入 gcc. 如果你没有安装 Command ...

  6. Egret Engine 2D - Get Started

    Get Started     Egret 也支持在命令行完成编译,运行,发布等操作.在下面的教程中会穿插对应操作的命令行代码.   可新建游戏项目,也可建eui项目   这里包含默认的几个库,egr ...

  7. FMDB 加入数据库版本信息

    //创建数据库版本表 if (![db tableExists:@"tb_dbInfo"]) { result = [db executeUpdate:@"CREATE ...

  8. s5pc100开发板uboot移植

    相关软件下载地址:http://pan.baidu.com/s/16yo8Y 适用于fsc100开发板 交叉编译器arm-cortex_a8-linux-gnueabi-gcc u-boot-2010 ...

  9. POJ 1416:Shredding Company

    Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4713   Accepted: 2714 ...

  10. POJ 3970:Party

    Party Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status ...