准备:
安装docker
安装docker compose
安装graylog
1、创建docker-compose.yml文件,位置不讲究
内容:
version: '3'
services:
mongo:
image: mongo:4.2
container_name: graylog_mongo
restart: unless-stopped
volumes:
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime #时区问题
- ./graylog/mongo_data:/data/db
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2
container_name: graylog_elasticsearch
restart: unless-stopped
volumes:
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime #时区问题
- ./graylog/es_data:/usr/share/elasticsearch/data
environment:
- http.host=0.0.0.0
- discovery.type=single-node
- "ES_JAVA_OPTS=-Xms256m -Xmx256m"
ulimits:
memlock:
soft: -1
hard: -1
deploy:
resources:
limits:
memory: 512m
graylog:
image: graylog/graylog:4.2
container_name: graylog_graylog
restart: unless-stopped
volumes:
- /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime #时区问题
- ./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:9600/ # TODO 这里填写自己的ip地址
- elasticsearch_hosts=http://elasticsearch:9200
- mongodb_uri=mongodb://mongodb/graylog
- GRAYLOG_ROOT_TIMEZONE=Asia/Shanghai #解决慢8小时问题
ports:
# Graylog web interface and REST API
- 9600:9000
# Syslog TCP
- 1514:1514
# Syslog UDP
- 1514:1514/udp
# GELF TCP
- 12201:12201
# GELF UDP
- 12201:12201/udp
- 5044:5044
- 5044:5044/udp
depends_on:
- mongo
- elasticsearch
links:
- mongo
- elasticsearch
2 开始安装
在刚刚建立的yml
目录下执行
docker-compose up -d
查看容器镜像:
或使用命令来查看:
docker ps | grep graylog
3 安装的系统本地地址:
http://127.0.0.1:9000/
密码admin/admin
4 测试
创建input - http请求
image.png
打开一个终端,输入下面的命令
curl -XPOST http://127.0.0.1:12201/gelf -p0 -d '{"message":"你好啊!!!", "host":"127.0.0.1", "facility":"test", "topic": "meme"}'
可以在Search
下看到对应的日志信息
5 springBoot集成Graylog
1、使用logback进行日志的封装
<!--logback-->
<dependency>
<groupId>com.github.pukkaone</groupId>
<artifactId>logback-gelf</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.1.6</version>
</dependency>
2、springBoot配置
1、application.properties
spring.application.name=tools
server.port=8085
spring.profiles.active=prod
#log level
logging.level.root=info
logging.level.org.springframework=info
logging.level.org.mybatis=error
2、application-prod.properties
server.port=8080
#logging properties
logging.config=classpath:logback-prod.xml
3、logback-prod.xml 配置如下
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="logging" class="com.github.pukkaone.gelf.logback.GelfAppender">
<graylogHost>127.0.0.1</graylogHost>
<originHost>tools</originHost> <!--source,来源-->
<levelIncluded>true</levelIncluded>
<locationIncluded>false</locationIncluded>
<loggerIncluded>true</loggerIncluded>
<markerIncluded>false</markerIncluded>
<mdcIncluded>false</mdcIncluded>
<threadIncluded>false</threadIncluded>
<facility>gelf-java</facility> <!--facility,随便写呗-->
<additionalField>application=tools</additionalField> <!--application,应用-->
<additionalField>environment=prod</additionalField> <!--environment,环境-->
<additionalField>managername=HealerJean</additionalField> <!--我自己加的managername,环境-->
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="logging" />
</root>
</configuration>
4、测试controller,下面有个异常哦
@Slf4j
@RestController
public class TestController {
@GetMapping("log")
public String log(){
log.error("error");
log.warn("warn");
log.info("info");
log.debug("debug");
int i = 1/0;
return "success";
}
}
5、开始启动springBoot的朋友们。观察浏览器graylog
请求log接口:
graylog简单使用就到这里了,个人体验下来graylog可以编辑的收集日志信息,筛选日志速度也比较快,从安装上体验下了看单机的安装与,graylog确实比elk要方便省心不少。
网友评论