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

[Web development practice from 0] SpringBoot integrates LogBack and LogStash

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Table of contents:

1. SpringBoot integrates and configures LogBack

two。 Encapsulate LogUtil and centralize log processing

3. SpringBoot integrates LogStash

4. Install and configure LogStash separately

5. Docker-compose deploy ELK with one click and be simple and self-willed

6. Common problems and solutions

The log is very important in the system, especially in the production environment. Once there is a problem, the error message in the log first triggers the early warning system, and then alerts the person in charge of the system by e-mail, text message or even phone call. During the troubleshooting phase, developers and testers usually also check the system log to analyze the cause of the failure.

ELK is a commonly used log processing system. It is very convenient for SpringBoot to integrate LogBack and LogStash. Configure LogBack to send logs to LogStash.

Code download: https://github.com/jextop/StarterApi/

First, SpringBoot integrates and configures LogBack

SpringBoot uses SLF4J and LogBack by default, and the configuration can be customized in application.yml.

Code file

Functional essentials

SpringBoot integrates SLF4J and LogBack

Pom.xml

Introduce log dependency: spring-boot-starter-logging, Note: spring-boot-starter and spring-boot-starter-web have introduced spring-boot-starter-logging

Application.yml

Custom configuration log property

Encapsulated LogUtil

LogUtil.java

Centralized processing of logs

LogUtilTest.java

Unit test log output

Function call

Xxx.java

Call LogUtil functions, such as LogUtil.info ()

1. Spring-boot-starter and spring-boot-starter-web introduce SLF4J and LogBack dependencies by default. If you need to introduce it separately, you can add spring-boot-starter-logging to pom.xml

Org.springframework.boot

Spring-boot-starter-logging

two。 Customize the configuration log property in application.yml:

Logging.level: specify the output log level of package. Optional: debug, info, warn, error

Logging.file.path: specify the log file path

Logging.file.max-size: specify a single file size. If the size is exceeded, multiple files will be scrolled.

Logging.file.max-history: specifies the longest history retained by the archive log file

Logging.pattern.console: log format for output to console workbench

Logging.pattern.file: format of output to log file

Logging:

Level:

Com.starter: info

File:

Path: logs

Max-size: 10MB

Max-history: 7

Pattern:

Console: "% d%-5level [% thread]% logger:% msg%n"

File: "% d%-5level [% thread]% logger:% msg%n"

Second, encapsulate LogUtil.java centralized processing logs

1. LogUtil centrally processes logs

two。 Unit test LogUtil

@ SpringBootTest (classes = StarterApplication.class)

Public class LogUtilTest {

@ Test

Public void testLog () {

LogUtil.debug ("debug", "message.")

LogUtil.info ("info", "message.")

LogUtil.warn ("warn", "message.")

LogUtil.error ("error", "message.")

}

}

Output log:

2020-01-31 14 com.common.util.LogUtil 21 INFO [main] com.common.util.LogUtil: info, message.

2020-01-31 14 com.common.util.LogUtil 21 WARN [main] com.common.util.LogUtil: warn, message.

2020-01-31 14 com.common.util.LogUtil 21 ERROR [main] com.common.util.LogUtil: error, message.

3. Call LogUtil in the code

-call LogUtil.info ("xxx")

LogUtil.info ("Check cache to set str", key, str)

-Console workbench and log file output:

2020-01-31 14 com.common.util.LogUtil 19V 02438 INFO [http-nio-8011-exec-2] com.common.util.LogUtil: Check cache to set str, cache_test_192.168.3.9_200131014871354985900257_ cache, cache_test_192.168.3.9_200131014871354985900257_ cache

Note: the logger in the output of LogUtil is all LogUtil. If you need to use different logger in different classes, please create your own logger in the class:

Private static final Logger log = LoggerFactory.getLogger (LogUtil.class)

Third, SpringBoot integrates LogStash

Code file

Functional essentials

SpringBoot integrated LogStash

Pom.xml

Introduce LogStash dependency: logstash-logback-encoder

Logback.xml

Configure LogStash log format

Application.yml

Reference logback.xml configuration

Start LogStash

Installation package bin directory

Configure logstash.conf to start logstash-f logstash.conf

1. Add LogStash dependencies in pom.xml

Net.logstash.logback

Logstash-logback-encoder

5.1

two。 Add logback.xml under the resouces directory

127.0.0.1:9600

3. Reference the logback.xml configuration in application.yml:

Logging:

Config: classpath:logback.xml

Fourth, install and configure LogStash separately

1. Download LogStash: https://www.elastic.co/downloads/logstash

When the official website is slow, you can use the network disk: https://pan.baidu.com/s/1b-czkB8z5aL6rdxZtBmNEw extraction code: arfb

two。 Configuration: add logstash.conf under the bin directory:

Input.tcp.host can set native ip

Input.tcp.port setting Port

Output.elasticsearch.hosts sets the output address

Input {

Tcp {

Mode = > "server"

Host = > "0.0.0.0"

Port = > 9600

Codec = > json_lines

}

}

Output {

Stdout {

Codec = > rubydebug

}

Elasticsearch {

Hosts = > ["elasticsearch:9200"]

}

}

3. Start: logstash-f logstash.conf. When you see the port message, it runs successfully.

4. Run the Spring project and LogStash will receive log information

Fifth, docker-compose deploys ELK with one click

ELK is a commonly used log management system, which is deployed with one click of Docker-compose, eliminating the tedious steps of installation and configuration.

1. Docker installation:

Https://docs.docker.com/install/linux/docker-ce/ubuntu/

Https://docs.docker.com/docker-for-windows/install/

two。 Script download:

Https://github.com/rickding/HelloDocker/tree/master/elk

├── docker-compose.yml

├── pull.sh

├── up.sh

├── logs.sh

├── down.sh

3. Docker-compose.yml script to configure ELK service:

Logstash and kibana need to connect to the elasticsearch instance, so the depends_on property is set.

Version:'3'

Services:

Elasticsearch:

Hostname: elasticsearch

Image: elasticsearch:latest

Ports:

-9200 purl 9200

-9300 purl 9300

Log:

Image: registry.cn-shanghai.aliyuncs.com/hellodock/logstash:latest

Ports:

-9600 purl 9600

-9601 Freund 9601

Depends_on:

-elasticsearch

Kibana:

Image: registry.cn-shanghai.aliyuncs.com/hellodock/kibana:latest

Ports:

-5601 Fraser 5601

Depends_on:

-elasticsearch

4. Pull the image by pull.sh

You can run docker pull elasticsearch:latest directly to pull the image and view it with docker images:

5. Up.sh startup container

The docker-compose up-d command is encapsulated in the script. After startup, run docker ps to view the container instance:

6. Logs.sh view ELK operation log

Custom commands highlight important information:

Docker-compose logs-ft | grep-- color-I-e error-e warn-e version-e exception

7. Down.sh out of service

Docker-compose down-- remove-orphans stops and deletes the container:

8. View the ELK service:

-elasticsearch:

-logstsh:

-kibana:

Using docker-compose to deploy ELK with one click is as simple as wayward.

Fifth, common problems and solutions

Multiple log frameworks conflict. The message appears at the start of the project: SLF4J: Class path contains multiple SLF4J bindings

Reason: other log frameworks such as log4j have been introduced into the jar package on which the project depends

Solution: configure exclude when introducing dependencies in pom.xml and exclude unwanted log4j

Org.slf4j

Slf4j-log4j12

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