配置、启动kibana
到kibana的安装目录:
./bin/kibana
默认配置即可。
访问localhost:5601,网页显示:
证明启动成功。
创建springboot工程
起步依赖如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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-starter-log4j</artifactId> <version>1.3.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
log4j的配置,/src/resources/log4j.properties如下:
log4j.rootLogger=INFO,console # for package com.demo.elk, log would be sent to socket appender. log4j.logger.com.forezp=DEBUG, socket # appender socket log4j.appender.socket=org.apache.log4j.net.SocketAppender log4j.appender.socket.Port=4560 log4j.appender.socket.RemoteHost=localhost log4j.appender.socket.layout=org.apache.log4j.PatternLayout log4j.appender.socket.layout.ConversionPattern=%d [%-5p] [%l] %m%n log4j.appender.socket.ReconnectionDelay=10000 # appender console 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=%d [%-5p] [%l] %m%n
打印log测试:
@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootElkApplicationTests { @Test public void contextLoads() { } private Logger logger = Logger.getLogger(getClass()); @Test public void test() throws Exception { for(int i=0;i<100;i++) { logger.info("输出info "); logger.debug("输出debug+skkkw嗡嗡嗡kw"); logger.error("输出error 嗡嗡嗡我"); } } }