Bootstrap

SpringBoot集成log4j且日志同步写入syslog

一、生成一个SpringBoot的项目

在线直接生成:https://start.spring.io/

二、添加log4j依赖

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</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-log4j</artifactId>
			<version>1.3.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>5.3.2</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-junit-jupiter</artifactId>
			<version>2.26.0</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

三、配置log4j

resource下增加log4j.properties文件

log4j.rootLogger=debug,Console,SYSLOG,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c - %L]%m%n

log4j.appender.File = org.apache.log4j.RollingFileAppender
#本地日志存储路径
log4j.appender.File.File = D:\\info.log
log4j.appender.File.MaxFileSize = 10MB

log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c - %L]%m%n

log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
#远程syslog服务器,无法设置端口
log4j.appender.SYSLOG.syslogHost=192.168.88.134
log4j.appender.syslog.Threshold=ALL
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.SYSLOG.Header=true
log4j.appender.SYSLOG.Facility=local2

四、配置rsyslog服务端

编辑/etc/rsyslog.conf

1)打开日志接受服务(注意关闭防火墙) 

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
 
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514

2)在文件末尾,增加以下内容:

local2.info     /var/log/login_info.log  
local2.debug    /var/log/login_debug.log  

3)重启rsyslog服务 

[root@localhost ~]# systemctl restart rsyslog.service

五、验证

1)编写测试类并执行

package com.example.demo;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class DemoApplicationTests {
    Logger logger = Logger.getLogger(DemoApplicationTests.class);

    @Test
    public void contextLoads() {
        logger.fatal("致命错误1");
        logger.error("严重警告2");
        logger.warn("警告3");
        logger.info("普通信息4");
        logger.debug("调试信息5");
    }
}

 

2)观测本地日志文件

 3)观测服务器日志

login_debug.log

 login_info.log

 

;