Spring Boot集成syslog快速入门

syslog简介

Syslog-ng是有Balabit IT Security Ltd.维护的额一套开源的Unix和类Unix系统的日志服务套件,它是一个灵活的、可伸缩的系统日志记录程序。对于服务器日志几种搜集,使用它是一个不错的解决方案。syslog-ng(syslog-Next Generation)是syslog的升级版

syslog-ng主要特性:

  • 支持SSL/TSL协议

  • 支持将日志写入数据库种,支持的数据库有Mysql、Microsoft SQL、Oracle、PostgreSql、SQLLite

  • 支持标准的syslog协议

  • 支持filter、parse和rewrite

  • 支持更多的平台

  • 更高的负载能力

环境准备

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
version: '3.6'

services:
syslog:
image: lscr.io/linuxserver/syslog-ng:latest
container_name: syslog-ng

ports:
- 514:5514/udp
- 601:6601/tcp
- 6514:6514/tcp
volumes:
- ./data/config:/config
- ./data/log:/var/log #optional
restart: unless-stopped

执行RUN命令

1
docker-compose -f docker-compose.yml up -d

down

1
docker-compose -f docker-compose.yml down

代码工程

将springboot应用日志发送到syslog-ng

  1. 引入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
    <exclusion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
    </exclusions>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
  2. 编写测试代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    import java.util.HashMap;
    import java.util.Map;

    @RestController
    public class HelloWorldController {
    private final Logger logger = LoggerFactory.getLogger(HelloWorldController.class);

    @RequestMapping("/hello")
    public Map<String, Object> showHelloWorld(@RequestParam("n") String name){
    logger.info("Say hello to {}", name);
    Map<String, Object> map = new HashMap<>();
    map.put("msg", "HelloWorld");
    return map;
    }
    }
  3. log配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration>
    <Appenders>
    <Console name="ConsoleAppender" target="SYSTEM_OUT">
    <PatternLayout
    pattern="%style{%date{DEFAULT}}{yellow} %highlight{%-5level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green} %message%n"/>
    </Console>
    <Syslog name="Syslog" format="RFC5424" host="localhost" port="514"
    protocol="UDP" appName="liuhaihua.cn" facility="LOCAL0" />
    </Appenders>

    <Loggers>
    <Root level="info">
    <AppenderRef ref="ConsoleAppender"/>
    <AppenderRef ref="Syslog"/>

    </Root>
    </Loggers>
    </Configuration>