Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java log printing

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use java log printing". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. brief introduction

Log printing is an indispensable important step in java code development.

Logs can troubleshoot problems and collect data

Second, commonly used log framework

The more commonly used logging framework is logback. Some old projects use log4j, and they all use the slf4j-api unified interface.

(1) use log4j

Log4j uses: 1. Slf4j-api-1.5.11.jar2. Slf4j-log4j12-1.5.11.jar3. Log4j-1.2.15.jar4. Log4j.properties

In the code

Import org.slf4j.Logger;import org.slf4j.LoggerFactory;Logger logger = LoggerFactory.getLogger (xx.class)

(2) use logback

Logback uses: 1. Slf4j-api-1.5.11.jar2. Logback-core.jar3. Logback-classic.jar4. Logback.xml

Code is the same as log4j

Import org.slf4j.Logger;import org.slf4j.LoggerFactory;Logger logger = LoggerFactory.getLogger (xx.class); III. Log level TRACE

< DEBUG < INFO < WARN < ERROR 日常使用较多的是error, info , debug 四、logback简单介绍 简单介绍下常用的logback,logback优点: 比log4j更快 和log4j使用了同一个接口,slf4j-api,可以非常方便切换 定义了功能非常丰富的appender 支持日志压缩 logback要正确理解xml配置文件 configuration:总的父节点 property:自定义属性 appender:输出控制器,可以输出到控制台,自定义文件等 logger:日志级别 root:控制器级别 一个例子: XXXXXXXX DEBUG ${log.pattern} ERROR ${log.path}/error.log ${log.path}/error-%d{yyyyMMdd}.%i.log.zip ${maxHistory} 2GB 100MB ${log.pattern} INFO ${log.path}/info.log ${log.path}/info-%d{yyyyMMdd}.%i.log.zip ${maxHistory} 2GB 100MB ${log.pattern} DEBUG ${log.path}/debug.log ${log.path}/debug-%d{yyyyMMdd}.%i.log.zip ${maxHistory} 2GB 100MB ${log.pattern} 五、常见问题 1、日志打的少,不好排查问题? 我们经常会遇到一个生产问题,去看日志,啥都没有,只能重新加日志,发包,再排查问题; 所以我们要养成打日志的习惯,开发环境可以debug看,生产看不了,只能加日志,在开发的时候就把必要的日志加上; 比较重要的是接口的入参,返回,重要节点开始,结束,mq发送接收等。 2、error, info , debug分别都什么时候使用? error: 捕获异常的时候使用,这个没有异议 info: 比较重要的信息,使用频次不是非常高的场景,比如入参出参 debug: 一个是不是特别重要的信息,但是又不能少,还有数据量大的数据,比如大量mq信息,访问频繁的接口入参出参 3、开启debug好多无用的debug信息怎么办? 把项目的目录定为debug,其它定成info。这样只有本项目的debug日志会打印了 logging.level.root=info logging.level.cn.mypackage=debug 4、需要自定义日志文件吗? logback可以把日志写进自定义文件,debug,info,error分开存储,历史数据还能压缩; 按自己项目的需求来,大型项目建议分开存储。 5、springboot启动命令自带的日志和自定义日志文件的日志重复怎么办? 我们一般启动springboot项目命令为:nohup java -jar XXXXXXXX.jar >

> XXXXXXX.log 2 > & 1 &

This will generate a log file, and the logback configuration will also generate its own log file, which will be repeated, resulting in a waste of space, how to choose?

If your company unifies the packaging script, use the log file generated by the command to remove the logback configuration.

If your company has a high degree of freedom, use the log generated by logback and change the command to > / dev/null.

6. How to write the debug log correctly?

Some people may type debug logs directly logger.debug ("*"); if the log level of the project is relatively high, it is a waste of performance. You can add an if (log.isDebugEnabled ()) {log.debug ("XXXXXXXXXXXXX");}

7. Dynamic log level?

Some companies support dynamic configuration files, such as diamond,nacos, etc., you can modify the level of configuration files, dynamically effective logging.level.root=info#logging.level.root=debug opens daily in info, if there is a problem, open debug, limited resources can do so, do not recommend production to use "how to use java log printing" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report