经验与实践
前两篇文章里我们介绍了nxlog的日志收集和转发《ELK系列~Nxlog日志收集加转发(解决log4日志换行导致json转换失败问题)》,今天我们主要总结一下,在与log4和fluentd及elasticsearch配合工作时需要注意的几个点,这几个点也是我们经常遇到的坑,希望可以帮到大家!我们从日志产生端log4开始说。
- log4需要注意的,编码与时间戳格式
- nxlog需要注意output里对内容处理
- fluentd需要注意类型和format的规定
1 log4产生日志,格式必须是utf-8,ansi编码对中文解析时有问题,主要表示在fluentd到elasticsearch写数据时
??? <datePattern value="yyyyMMdd"Error.log"" />
?????<encoding value="utf-8" /> ??????<layout type="log4net.Layout.PatternLayout"> ?????</layout> ???</appender>
2 log4日志里,时间戳@timestamp,需要是UTC时间,格式为yyyy-MM-ddTHH:mm:ss.fff+0800
???????private static string FormatStr(string level, string message, Exception ex) ???????{ ???????????var json = JsonConvert.SerializeObject(new ???????????{ ???????????????target_index = projectName, ???????????????timestamp = DateTime.Now.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fff+0800"), ???????????????Level = level.ToString(), ???????????????Message = message, ???????????????StackTrace = ex?.StackTrace ???????????}); ???????????json = json.Replace("target_index", "@target_index").Replace("timestamp", "@timestamp"); ???????????return json; ???????}
3 nxlog日志收集时,由于log4产生的数据都是\r符号,所以在nxlog.conf里需要对output进行过滤,当然\n,使它变成两条消息,这样主体消息里就没有\r了,json解析时就不会有问题
<Output out> ???Module ?????om_tcp ???Host ???????192.168.200.214 ???Port ???????24224 ?????Exec $raw_event =$raw_event + "\n";</Output>
4 fluentd的配置里,类型需要是tcp,格式format需要是none
<source> ???@type tcp ???tag windows.log ???format none ???port 24224 ???bind 0.0.0.0 ?</source> ?<filter docker.**> ???type parser ???format json ???time_format %Y-%m-%dT%H:%M:%S.%L%Z ???key_name log ???reserve_data true ?</filter> ?<match **> ???@type stdout ?</match>
5 最后就是成功写入elasticsearch,通过kibana就可以查看日志了
请把学习作为一种习惯!
请把积累和总结变成一种学习的方式!
感谢各位的阅读!
ELK系列~log4-nxlog-Fluentd-elasticsearch写json数据需要注意的几点
原文地址:http://www.cnblogs.com/lori/p/7727375.html