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 the log4j2 Log component in springboot2

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

Share

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

In this issue, the editor will bring you about how to use the log4j2 log component in springboot2. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Springboot2+log4j2+kafka

It is very convenient for these three to be integrated together, and the officials directly support them. The main thing is that springboot excludes spring-boot-starter-logging, and then adds appenders of kafka to the configuration file of log4j2. A simple example is as follows

${bootstrap.servers}

This allows the log to be output to the topic of kafka. (kafka depends on zookeeper and needs to be enabled first.) the pattern of the log here is as follows:

[${application.name}] [% d {DEFAULT}] [% X {X-B3-TraceId},% X {X-B3-SpanId},% X {X-B3-ParentSpanId}] [% t] [% c {20}] [% p] -% m% n

The traceId and spanId can be ignored, which is used for link tracking with integrated micro-services.

Elasticsearch+Logstash+Kibana

These three can go directly to the official website to download the latest, Elasticsearch and Kibana integration is the easiest. No need to change any configuration, just get up. Start Elasticsearch first, and then start Kibana,Kibana. By default, there is an ES data source, so there is no additional configuration. After ES starts, you can access the default port 9200, and you can see that the information input with the version number is started successfully. Kibana defaults to port 5601. If you start successfully, visiting this port will enter the home page.

Logstash needs to be integrated with kafka and ES, so you need to add configuration and start again. Add a new logstash.conf file under the config directory, and then configure the following:

Input {kafka {topics_pattern = > "kafka-log-.*" bootstrap_servers = > "127.0.0.1 topics_pattern 9092" auto_offset_reset = > "earliest" consumer_threads = > 5 decorate_events = > "true"}} filter {grok {match = > {message = > "\ [(?. *?)\]\ [(. *) ?)\]\ [(?. *)\]-(. *) "} mutate {add_field = > {" logtime ">"% {date} "} date {timezone = >" Asia/Shanghai "match = > [" logtime " "yyyy-MM-dd HH:mm:ss,SSS"] target = > "@ timestamp" remove_field = > ["logtime"]}} output {elasticsearch {hosts = > ["http://localhost:9200"] index = >"% {[@ metadata] [kafka] [topic]} -% {+ YYYY-MM-dd} "} stdout {codec = > rubydebug}}

Topics_pattern is used here because the kafka log topic tested by my micro-service project has the same prefix. The attribute decorate_events needs to be configured with true to facilitate the specific topic in the index in the output below, which is this% {[@ metadata] [kafka] [topic]}. The message corresponds to the pattern configured in the log4j2. Date regards the time in the log as the time index field in ES.

After this configuration, you can start logstash, then go to the kibana home page to add Index patterns, and finally see the log here in Discover. The effect picture is as follows:

Click to view details, and you can see that the log content is the split content configured in logstash above, as follows:

This is how to use the log4j2 log component in springboot2 shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report