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

Why use JSON format to log

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "Why to use JSON format to record logs". In daily operation, I believe many people have doubts about why they should use JSON format to record logs. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "Why use JSON format to record logs". Next, please follow the editor to study!

Logging and monitoring are like TonyStark and his IronMan suit, they need to be used together to make the most of their power, because they can complement each other.

Logging has always been an important means of application and infrastructure performance and fault diagnosis, but now we have realized that logging can be used not only for fault diagnosis, but also for big data analysis, business visualization and performance analysis, and so on.

Therefore, it is very important to record application logs.

Why use JSON format

To understand the advantages of JSON logging, let's first take a look at a conversation between Anuj (systems engineer) and Kartik (business analyst).

But a few days later, Kartik found that the Web interface was dead, and Anuj touched his head and looked at the log and found that the developer had added an extra field to the log, which broke his custom log parser.

I believe many children's shoes may have encountered a similar situation, right?

In this case, if the developer designs the application as a log in JSON format, the parser defined by Anuj is very simple, and then the key search field based on JSON is fine, regardless of whether the new field has been modified in the log.

The biggest advantage of using JSON format log is that it is structured, so it is very convenient for us to analyze the application log, not only can we easily read the log, but also can query the log through each field, and almost all programming languages can parse it easily.

JSON log magic

Recently, we created a Golang sample application to get some information about the code build, test, and deployment phases, and we used logs in JSON format for logging.

The log samples collected are as follows:

When using ELK for log collection, we only need to add the log resolution shown below to the Logstash:

Filter {json {source = > "message"}}

We don't need any additional parsing steps, even if new fields are added to the log. The collected log is shown in the following figure:

We can see that in Kibana, the key of JSON logs has been automatically parsed into ES attributes, such as employeename, employeecity and other fields. We do not need to add some very complex parsing in Logstash or other tools at all. Now it is very easy for us to use this data to create some Dashboard for data analysis.

Conclusion

It doesn't take long to migrate from text logs to JSON log format. Most programming languages have corresponding JSON log libraries, and I'm pretty sure JSON log format will provide more flexibility to your current log collection system.

Here are some popular libraries that support the JSON log format:

Golang-https://github.com/sirupsen/logrus

Python-https://github.com/thangbn/json-logging-python

Java-https://howtodoinjava.com/log4j2/log4j-2-json-configuration-example/

PHP-https://github.com/nekonomokochan/php-json-logger

At this point, the study on "Why to use JSON format to record logs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 201

*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

Internet Technology

Wechat

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

12
Report