1.什么syslog?
Syslog-ng是由Balabit IT Security Ltd.维护的一套开源的Unix和类Unix系统的日志服务套件。它是一个灵活的、可伸缩的系统日志记录程序。对于服务器日志集中收集,使用它是一个不错的解决方案。syslog-ng (syslog-Next generation) 是syslog的升级版
Syslog-ng主要特性有:
- 支持SSL/TSL协议
- 支持将日志写入数据库中,支持的数据库有MySQL, Microsoft SQL (MSSQL),Oracle, PostgreSQL, and SQLite.
- 支持标准的syslog协议
- 支持filter、parse以及rewrite
- 支持更多的平台
-
更高的负载能力
2.环境准备
docker-compose.yml
version: '3.6'services:syslog:image: lscr.io/linuxserver/syslog-ng:latestcontainer_name: syslog-ngports:- 514:5514/udp- 601:6601/tcp- 6514:6514/tcpvolumes:- ./data/config:/config- ./data/log:/var/log #optionalrestart: unless-stopped
run
docker-compose -f docker-compose.yml up -d
down
docker-compose -f docker-compose.yml down
3.代码工程
实验目的
实现将springboot应用日志发送到syslog-ng
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>syslog</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><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></dependencies>
</project>
controller
package com.et.syslog.controller;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;}
}
log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration><Appenders><Console name="ConsoleAppender" target="SYSTEM_OUT"><PatternLayoutpattern="%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>
以上只是一些关键代码,所有代码请参见下面代码仓库
代码仓库
- https://github.com/Harries/springboot-demo
4.测试
- 启动spring boot应用
- 访问http://127.0.0.1:8088/hello?n=niki
- 查看syslog-ng日志 (/var/log/messages,/var/log/messages-kv.log)
5.引用
- Syslog Support :: Spring Integration
- Spring Boot集成syslog快速入门Demo | Harries Blog™