In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the way for Slf4j+logback to output logs in JSON format? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Slf4j+logback implementation of JSON format log output depends on org.projectlomboklombok1.18.8providedorg.slf4jslf4j-api1.7.28net.logstash.logbacklogstash-logback-encoder6.1ch.qos.logbacklogback-classic1.2.3ch.qos.logbacklogback-access1.2.3
Logback.xml
{"date": "% date {\" yyyy-MM-dd HH:mm:ss.SSS\ ", UTC}", "level": "% level" "msg": "% msg"} UTF-8
Output:
{"date": "2019-09-1708 INFO", "level": "INFO", "msg": "{\" tags\ ": {\" namespace\ ":\" service.MQTT.push\ ",\" metric.correct\ ": true}"} "}
{"date": "2019-09-17 08 tags 3015 20.784", "level": "INFO", "msg": "{\" tags\ ": {\" namespace\ ":\" service.MQTT.command\ ",\" metric.correct\ ": true}"} "}
{"date": "2019-09-17 0815 INFO", "level": "INFO", "msg": "{\" tags\ ": {\" namespace\ ":\" service.MQTT.push\ ",\" metric.correct\ ": true}"} "}
Logback records JSON log
First of all, you need to configure a class's logger separately in the json log you want to record.
As shown in the picture
Then call the logger in your corresponding class
As shown in the picture
And then here you need to record the location to record the json data you need, where the javaben toString method has been rewritten, and the returned string is the json string.
/ / add operation log record ES_LOG.info (new InOutLogModel (outBoundParamsModel.getScanCode (), outboundResult.getFormCode (), outBoundParamsModel.getMerchantId () + ", outBoundParamsModel.getSource ()," out of the library one by one here! ", new SimpleDateFormat (" yyyy-MM-dd hh:mm:ss ") .format (new Date () OutBoundParamsModel.getUserInfo () .toString ()
Finally, check the log just recorded on the server as shown in the figure.
This is the answer to the question about how Slf4j+logback implements the log output in JSON format. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.