基于Flume+Log4j+Kafka的日志采集架构方案(6)

{ "timeMillis" : 1462712870591, "thread" : "main", "level" : "ERROR", "loggerName" : "com.banksteel.log.demo.log4j2.Demo", "message" : "处理业务逻辑的时候发生一个错误……", "thrown" : { "commonElementCount" : 0, "localizedMessage" : "错误消息啊", "message" : "错误消息啊", "name" : "java.lang.Exception", "extendedStackTrace" : [ { "class" : "com.banksteel.log.demo.log4j2.Demo", "method" : "main", "file" : "Demo.java", "line" : 20, "exact" : true, "location" : "classes/", "version" : "?" }, { "class" : "sun.reflect.NativeMethodAccessorImpl", "method" : "invoke0", "file" : "NativeMethodAccessorImpl.java", "line" : -2, "exact" : false, "location" : "?", "version" : "1.7.0_80" }, { "class" : "sun.reflect.NativeMethodAccessorImpl", "method" : "invoke", "file" : "NativeMethodAccessorImpl.java", "line" : 57, "exact" : false, "location" : "?", "version" : "1.7.0_80" }, { "class" : "sun.reflect.DelegatingMethodAccessorImpl", "method" : "invoke", "file" : "DelegatingMethodAccessorImpl.java", "line" : 43, "exact" : false, "location" : "?", "version" : "1.7.0_80" }, { "class" : "java.lang.reflect.Method", "method" : "invoke", "file" : "Method.java", "line" : 606, "exact" : false, "location" : "?", "version" : "1.7.0_80" }, { "class" : "com.intellij.rt.execution.application.AppMain", "method" : "main", "file" : "AppMain.java", "line" : 144, "exact" : true, "location" : "idea_rt.jar", "version" : "?" } ] }, "endOfBatch" : false, "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger", "source" : { "class" : "com.banksteel.log.demo.log4j2.Demo", "method" : "main", "file" : "Demo.java", "line" : 22 } }

为了减少日志对空间的占用,我们通常还会设置JSONLayout的compact属性为true,这样在kafka中获得的日志将会排除掉空格和换行符。 

最后

由于在实际开发中,我们会引入多个第三方依赖,这些依赖往往也会依赖无数的log日志框架,为了保证测试通过,请认清本文例子中的包名以及版本号,log4j 1.x 的 Json 输出是为了完全模拟 2.x 的字段,因此部分字段用?代替,如果想要完美,请自行解决。

随便解释一下日志级别,以便建立规范:

log.error 错误信息,通常写在 catch 中,可以使用 log.error("发生了一个错误",e) 来记录详细的异常堆栈

log.fatal 严重错误,该级别的错误用来记录会导致程序异常退出的错误日志。

log.warn 警告

log.info 信息

log.trace 简单输出文字

log.debug 调试信息

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/34e1564505a6b7a1c6395fcee32dc310.html