一、graylog介绍
GrayLog是一个轻量型的分布式日志管理平台,一个开源的日志聚合、分析、审计、展示和预警工具。在功能上来说,和ELK类似,但又比ELK要简单轻量许多。依靠着更加简洁,高效,部署使用简单的优势很快受到许多公司的青睐。GrayLog包含Elasticsearch、MongoDb 和Graylog三个模块
二、graylog环境搭建
这里采用docker-compose来搭建graylog测试环境,具体配置如下
# docker-compose配置参考:https://docs.graylog.org/en/3.3/pages/installation/docker.html
version: '3'
services:
mongo:
image: mongo:3
container_name: graylog_demo_mongo
restart: unless-stopped
# volumes:
# - ./graylog/mongo_data:/data/db
networks:
- graylog
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.10
container_name: graylog_demo_elasticsearch
restart: unless-stopped
# volumes:
# - ./graylog/es_data:/usr/share/elasticsearch/data
environment:
- http.host=0.0.0.0
- transport.host=localhost
- network.host=0.0.0.0
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
deploy:
resources:
limits:
memory: 1g
networks:
- graylog
graylog:
image: graylog/graylog:3.3
container_name: graylog_demo_graylog
restart: unless-stopped
# volumes:
# - ./graylog/graylog_data_journal:/usr/share/graylog/data/journal
environment:
- GRAYLOG_PASSWORD_SECRET=somepasswordpepper # CHANGE ME (must be at least 16 characters)!
- GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 # Password: admin
- GRAYLOG_HTTP_EXTERNAL_URI=http://127.0.0.1:9000/ # TODO 这里填写自己的ip地址
ports:
# Graylog web interface and REST API
- 9000:9000
# Syslog TCP
- 1514:1514
# Syslog UDP
- 1514:1514/udp
# GELF TCP
- 12201:12201
# GELF UDP
- 12201:12201/udp
networks:
- graylog
depends_on:
- mongo
- elasticsearch
# 网桥graylog -> 方便相互通讯
networks:
graylog:
driver: bridge
启动服务
docker-compose -f docker-compose-graylog.yml -p graylog_demo up -d
访问地址:http://ip地址:9001
默认登录账号密码:admin/admin
2.1、新建input http input
测试是否正常发送
curl -XPOST http://localhost:12201/gelf -p0 -d '{"message":"hello这是一条消息", "host":"127.0.0.1", "facility":"test", "topic": "meme"}'
2.2、创建 gref udp input
由于http发送效率比较低,所以后面springboot会采用UDP方法来采集日志
三、代码工程
pom.xml
springboot-demo
com.et
1.0-SNAPSHOT
4.0.0
grayLog
8
8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-autoconfigure
biz.paluch.logging
logstash-gelf
1.11.1
org.projectlombok
lombok
HelloWorldController.java
package com.et.graylog.controller;
import com.et.graylog.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Controller
public class HelloWorldController {
@Autowired
DemoService demoService;
@RequestMapping("/hello")
@ResponseBody
public Map showHelloWorld(){
Map map = new HashMap();
map.put("msg", "HelloWorld");
log.info("my name is HelloWorldController");
demoService.hello();
return map;
}
@GetMapping("log")
public String log(){
log.error("error");
log.warn("warn");
log.info("info");
log.debug("debug");
int i = 1/0;
return "success";
}
}
DemoApplication.java
package com.et.graylog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
application.yaml
server:
port: 8088
groylog:
ip: 127.0.0.1
port: 12201
maximumMessageSize: 81服务器托管网92
logback.xml
udp:127.0.0.1
12201
1.1
springboot-with-graylog
true
true
true
yyyy-MM-dd HH:mm:ss,SSSS
8192
mdcField1,mdcField2
mdc.*,(mdc|MDC)fields
true
%d{HH:mm:ss.SSS} %服务器托管网-5level %logger{36} - %msg%n
代码参考
-
https://github.com/Harries/springboot-demo
四、测试
启动服务
"C:Program FilesJavajdk1.8.0_251binjava.exe" -Dvisualvm.id=505582826333400 "-javaagent:D:huiyidadevelopIntelliJ IDEA Community Edition 2022.1.3libidea_rt.jar=57262:D:huiyidadevelopIntelliJ IDEA Community Edition 2022.1.3bin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_251jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_251jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_251jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_251jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_251jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_251jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_251jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_251jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_251jrelibextnashorn.jar;C:Program FilesJavajdk1.8.0_251jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_251jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_251jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_251jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_251jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_251jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_251jrelibjce.jar;C:Program FilesJavajdk1.8.0_251jrelibjfr.jar;C:Program FilesJavajdk1.8.0_251jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_251jrelibjsse.jar;C:Program FilesJavajdk1.8.0_251jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_251jrelibplugin.jar;C:Program FilesJavajdk1.8.0_251jrelibresources.jar;C:Program FilesJavajdk1.8.0_251jrelibrt.jar;D:IdeaProjectsETFrameworkgrayLogtargetclasses;D:jar_repositoryorgspringframeworkbootspring-boot-starter-web2.2.5.RELEASEspring-boot-starter-web-2.2.5.RELEASE.jar;D:jar_repositoryorgspringframeworkbootspring-boot-starter2.2.5.RELEASEspring-boot-starter-2.2.5.RELEASE.jar;D:jar_repositoryorgspringframeworkbootspring-boot-starter-logging2.2.5.RELEASEspring-boot-starter-logging-2.2.5.RELEASE.jar;D:jar_repositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;D:jar_repositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;D:jar_repositoryorgslf4jslf4j-api1.7.30slf4j-api-1.7.30.jar;D:jar_repositoryorgapachelogginglog4jlog4j-to-slf4j2.12.1log4j-to-slf4j-2.12.1.jar;D:jar_repositoryorgapachelogginglog4jlog4j-api2.12.1log4j-api-2.12.1.jar;D:jar_repositoryorgslf4jjul-to-slf4j1.7.30jul-to-slf4j-1.7.30.jar;D:jar_repositoryjakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;D:jar_repositoryorgspringframeworkspring-core5.2.4.RELEASEspring-core-5.2.4.RELEASE.jar;D:jar_repositoryorgspringframeworkspring-jcl5.2.4.RELEASEspring-jcl-5.2.4.RELEASE.jar;D:jar_repositoryorgyamlsnakeyaml1.25snakeyaml-1.25.jar;D:jar_repositoryorgspringframeworkbootspring-boot-starter-json2.2.5.RELEASEspring-boot-starter-json-2.2.5.RELEASE.jar;D:jar_repositorycomfasterxmljacksoncorejackson-databind2.10.2jackson-databind-2.10.2.jar;D:jar_repositorycomfasterxmljacksoncorejackson-annotations2.10.2jackson-annotations-2.10.2.jar;D:jar_repositorycomfasterxmljacksoncorejackson-core2.10.2jackson-core-2.10.2.jar;D:jar_repositorycomfasterxmljacksondatatypejackson-datatype-jdk82.10.2jackson-datatype-jdk8-2.10.2.jar;D:jar_repositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.10.2jackson-datatype-jsr310-2.10.2.jar;D:jar_repositorycomfasterxmljacksonmodulejackson-module-parameter-names2.10.2jackson-module-parameter-names-2.10.2.jar;D:jar_repositoryorgspringframeworkbootspring-boot-starter-tomcat2.2.5.RELEASEspring-boot-starter-tomcat-2.2.5.RELEASE.jar;D:jar_repositoryorgapachetomcatembedtomcat-embed-core9.0.31tomcat-embed-core-9.0.31.jar;D:jar_repositoryorgapachetomcatembedtomcat-embed-el9.0.31tomcat-embed-el-9.0.31.jar;D:jar_repositoryorgapachetomcatembedtomcat-embed-websocket9.0.31tomcat-embed-websocket-9.0.31.jar;D:jar_repositoryorgspringframeworkbootspring-boot-starter-validation2.2.5.RELEASEspring-boot-starter-validation-2.2.5.RELEASE.jar;D:jar_repositoryjakartavalidationjakarta.validation-api2.0.2jakarta.validation-api-2.0.2.jar;D:jar_repositoryorghibernatevalidatorhibernate-validator6.0.18.Finalhibernate-validator-6.0.18.Final.jar;D:jar_repositoryorgjbossloggingjboss-logging3.4.1.Finaljboss-logging-3.4.1.Final.jar;D:jar_repositorycomfasterxmlclassmate1.5.1classmate-1.5.1.jar;D:jar_repositoryorgspringframeworkspring-web5.2.4.RELEASEspring-web-5.2.4.RELEASE.jar;D:jar_repositoryorgspringframeworkspring-beans5.2.4.RELEASEspring-beans-5.2.4.RELEASE.jar;D:jar_repositoryorgspringframeworkspring-webmvc5.2.4.RELEASEspring-webmvc-5.2.4.RELEASE.jar;D:jar_repositoryorgspringframeworkspring-aop5.2.4.RELEASEspring-aop-5.2.4.RELEASE.jar;D:jar_repositoryorgspringframeworkspring-context5.2.4.RELEASEspring-context-5.2.4.RELEASE.jar;D:jar_repositoryorgspringframeworkspring-expression5.2.4.RELEASEspring-expression-5.2.4.RELEASE.jar;D:jar_repositoryorgspringframeworkbootspring-boot-autoconfigure2.2.5.RELEASEspring-boot-autoconfigure-2.2.5.RELEASE.jar;D:jar_repositoryorgspringframeworkbootspring-boot2.2.5.RELEASEspring-boot-2.2.5.RELEASE.jar;D:jar_repositorybizpaluchlogginglogstash-gelf1.11.1logstash-gelf-1.11.1.jar;D:jar_repositoryorgprojectlomboklombok1.18.12lombok-1.18.12.jar" com.et.graylog.DemoApplication
. ____ _ __ _ _
/ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
10:07:22.906 INFO com.et.graylog.DemoApplication - Starting DemoApplication on BLHHUAPC with PID 30200 (D:IdeaProjectsETFrameworkgrayLogtargetclasses started by Dell in D:IdeaProjectsETFramework)
10:07:22.918 INFO com.et.graylog.DemoApplication - No active profile set, falling back to default profiles: default
10:07:23.801 INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8088 (http)
10:07:23.811 INFO o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8088"]
10:07:23.813 INFO o.a.catalina.core.StandardService - Starting service [Tomcat]
10:07:23.813 INFO o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.31]
10:07:23.885 INFO o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
10:07:23.887 INFO o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 930 ms
10:07:24.025 INFO o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
10:07:24.124 INFO o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8088"]
10:07:24.143 INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8088 (http) with context path ''
10:07:24.147 INFO com.et.graylog.DemoApplication - Started DemoApplication in 1.564 seconds (JVM running for 2.09)
登录graylog,查看日志是否已经发送过去
五、参考资料
-
http://blog.healerjean.com/docker/log/2018/03/16/2_Docker%E5%AE%89%E8%A3%85graylog%E5%92%8C%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/
-
https://archivedocs.graylog.org/en/3.3/
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
把当前文件夹下的所有文件夹和文件列出来,允许点击返回上层目录,允许点击文件夹进入下级目录并显示此文件夹内容 允许点击文件进行下载 from flask import Flask, render_template, send_file, request, red…