Skip to content
标签
欢迎扫码关注公众号

ELK 部分日志无法保存的问题

今天突然发现 ELK 中的部分日志无法保存了,只有异常(error)日志可以保存(我们是根据日志 Level 分别创建的索引,每月创建一个索引)。

查了好久终于在比对索引字段类型的时候发现有一个 serverTime 的字段在两个索引中的类型不同,一个是 long 型,一个是 date 型。

json
"serverTime": {
    "type": "long"
}
json
"serverTime": {
    "type": "date"
}

这才想起来上个月接入了 Java 端的日志,之前只有 .NET Core 的日志。

java
/**
    * 创建时间
    */
private Date serverTime = new Date();

默认情况下其序列化(Jackson)为时间戳,ElasticSearch 将其当做 long 型。
这也就导致了当该月 Java 端首次记录这种类型的日志时,就会创建 long 型字段的索引。

json
{
    "serverTime": 1567407747966
}

知道原因就好解决了,统一序列化成 ElasticSearch 的 date 型就可以了。

参考 Jackson Date 上的方法将日期型字段序列化为 ISO-8601 格式。

java
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// StdDateFormat is ISO8601 since jackson 2.9
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
return mapper.writeValueAsString(logEntity);

修改后的序列化结果:

json
{
    "serverTime": "2019-09-02T07:00:22.805+00:00"
}

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.