# javaApp.log
2019-08-14 14:51:22,299 ERROR [http-nio-8080-exec-8] classOne: Index out of range
java.lang.StringIndexOutOfBoundsException: String index out of range: 18
at java.lang.String.charAt(String.java:658)
at com.example.app.loggingApp.classOne.getResult(classOne.java:15)
at com.example.app.loggingApp.AppController.tester(AppController.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
[...]
{"@timestamp":"2019-08-14T18:46:04.449+00:00","@version":"1","message":"Index out of range","logger_name":"com.example.app.loggingApp.classOne","thread_name":"http-nio-5000-exec-6","level":"ERROR","level_value":40000,"stack_trace":"java.lang.StringIndexOutOfBoundsException: String index out of range: 18ntat java.lang.String.charAt(String.java:658)ntat com.example.app.loggingApp.classOne.getResult(classOne.java:15)ntat com.example.app.loggingApp.AppController.tester(AppController.java:27)ntat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)ntat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)ntat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)ntat java.lang.reflect.Method.invoke(Method.java:498)ntat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)ntat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)ntat
[...]
}
2019-08-01 18:58:05,898 ERROR:Exception on main handler
Traceback (most recent call last):
File "python-logger.py", line 9, in make_log
return word[13]
IndexError: string index out of range
{
"timestamp": "2019-08-01 19:22:14,196",
"level": "ERROR:",
"message": "Exception on main handlernTraceback (most recent call last):n File "python-logger.py", line 9, in make_logn return word[13]nIndexError: string index out of range"
}
该日志已被格式化为 JSON,我们匹配的标签也被设置为了 Key。
在 Fluentd 官方文档中也有几个示例说明:
Rails 日志
比如输入的 Rails 日志如下所示:
Started GET "/users/123/" for 127.0.0.1 at 2013-06-14 12:00:11 +0900
Processing by UsersController#show as HTML
Parameters: {"user_id"=>"123"}
Rendered users/show.html.erb within layouts/application (0.3ms)
Completed 200 OK in 4ms (Views: 3.2ms | ActiveRecord: 0.0ms)
我们可以使用下面的解析配置进行多行匹配:
@type multiline
format_firstline /^Started/
format1 /Started (?[^ ]+) "(?[^"]+)" for (?[^ ]+) at (?
2013-3-03 14:27:33 [main] INFO Main - Start
2013-3-03 14:27:33 [main] ERROR Main - Exception
javax.management.RuntimeErrorException: null
at Main.main(Main.java:16) ~[bin/:na]
2013-3-03 14:27:33 [main] INFO Main - End
Fluent Bit 的 tail input 插件也提供了处理多行日志的配置选项,比如现在我们还是来处理之前的 Python 多行日志:
2019-08-01 18:58:05,898 ERROR:Exception on main handler
Traceback (most recent call last):
File "python-logger.py", line 9, in make_log
return word[13]
IndexError: string index out of range
如果不用多行解析器 Fluent Bit 同样会将每一行当成一条日志进行处理,我们可以配置使用 Fluent Bit 内置的 regex 解析器插件来结构化多行日志:
[PARSER]
Name log_date
Format regex
Regex /d{4}-d{1,2}-d{1,2}/
[PARSER]
Name log_attributes
Format regex
Regex /(?[^ ]* [^ ]*) (?[^s]+:)(?[sS]*)/
[INPUT]
Name tail
tag sample.tag
path /path/to/pythonApp.log
Multiline On
Parser_Firstline log_date
Parser_1 log_attributes
{
"timestamp": "2019-08-01 19:22:14,196",
"level": "ERROR:",
"message": "Exception on main handlernTraceback (most recent call last):n File "python-logger.py", line 9, in make_logn return word[13]nIndexError: string index out of range"
}
09-24 16:09:07.042: ERROR System.out(4844): java.lang.NullPointerException
at com.temp.ttscancel.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)Copied!