背景
- 为了方便统一的收集应用产生的日志,便于分析、排查和优化应用问题
说明
- java日志
索引命名规则: 环境-java-项目名称(如:test-java-wofang-cloud-oauth)
字段 | 说明 | 类型 | 备注 |
---|---|---|---|
appname | 系统简称 | 字符型 | 必填 |
host | 服务器名称 | 字符型 | 必填 |
evel | 日志级别 | 字符型 | 必填 |
thread_name | 线程名称 | 字符型 | 必填 |
logger_name | 事件耗时 | 数字型 | 必填 |
message | 扩展属性 | 字符型 | 必填 |
stack_trace | 推栈信息 | 字符型 | 可选(当level为error才有) |
- 输出json示例如下:
{
"message":"Found key 'spring.application.name' in PropertySource 'configurationProperties'",
"logger_name":"org.springframework.core.env.PropertySourcesPropertyResolver",
"thread_name":"restartedMain",
"level":"DEBUG",
"appname":"oauth"
}
- nginx日志
索引命名规则: 环境-nginx-项目名称(如:test-nginx-xxxx)
字段 | 说明 | 类型 | 备注 |
---|---|---|---|
up_resp_time | 服务响应耗时 | 数字型 | 必填 |
request_time | 服务请求耗时 | 数字型 | 必填 |
request | 请求地址 | 字符型 | 必填 |
bytes | 字节数 | 数字型 | 必填 |
status | 服务返回状态 | 数字型 | 必填 |
remote_addr | 服务地址 | 字符型 | 必填 |
path | 日志路径 | 字符型 | 必填 |
- 输出json示例如下:
{
"up_resp_time":0.256,
"request_time":0.257,
"request":"GET /news/fangchan/p/1602/ HTTP/1.1",
"bytes":9700,
"status":200,
"remote_addr":"xxx.xxx.xxx.xxx",
"path":"/data/wwwlogs/xxxx.access.log"
}
- Java使用示例(参考xxx-cloud-xxx项目):
- logback-spring.xml配置
<appender name="LOGSTASH" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logstash/oauth.json</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logstash/oauth.json.%d{yyyy-MM-dd}</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"appname":"oauth"}</customFields>
</encoder>
</appender>
2.pom依赖
<!--需要引用最新版的-->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.3</version>
</dependency>
3.最终输出文件的文本格式( !!#ff0000 注意每个json是一行!! )
- nginx使用示例(参考房信项目):
1.nginx.conf配置文件
Logstash创建索引命名规范(运维关注就行,开发人员忽略)
例子:(type和index按照命名规范来 )
input {
file {
path => "/data/wwwlogs/wf_erp__access_nginx.log"
type => "xxx-slave-nginx-logs"
start_position => beginning
codec => "json"
}
}
output {
if[type] == "xxx-slave-nginx-logs"{
elasticsearch {
hosts => ["http://es的ip:9200"]
action => "index"
index => "xxx-slave-nginx-logs"
}
}
}
示例: 根据以上命名规则去处理