aop
package com.gtf.rz.Test02;
import org.springframework.beans.factory.annotation.Autowired;
@Aspect
@Component
@Slf4j
public class AopLog {
private static final String START_TIME = "request-start";
private SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
@Autowired
private LogMange logMange;
/**
* 切入点
*/
@Pointcut("execution(public * com.gtf.rz.Test02.*(..))")
public void log() {
}
/**
* 前置操作
*
* @param point 切入点
*/
@Before("log()")
public void beforeLog(JoinPoint point) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
logMange.addLog("【请求 时间】:" + sdf4.format(new Date()));
logMange.addLog("【请求 URL】:" + request.getRequestURL());
logMange.addLog("【请求 IP】:" + request.getRemoteAddr());
logMange.addLog("【类名 Class】:" + point.getSignature().getDeclaringTypeName());
logMange.addLog("【方法名 Method】:" + point.getSignature().getName());
logMange.addLog("【请求参数 Args】:" +
JSON.toJSONString(point.getArgs())
);
// log.info("【请求 时间】:{}", System.currentTimeMillis());
// log.info("【请求 URL】:{}", request.getRequestURL());
// log.info("【请求 IP】:{}", request.getRemoteAddr());
// log.info("【类名 Class】:{},【方法名 Method】:{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName());
// log.info("【请求参数 Args】:{},", point.getArgs());
}
}
文件操作工具类
package com.gtf.rz.Test02;
public class FileUtils {
/*判断文件是否存在*/
public static boolean isExists(String filePath) {
File file = new File(filePath);
return file.exists();
}
/*判断是否是文件夹*/
public static boolean isDir(String path) {
File file = new File(path);
if (file.exists()) {
return file.isDirectory();
} else {
return false;
}
}
/**
* 文件或者目录重命名
*
* @param oldFilePath 旧文件路径
* @param newName 新的文件名,可以是单个文件名和绝对路径
* @return
*/
public static boolean renameTo(String oldFilePath, String newName) {
try {
File oldFile = new File(oldFilePath);
//若文件存在
if (oldFile.exists()) {
//判断是全路径还是文件名
if (newName.indexOf("/") < 0 && newName.indexOf("\\") < 0) {
//单文件名,判断是windows还是Linux系统
String absolutePath = oldFile.getAbsolutePath();
if (newName.indexOf("/") > 0) {
//Linux系统
newName = absolutePath.substring(0, absolutePath.lastIndexOf("/") + 1) + newName;
} else {
newName = absolutePath.substring(0, absolutePath.lastIndexOf("\\") + 1) + newName;
}
}
File file = new File(newName);
//判断重命名后的文件是否存在
if (file.exists()) {
System.out.println("该文件已存在,不能重命名");
} else {
//不存在,重命名
return oldFile.renameTo(file);
}
} else {
System.out.println("原该文件不存在,不能重命名");
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/*文件拷贝操作*/
public static void copy(String sourceFile, String targetFile) {
File source = new File(sourceFile);
File target = new File(targetFile);
target.getParentFile().mkdirs();
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel in = null;
FileChannel out = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(target);
in = fis.getChannel();//得到对应的文件通道
out = fos.getChannel();//得到对应的文件通道
in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*读取Text文件操作*/
public static String readText(String filePath) {
String lines = "";
try {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
lines += line + "\n";
}
} catch (Exception e) {
e.printStackTrace();
}
return lines;
}
/*写入Text文件操作*/
public static void writeText(String filePath, String content, boolean isAppend) {
FileOutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
try {
outputStream = new FileOutputStream(filePath, isAppend);
outputStreamWriter = new OutputStreamWriter(outputStream);
bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(content);
bufferedWriter.newLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter != null) {
bufferedWriter.close();
}
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
日志实现
package com.gtf.rz.Test02;
import org.springframework.util.StringUtils;
import java.util.concurrent.LinkedBlockingDeque;
public class LogMange {
//定义一个日志的缓存 容器
private LinkedBlockingDeque<String> logQueue = new LinkedBlockingDeque<String>();
private logThread logThread;
private String logPath="/Users/gaotengfei/Desktop/log/";
public LogMange(){
logThread = new logThread();
logThread.start();
}
//添加日志
public void addLog(String log){
logQueue.add(log);
}
//获取日志
public String getLog(){
return logQueue.poll();
}
class logThread extends Thread{
@Override
public void run() {
while(true){
String poll =getLog();
if(!StringUtils.isEmpty(poll)) {
//将日志写入文件
FileUtils.writeText(logPath,poll,true);
}
}
}
}
}