当前位置:网站首页>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
- Markdown to word or PDF
- ACM寒假集训#5
- Add new startup logo and startup / shutdown animation in mt6735
- GKNoiseMap
- GKConstantNoiseSource
- 20200217 training match L1 - 7 2019 is coming (20 points)
- The 11th Blue Bridge Cup Undergraduate group competition (20200321)
- Implement a queue with two stacks [C language]
- Go memory model (version on May 31st, 2014)
猜你喜欢

QT generation Exe file and run without QT environment (enigma virtual box for green executable software packaging) graphic tutorial

Redis-day01-常识补充及redis介绍

Batch Normlization

GKCylindersNoiseSource

GKCheckerboardNoiseSource

Django celery redis send email asynchronously

粒子群解决tsp的技术问题

Machine learning -- handwritten English alphabet 2 -- importing and processing data

PyQt5快速开发与实战 4.13 菜单栏、工具栏与状态栏 and 4.14 QPrinter

Semeval 2022 | introducing knowledge into ner system, aridamo academy won the best paper award
随机推荐
GKCoherentNoiseSource
ACM寒假集训#5
Troubleshooting of tool failure caused by Chinese characters in PT kill query
两年CRUD,二本毕业,备战两个月面试阿里,侥幸拿下offer定级P6
Yarn报错:Could not find any valid local directory for nmPrivate/
产品端数据分析思维
Codeforces Round #614 (Div. 2) B. JOE is on TV!
GKNoiseSource
CentOS7下安装mysql5.7
Machine learning -- handwritten English alphabet 2 -- importing and processing data
非关系型数据库MongoDB的特点及安装
ACM寒假集训#6
Excel word 简单 技巧 整理(持续更新 大概~)
6、MapReduce自定义分区实现
生成对抗网络在DeepFake中的未来
2019年9月PAT甲级题目
AP AUTOSAR platform design 3 architecture
SemEval 2022 | 将知识引入NER系统,阿里达摩院获最佳论文奖
2020 second intelligence cup preliminaries
SDUT Round 9 2020 Spring Festival campaign