1、配置nginx,当然我用的是windows 模拟的,nginx版本需要大于1.9,监听14330端口
worker_processes 1;
events {
worker_connections 1024;
}
stream {
upstream backend {
server 192.168.95.150:4560;
server 192.168.95.150:4560;
}
server {
listen *:14330;
proxy_pass backend;
}
}
2、启动nginx nginx.exe 关闭 nginx.exe -s stop
3、测试类
import org.apache.log4j.Logger;
public class ElkLog4jTest {
private static final Logger logger = Logger.getLogger(ElkLog4jTest.class);
public static void main(String[] args) throws Exception {
for (int i = 0; i < 100; i++) {
logger.debug("测试tcp代理 is " + i + " debug message!");
logger.info("测试tcp代理 is " + i + " info message!");
logger.warn("测试tcp代理 is " + i + " warn message!");
logger.error("测试tcp代理 is " + i + " error message!");
}
try {
System.out.println(5 / 0);
} catch (Exception e) {
logger.error(e);
}
}
}
pom配置如下
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
4、日志配置
log4j.rootLogger = debug,stdout,D,E,logstash
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出DEBUG 级别以上的日志到=/Users/bee/Documents/elk/log4j/debug.log###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E:\\logs\\debug.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=/Users/bee/Documents/elk/log4j/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E:\\logs\\error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
#输出日志到logstash
log4j.appender.logstash=org.apache.log4j.net.SocketAppender
log4j.appender.logstash.RemoteHost=127.0.0.1
log4j.appender.logstash.port=14330
log4j.appender.logstash.ReconnectionDelay=60000
log4j.appender.logstash.LocationInfo=true
6、测试效果