当前位置:网站首页>I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
2022-07-28 10:45:00 【ikyrxbxfas】
Preface
There's a problem online , What was your first reaction ?
If I were you , The first thought should be to check the log :
- if…else Which branch did you enter ?
- Are the key parameters missing ?
- Is there a problem with participation , I didn't check it well and put it in ?
A good log can help us quickly locate the problem , The thing that pits you is often the most invisible , A good log is to make these things invisible !
The level of logging
Java Application , Logs are generally divided into the following 5 A level :
- ERROR error message
- WARN Warning message
- INFO General information
- DEBUG Debugging information
- TRACE Tracking information
1)ERROR
ERROR Level logs are generally catch Inside the block , Used to record errors that affect the normal operation of the current thread , appear Exception Where you can consider printing ERROR journal , But it does not include business exceptions .
It should be noted that , If you throw an exception , Don't record ERROR Log , It should be dealt with in the final place , This is wrong :
try {
int i = 1 / 0;
} catch (Exception e) {
log.error(" Something went wrong , I don't know what's wrong , Ah ha ha ha !", e);
throw new CloudBaseException();
}
Copy code 2)WARN
Not supposed to show up , However, if it will not affect the execution of the current thread, you can consider printing WARN Level of logging , There are many such situations , such as :
- All kinds of pools ( Thread pool 、 Connection pool 、 Buffer pool ) The use of exceeds the threshold , Reach the alarm line
- Record business exceptions
- There was a mistake , But the fault-tolerant mechanism is designed , So the program can run normally , But it needs to be recorded
3)INFO
The most used log level , It's widely used , It is used to record the operation information of the system , such as :
- The logical steps in important modules are presented
- Client request parameter record
- Parameters and return structure when calling the third party
4)DEBUG
Debug Logs are used to record all the information you want to know , It is often the details of the operation of a function module , The data has changed in the middle , And performance information .
Debug Information is generally off in the production environment , Switch management is required ( such as SpringBoot Admin It can be done ), Opening it all the time will produce a lot of Debug, and Debug Logs are useless most of the time when the program is running normally .
if (log.isDebugEnabled()) {
log.debug(" Start execution , Starting time :[{}], Parameters :[{}]", startTime, params);
log.debug(" By calculation , Get parameters 1:[{}], Parameters 2:[{}]", param1, param2);
log.debug(" Finally, the result is processed :[{}]", result);
}
Copy code 5)TRACE
Special detailed system operation completion information , Generally, it is not used in business code , Unless it has special significance , Otherwise, it is generally used DEBUG Instead of , in fact , I've been coding until now , I have never used this level of log .
Use the correct format
If you print the log like this :
log.info(" According to the condition id:{}" + id + " Query user information ");
Copy code Don't do this , A large number of string objects will be generated , Taking up space will also affect performance .
The correct way is to use parameterized information :
log.info(" According to the condition id:[{}], Query user information ", id);
Copy code In addition to avoiding creating a large number of strings , It can also clearly isolate parameters , When you need to copy the parameters , Just double click the mouse , Instead of slowly aiming at it with the mouse and then drawing it .
The log printed like this , High readability , It is also very helpful for troubleshooting problems !
Tips
1) Multithreading
How to log when multiple threads execute together ?
Some systems , Involves concurrent execution , Scheduling, etc , There will be multiple execution logs mixed together , It's not easy to find out if something goes wrong , We can print the thread name , Or add an identifier to indicate which execution this log belongs to :
if (log.isDebugEnabled()) {
log.debug(" perform ID=[{}], Processed ID=[{}] The news of , Processing results :[{}]", execId, id, result);
}
Copy code 2) Use SpringBoot Admin Flexible switch log level

At the end
At the beginning of writing code , There is no consciousness of standardizing logs , No matter where , All of them INFO, I haven't thought about what I printed , Does it make sense , In fact, I stepped on many pits , Many extra classes , Look back , I want to say something to me during my study :” Things that can make you work overtime , All hidden in various details ! Before writing the code , First, learn how to log !“
边栏推荐
- GKRandomSource
- 287. Find the Duplicate Number
- c语言进阶篇:指针(一)
- gcc: error trying to exec 'as': execvp: No such file or directory
- 产品端数据分析思维
- django-celery-redis异步发邮件
- AP AUTOSAR platform design 1-2 introduction, technical scope and method
- Machine learning -- handwritten English alphabet 3 -- engineering features
- andorid 开发
- 2020第二届传智杯初赛
猜你喜欢

Semeval 2022 | introducing knowledge into ner system, aridamo academy won the best paper award

SQL Server 2016学习记录 --- 单表查询

SQL Server 2016 learning records - single table query

生成对抗网络在DeepFake中的未来

Particle swarm optimization to solve the technical problems of TSP

CentOS7下安装mysql5.7

GKCheckerboardNoiseSource

SQL Server 2016 learning records - set query

6、MapReduce自定义分区实现

SQL Server 2016 learning records - connection query
随机推荐
GKSpheresNoiseSource
GKCheckerboardNoiseSource
ICML 2022 | graph represents the structure aware transformer model of learning
CentOS7下安装mysql5.7
OCR 知识 概括
How to write Ogg with multiple filter syntax?
SQL Server 2016 learning record - Data Definition
ACM寒假集训#6
2019年9月PAT甲级题目
20200217训练赛 L1 - 7 2019来了 (20分)
10_ue4进阶_添加倒地和施法动作
Powerful and unique! Yingzhong technology 2020 10th generation core unique product launch
产品端数据分析思维
Why does the cluster need root permission
ACM winter vacation training 6
GKCircleObstacle
Idea create my first project
GKLinearCongruentialRandomSource
GKPolygonObstacle
C language input string with spaces