当前位置:网站首页>Server log analysis tool (identify, extract, merge, and count exception information)
Server log analysis tool (identify, extract, merge, and count exception information)
2022-04-23 16:31:00 【Spilled off】
demand : I need to check yesterday's log today , And pay attention to the exceptions , In order to deal with some of these problems .
Manual reading takes a long time , Generally, there are more than ten log files a day , It's impossible to see one by one , And a lot of log information is info Information , No need to pay attention . So a gadget is implemented to automatically read all log files , Extract the exception information , For business exceptions, the same are merged together , In this way, you only need to view the generated exception information file , It saves a lot of time . Here is the code implementation :
import com.google.common.collect.Maps;
import org.springframework.util.CollectionUtils;
import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
/** * @author visy.wang * @desc Log reading * @date 2020/11/25 10:39 */
public class LogReader {
// Keywords for extracting exception information
private static final String[] KEY_WORDS = {
"xception", "at "};
// Save statistics of business exceptions
private static Map<String, Integer> errMap = Maps.newHashMap();
/** * Read exception information * @param folder Folder path * @param ignores Ignored keywords ( If there is a keyword in it, skip ) */
private static void readException(String folder, String... ignores) throws Exception{
File dir = new File(folder);
File[] fileArr = dir.listFiles();
if(fileArr==null || fileArr.length==0){
System.out.println(" The folder is empty ");
return;
}
// decompression
for(File in: fileArr){
if(!in.isDirectory() && in.getName().endsWith(".gz")){
unGzip(in);
}
}
// Filter & Sort
List<File> files = Arrays.stream(fileArr).filter(f -> {
String name = f.getName();
// Only deal with debug.*.log file , Modifiable
return !f.isDirectory() && name.startsWith("debug") && name.endsWith(".log");
}).sorted(Comparator.comparing(f->{
// File sorting , You can delete this part of the code without sorting , Or modify other sorting rules
// Name example :debug.2020-11-24.1.log, Extract numbers 1 Used to sort
String name = f.getName();
int secondIndex = name.indexOf(".",name.indexOf(".")+1);
String index = name.substring(secondIndex+1, name.lastIndexOf("."));
return Integer.parseInt(index);
})).collect(Collectors.toList());
if(CollectionUtils.isEmpty(files)){
System.out.println(" Log file not found ");
return;
}
// Create output file
String yesterday = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String outputFileName = "exceptions."+ yesterday +".log";
File outputFile = new File(folder+"\\"+outputFileName);
if(!outputFile.exists()){
if(!outputFile.createNewFile()){
System.out.println(outputFileName+" Create failure !!!");
}
}
FileWriter writer = new FileWriter(outputFile);
for(File file: files){
String fileName = file.getName();
System.out.println(" Reading :"+fileName+" ...");
writer.write("LOG_FILE_NAME-------------------->"+ fileName+"\n");
String tempStr;
BufferedReader reader = new BufferedReader(new FileReader(file));
boolean flag = false;
while ((tempStr = reader.readLine()) != null) {
if(isIncludes(tempStr, KEY_WORDS) && !isIncludes(tempStr, ignores)){
// Control business exceptions to print only once ( Merge )
//ServiceException Is the custom business exception name of my project , It can be modified according to its own name
if(tempStr.contains("ServiceException:")){
String errMsg = tempStr.substring(tempStr.indexOf(":")+1).trim();
Integer count;
if((count=errMap.get(errMsg))==null){
errMap.put(errMsg, 1);
flag = false;
}else{
errMap.put(errMsg, count+1);
flag = true;
continue;
}
}else{
if(tempStr.contains("at ")){
if(flag){
continue;
}
}else{
flag = false;
}
}
writer.write(tempStr+"\n");
}
}
reader.close();
}
writer.write("\n\n-------------- Business exception statistics -------------------\n");
for(String k : errMap.keySet()){
writer.write(k+": "+ errMap.get(k)+" Time \n");
}
writer.close();
System.out.println(" Exception file generation completed !!!");
}
// Whether to find the keyword
private static boolean isIncludes(String tempStr, String[] keyWords){
for(String keyWord: keyWords){
if(tempStr.contains(keyWord)){
return true;
}
}
return false;
}
// decompression
private static void unGzip(File inFile) throws Exception{
System.out.println(" Decompressing :"+ inFile.getName() + "...");
// establish gzip Compressed file input stream
FileInputStream fin = new FileInputStream(inFile);
// establish gzip Decompression workflow
GZIPInputStream gzin = new GZIPInputStream(fin);
// Establish the output stream of decompressed files
String path = inFile.getAbsolutePath();
String outPath = path.substring(0, path.lastIndexOf('.'));
FileOutputStream fout = new FileOutputStream(outPath);
int num;
byte[] buf=new byte[1024];
while ((num = gzin.read(buf,0,buf.length)) != -1) {
fout.write(buf,0,num);
}
gzin.close();
fout.close();
fin.close();
// Delete source file
inFile.delete();
}
public static void main(String[] args) throws Exception{
// Put all the log files to be analyzed on the desktop logs Under the folder , The file must be decompressed .log file
// After the analysis, it will be in logs Generate under folder exceptions.yyyy-MM-dd.log file , You can see it.
// All business exceptions will only print the exception information once , At the end of the file, there are the number of exceptions
readException("C:\\Users\\admin\\Desktop\\logs", "aaa", "bbb");
}
}
版权声明
本文为[Spilled off]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231403163430.html
边栏推荐
- Redis "8" implements distributed current limiting and delay queues
- 最详细的背包问题!!!
- 七朋元视界可信元宇宙社交体系满足多元化的消费以及社交需求
- Compile, connect -- Notes
- vim编辑器的实时操作
- 欣旺达:HEV和BEV超快充拳头产品大规模出货
- The system research problem that has plagued for many years has automatic collection tools, which are open source and free
- 05 Lua 控制结构
- Construction of esp32 compilation environment
- Day (2) of picking up matlab
猜你喜欢

The first line and the last two lines are frozen when paging

Day (5) of picking up matlab

You need to know about cloud disaster recovery

Install redis and deploy redis high availability cluster

TIA botu - basic operation

The solution of not displaying a whole line when the total value needs to be set to 0 in sail software

Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons

JMeter setting environment variable supports direct startup by entering JMeter in any terminal directory

Nacos detailed explanation, something

RecyclerView advanced use - to realize drag and drop function of imitation Alipay menu edit page
随机推荐
Set cell filling and ranking method according to the size of the value in the soft report
04 Lua operator
Best practice of cloud migration in education industry: Haiyun Jiexun uses hypermotion cloud migration products to implement progressive migration for a university in Beijing, with a success rate of 1
Using JSON server to create server requests locally
Qipengyuan horizon credible meta universe social system meets diversified consumption and social needs
Nanny Anaconda installation tutorial
The most detailed knapsack problem!!!
Install redis and deploy redis high availability cluster
Gartner announces emerging technology research: insight into the meta universe
捡起MATLAB的第(8)天
The first line and the last two lines are frozen when paging
Win11 / 10 home edition disables the edge's private browsing function
05 Lua 控制结构
ESP32编译环境的搭建
Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
Interview question 17.10 Main elements
Solution to the fourth "intelligence Cup" National College Students' IT skills competition (group B of the final)
Method 2 of drawing ROC curve in R language: proc package
Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago