ELK之日志来源微服务

 

ELK分析的是日志,日志本身是应用服务器产生的各种业务日志及留痕日志,方便后期进行追溯及查看分析;

 

真正开发到一定层次的人,业务就是基本中的最基本的了.....更高层次的追求就是更好的维护和兼容这些业务日志,更重要的是操作及分析日志的能力;

 

这就要求coder去实现完美的日志记录;这个日志的好坏直接决定了后期分析日志的可用性....

 

不扯了...

上服务日志,springboot使用的是1.4.3,相应的pom文件如下

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.young</groupId>
	<artifactId>logstashservice</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>logstashservice</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.2.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.2.2</version>
		</dependency>

		<!--logstash-->
		<dependency>
			<groupId>net.logstash.logback</groupId>
			<artifactId>logstash-logback-encoder</artifactId>
			<version>4.6</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

配置的logger日志的配置类为:

 

package com.young.logstashservice.config;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import net.logstash.logback.appender.LogstashTcpSocketAppender;
import net.logstash.logback.encoder.LogstashEncoder;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;

/**
 * @author Young
 * @description 日志文件配置类
 * @date 15:56 2019/11/25
 */
@Component
public class LogstashConfig {
    @Value(value ="${logstash.shipper.host}")
    private String host;
    @Value("${logstash.shipper.port}")
    private String port;
    @Value("${logstash.level}")
    private String level;

    @PostConstruct
    public void startLogstashAppener() {
        Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        LogstashTcpSocketAppender appender = new LogstashTcpSocketAppender();
        appender.setName("stash");
        appender.addDestinations(new InetSocketAddress(host,Integer.valueOf(port)));
        LogstashEncoder encoder = new LogstashEncoder();
        String serviceName = "logstashService";
        encoder.setCustomFields("{\"service\":\""+serviceName+"\"}");
        encoder.start();

        appender.setEncoder(encoder);
        appender.setContext(logger.getLoggerContext());
        appender.start();
        logger.addAppender(appender);
        logger.setLevel(Level.toLevel(level));
    }

}

配置文件就是上文提到的host,port和level,在application.properties文件中

logstash.shipper.host=192.168.241.239
logstash.shipper.port=4560
logstash.level=info

 

然后下面就是自己写接口,输出自己想要的日志:

下文就是自己的简单示例:

package com.young.logstashservice;

import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api("logstash相关api")
@RestController
@RequestMapping("/logstash")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(value = "/test/{userName}",method = RequestMethod.GET)
    public int getUserAge(@PathVariable("userName")String userName){
        int age=0;
        if (userName.equals("小红")){
            logger.debug("我是[{}],今年[{}]岁",userName,age);
        }else {
            age=20;
            logger.error("我是[{}],今年[{}]",userName,age);
        }
        return age;
    }
}

 

发布了165 篇原创文章 · 获赞 40 · 访问量 15万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章