阅读错误日志是rb模块的责任,它的接口极其简单。
PS C:\CodeProjects\ErlangProjects\erlang_learning\src\otp> erl -boot start_sasl -config ./.cfg/erlog
Eshell V9.3 (abort with ^G)
1> rb:help().
Report Browser Tool - usage
===========================
rb:start() - start the rb_server with default options
rb:start(Options) - where Options is a list of:
{start_log, FileName}
- default: standard_io
{max, MaxNoOfReports}
- MaxNoOfReports should be an integer or 'all'
- default: all
{report_dir, DirString}
- DirString should be a string without trailing
- directory delimiter.
- default: {sasl, error_logger_mf_dir}
{type, ReportType}
- ReportType should be a supported type, 'all'
- or a list of supported types
- default: all
{abort_on_error, Bool}
- Bool: true | false
- default: false
rb:h() - print this help
rb:help() - print this help
rb:list() - list all reports
rb:list(Type) - list all reports of type Type
rb:log_list() - log list of all reports
rb:log_list(Type) - log list of all reports of type Type
currently supported types are:
- crash_report
- supervisor_report
- progress
- error
rb:grep(RegExp) - print reports containing RegExp.
RegExp must be a valid argument for
the function re:run/2 or re:run/3.
rb:filter(Filters) - print reports matching Filters.
reports must be proplists.
Filters is a list of tuples of the following form:
- {Key, Value}
includes report containing {Key, Value}
- {Key, Value, no}
excludes report containing {Key, Value}
- {Key, RegExp, re}
RegExp must be a valid argument for
the function re:run/2 or re:run/3.
- {Key, RegExp, re, no}
excludes report containing {Key, RegExp}
rb:filter(Filters, Dates) -
same as rb:filter/1 but accepts date ranges to filter reports.
Dates must be of the following form:
- {StartDate, EndDate}
StartDate = EndDate = {{Y,M,D},{H,M,S}}
prints the reports with date between StartDate and EndDate
- {StartDate, from}
prints the reports with date greater than StartDate
- {EndDate, to}
prints the reports with date lesser than StartDate
rb:rescan() - rescans the report directory with same
options.
rb:rescan(Options) - rescans the report directory with new
options. Options is same as in start/1.
rb:show(Number) - print report no Number
rb:show(Type) - print all reports of type Type
rb:show() - print all reports
rb:start_log(File) - redirect all reports to file or io_device
rb:stop_log() - close the log file and redirect to
standard_io
rb:stop - stop the rb_server
ok
2>
接下来我们调用第一个空参的start方法,正常的是这样
PS C:\CodeProjects\ErlangProjects\erlang_learning\src\otp\.cfg> erl -boot start_sasl -config erlog
Eshell V9.3 (abort with ^G)
1> rb:start().
rb: reading report...done.
{ok,<0.68.0>}
2>
如果报错了,请大家去检查一下配置文件中的错误日志目录是否存在,不存在一定要自己去创建一下,否则就会出现这种情况
1> rb:start().
{error,{"cannot read the index file",
{child,undefined,rb_server,
{rb,start_link,[[]]},
temporary,brutal_kill,worker,
[rb]}}}
接下来我们用带参数的start方法启动
PS C:\CodeProjects\ErlangProjects\erlang_learning\src\otp\.cfg> erl -boot start_sasl -config erlog
Eshell V9.3 (abort with ^G)
1> rb:start().
rb: reading report...done.
{ok,<0.68.0>}
2> rb:start([{max,20}]).
{error,{already_started,<0.68.0>}}
3> rb:stop().
ok
4> rb:start([{max,20}]).
rb: reading report...done.
{ok,<0.73.0>}
5>
由于之前我们启动了一个rb_server了,所以这里我们先要调用rb:stop方法将之前的停止掉,再来启动一个带参数的新的rb_server
首先必须用正确的配置文件启动Erlang,这样才能定位错误日志;然后启动报告浏览器,告诉它要读取多少日志条目(在这个案例里是最后20条)。现在列出日志里的条目。
PS C:\CodeProjects\ErlangProjects\erlang_learning\src\otp\.cfg> erl -boot start_sasl -config erlog
Eshell V9.3 (abort with ^G)
1> rb:start().
rb: reading report...done.
rb: reading report...done.
rb: reading report...done.
{ok,<0.68.0>}
2> rb:list().
No Type Process Date Time
== ==== ======= ==== ====
15 progress <0.57.0> 2024-09-13 23:48:28
14 progress <0.57.0> 2024-09-13 23:48:28
13 progress <0.57.0> 2024-09-13 23:48:28
12 progress <0.51.0> 2024-09-13 23:48:28
11 progress <0.57.0> 2024-09-13 23:48:45
10 progress <0.57.0> 2024-09-13 23:52:39
9 error <0.51.0> 2024-09-13 23:55:23
8 error <0.51.0> 2024-09-13 23:56:51
7 error <0.51.0> 2024-09-13 23:56:52
6 error <0.51.0> 2024-09-13 23:58:22
5 error <0.51.0> 2024-09-13 23:58:26
4 progress <0.57.0> 2024-09-13 23:58:58
3 progress <0.57.0> 2024-09-13 23:58:58
2 progress <0.57.0> 2024-09-13 23:58:58
1 progress <0.51.0> 2024-09-13 23:58:58
ok
3> rb:show(7).
ERROR REPORT <0.66.0> 2024-09-13 23:56:52
===============================================================================
new errorok
4>
这里有个问题,我们调用error_logger:error_msg/1产生的错误日志条目消息最终成为日志里的第1条,宁没有立即出现在list里面,而是下一次启动的时候再去读取才会把这个错误信息读取到list中
PS C:\CodeProjects\ErlangProjects\erlang_learning\src\otp\.cfg> erl -boot start_sasl -config erlog
Eshell V9.3 (abort with ^G)
1> rb:start([{max,20}]).
rb: reading report...done.
{ok,<0.68.0>}
2> rb:list().
No Type Process Date Time
== ==== ======= ==== ====
4 progress <0.57.0> 2024-09-14 00:02:23
3 progress <0.57.0> 2024-09-14 00:02:23
2 progress <0.57.0> 2024-09-14 00:02:23
1 progress <0.51.0> 2024-09-14 00:02:23
ok
3> error_logger:error_msg("new error").
ok
4>
=ERROR REPORT==== 14-Sep-2024::00:02:41 ===
new error4> rb:list().
No Type Process Date Time
== ==== ======= ==== ====
4 progress <0.57.0> 2024-09-14 00:02:23
3 progress <0.57.0> 2024-09-14 00:02:23
2 progress <0.57.0> 2024-09-14 00:02:23
1 progress <0.51.0> 2024-09-14 00:02:23
ok
5> rb:stop().
ok
6> rb:start([{max,20}]).
rb: reading report...done.
{ok,<0.75.0>}
7> rb:list().
No Type Process Date Time
== ==== ======= ==== ====
6 progress <0.57.0> 2024-09-14 00:02:23
5 progress <0.57.0> 2024-09-14 00:02:23
4 progress <0.57.0> 2024-09-14 00:02:23
3 progress <0.51.0> 2024-09-14 00:02:23
2 progress <0.57.0> 2024-09-14 00:02:29
1 error <0.51.0> 2024-09-14 00:02:41
ok
8> rb:show(1).
ERROR REPORT <0.66.0> 2024-09-14 00:02:41
===============================================================================
new errorok
9>