Nginx 日志处理
/**
* 日志根目录、默认目录
*/
@Value("${log.monitor.defaultPath}")
private String logRootPath;
File file = null;
FileChannel channel = null;
long lastModified = 0;
boolean isInit = false;
private void init() {
try {
file = new File(logRootPath);
channel = new FileInputStream(file).getChannel();
channel.position(channel.size());
lastModified = file.lastModified();
} catch (Exception e) {
log.info("监控文件失败,检查路径是否正确");
e.printStackTrace();
}
isInit = true;
}
//3.添加定时任务
@Scheduled(cron = "1/10 * * * * ?")
public void doJob()
{
if (!isInit)
{
init();
}
long now = file.lastModified();
if (now != lastModified)
{
String newContent = getNewContent(channel);
if(newContent==null)
{
return;
}
String[] line=newContent.split("\n");
for(String l:line)
{
try {
LogRequest logRequest = new LogRequest();
String[] datas = l.split(" \"");
if(datas.length<5)
{
continue;
}
logRequest.setCreatedTime(new Date());
logRequest.setRemoteAddr(datas[0].split(" -")[0]);
String[] cc = datas[1].split("\" ");
logRequest.setUrl(cc[0]);
String[] sl = cc[1].split(" ");
logRequest.setHttpStatus(sl[0]);
logRequest.setBodyBytesSent(sl[1]);
logRequest.setHttpReferer(datas[2].substring(0, datas[2].length() - 1));
logRequest.setHttpUserAgent(datas[3].substring(0, datas[3].length() - 1));
logRequest.setRealIp(datas[4].split("\"")[0]);
logRequestService.insertLogRequest(logRequest);
}catch (Exception e){}
}
lastModified = now;
}
}
private ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 100);
private CharBuffer charBuffer = CharBuffer.allocate(1024 * 50);
private CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
private String getNewContent(FileChannel channel) {
try {
byteBuffer.clear();
charBuffer.clear();
int length = channel.read(byteBuffer);
if (length != -1) {
byteBuffer.flip();
decoder.decode(byteBuffer, charBuffer, true);
charBuffer.flip();
return charBuffer.toString();
} else {
channel.position(channel.size());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
全部评论