浏览代码

ft:修改为基于lager3.9.0版本

master
SisMaker 4 年前
父节点
当前提交
5ac5f7f862
共有 9 个文件被更改,包括 629 次插入657 次删除
  1. +15
    -0
      README.md
  2. +2
    -2
      include/eRum.hrl
  3. +1
    -1
      rebar.config
  4. +4
    -2
      src/eRum.app.src
  5. +2
    -2
      src/transform/rumTransform.erl
  6. +22
    -20
      src/utils/rumUtil.erl
  7. +4
    -13
      test/crash_statem.erl
  8. +533
    -523
      test/lager_test_backend.erl
  9. +46
    -94
      test/lager_trace_test.erl

+ 15
- 0
README.md 查看文件

@ -1,3 +1,6 @@
# 基于lager 3.9.0 rewrite
Overview
--------
Lager (as in the beer) is a logging framework for Erlang. Its purpose is to provide a more traditional way to perform
@ -1064,6 +1067,18 @@ Example Usage:
3.x Changelog
-------------
3.9.0 - 24 February 2021
* Bugfix: Try to make a log root of "log" more sensible (#540)
* Feature: Further adapt to OTP 24 (also remove pre OTP 21 code),
adopt Github Actions for tests
3.8.2 - 4 February 2021
* Bugfix: Make directory expansion return an absolute path (#535)
* Feature: Write crash.log under the log_root location (#536)
* Bugfix: Handle line numbering correctly in forthcoming OTP 24 release (#537)
3.8.1 - 28 August 2020

+ 2
- 2
include/eRum.hrl 查看文件

@ -17,8 +17,8 @@
-define(DEFAULT_HANDLER_CONF,
[
{lager_console_backend, [{level, info}]},
{lager_file_backend, [{file, "log/error.log"}, {level, error}, {size, 10485760}, {date, "$D0"}, {count, 5}]},
{lager_file_backend, [{file, "log/console.log"}, {level, info}, {size, 10485760}, {date, "$D0"}, {count, 5}]}
{lager_file_backend, [{file, "error.log"}, {level, error}, {size, 10485760}, {date, "$D0"}, {count, 5}]},
{lager_file_backend, [{file, "console.log"}, {level, info}, {size, 10485760}, {date, "$D0"}, {count, 5}]}
]).
%%

+ 1
- 1
rebar.config 查看文件

@ -1,6 +1,5 @@
{erl_opts, [
{lager_extra_sinks, ['__lager_test_sink']},
{platform_define, "^(19|20|21|22)", test_statem},
{platform_define, "^18", 'FUNCTION_NAME', unavailable},
{platform_define, "^18", 'FUNCTION_ARITY', 0},
debug_info,
@ -30,6 +29,7 @@
{deps, [
{eGbh, ".*", {git, "http://47.108.26.175:53000/SisMaker/eGbh.git", {branch, "master"}}},
{eFmt, ".*", {git, "http://47.108.26.175:53000/SisMaker/eFmt.git", {branch, "master"}}},
{goldrush, "0.1.9"}
]}.

+ 4
- 2
src/eRum.app.src 查看文件

@ -1,7 +1,7 @@
{application, eRum,
[
{description, "Erlang logger application"},
{vsn, "0.1.0"},
{vsn, "3.9.0"},
{modules, []},
{applications, [kernel, stdlib, goldrush]},
{registered, [eRum_sup, lager_event, rumCrashLog, rumHleWatcherSup]},
@ -21,8 +21,10 @@
{alert, "\e[1;44m"},
{emergency, "\e[1;41m"}]},
%% Where to write the logs
{log_root, "log"},
%% Whether to write a crash log, and where. False means no crash logger.
{crash_log, "log/crash.log"},
{crash_log, "crash.log"},
%% Maximum size in bytes of events in the crash log - defaults to 65536
{crash_log_msg_size, 65536},
%% Maximum size of the crash log in bytes, before its rotated, set

+ 2
- 2
src/transform/rumTransform.erl 查看文件

@ -244,8 +244,8 @@ handle_args(DefaultAttrs, Line, [Arg1, Arg2]) ->
handle_args(DefaultAttrs, _Line, [Attrs, Format, Args]) ->
{concat_lists(Attrs, DefaultAttrs), Format, Args}.
make_varname(Prefix, Line) ->
list_to_atom(Prefix ++ atom_to_list(get(module)) ++ integer_to_list(Line)).
make_varname(Prefix, CallAnno) ->
list_to_atom(Prefix ++ atom_to_list(get(module)) ++ integer_to_list(erl_anno:line(CallAnno))).
%% concat 2 list ASTs by replacing the terminating [] in A with the contents of B
concat_lists({var, Line, _Name} = Var, B) ->

+ 22
- 20
src/utils/rumUtil.erl 查看文件

@ -480,19 +480,21 @@ is_loggable(Msg, SeverityThreshold, MyName) when is_integer(SeverityThreshold) -
%% When log_root option is provided, get the real path to a file
expand_path(RelPath) ->
case application:get_env(lager, log_root) of
{ok, LogRoot} when is_list(LogRoot) -> % Join relative path
%% check if the given RelPath contains LogRoot, if so, do not add
%% it again; see gh #304
case string:str(filename:dirname(RelPath), LogRoot) of
X when X > 0 ->
RelPath;
_Zero ->
filename:join(LogRoot, RelPath)
end;
undefined -> % No log_root given, keep relative path
RelPath
end.
RelPath2 = case application:get_env(lager, log_root) of
{ok, LogRoot} when is_list(LogRoot) -> % Join relative path
%% check if the given RelPath contains LogRoot, if so, do not add
%% it again; see gh #304
case filename:dirname(RelPath) of
"." ->
filename:join(LogRoot, RelPath);
false ->
RelPath
end;
undefined -> % No log_root given, keep relative path
RelPath
end,
%% see #534 make sure c:cd can't change file path, trans filename to abs name
filename:absname(RelPath2).
%% Find a file among the already installed handlers.
%%
@ -501,8 +503,8 @@ expand_path(RelPath) ->
find_file(_File1, _Handlers = []) ->
false;
find_file(File1, [{{lager_file_backend, File2}, _Handler, _Sink} = HandlerInfo | Handlers]) ->
File1Abs = filename:absname(File1),
File2Abs = filename:absname(rumUtil:expand_path(File2)),
File1Abs = File1,
File2Abs = lager_util:expand_path(File2),
case File1Abs =:= File2Abs of
true ->
% The file inside HandlerInfo is the same as the file we are looking
@ -851,13 +853,13 @@ expand_path_test() ->
OldRootVal = application:get_env(lager, log_root),
ok = application:unset_env(lager, log_root),
?assertEqual("/foo/bar", expand_path("/foo/bar")),
?assertEqual("foo/bar", expand_path("foo/bar")),
?assertEqual(filename:absname("/foo/bar"), expand_path("/foo/bar")),
?assertEqual(filename:absname("foo/bar"), expand_path("foo/bar")),
ok = application:set_env(lager, log_root, "log/dir"),
?assertEqual("/foo/bar", expand_path("/foo/bar")), % Absolute path should not be changed
?assertEqual("log/dir/foo/bar", expand_path("foo/bar")),
?assertEqual("log/dir/foo/bar", expand_path("log/dir/foo/bar")), %% gh #304
?assertEqual(filename:absname("/foo/bar"), expand_path("/foo/bar")), % Absolute path should not be changed
?assertEqual(filename:absname("log/dir/foo/bar"), expand_path("foo/bar")),
?assertEqual(filename:absname("log/dir/foo/bar"), expand_path("log/dir/foo/bar")), %% gh #304
case OldRootVal of
undefined -> application:unset_env(lager, log_root);

+ 4
- 13
test/crash_statem.erl 查看文件

@ -1,6 +1,5 @@
-module(crash_statem).
%% we're only going to compile this on OTP 19+
-ifdef(test_statem).
-behaviour(gen_statem).
-export([
@ -11,10 +10,10 @@
handle_event/4
]).
-export([terminate/3, code_change/4, init/1, callback_mode/0]).
-export([terminate/3,code_change/4,init/1,callback_mode/0]).
start() ->
gen_statem:start({local, ?MODULE}, ?MODULE, [], []).
gen_statem:start({local,?MODULE}, ?MODULE, [], []).
crash() ->
gen_statem:call(?MODULE, boom).
@ -27,11 +26,11 @@ timeout() ->
%% Mandatory callback functions
terminate(_Reason, _State, _Data) -> ok.
code_change(_Vsn, State, Data, _Extra) -> {ok, State, Data}.
code_change(_Vsn, State, Data, _Extra) -> {ok,State,Data}.
init([]) ->
%% insert rant here about breaking changes in minor versions...
case erlang:system_info(version) of
"8.0" -> {callback_mode(), state1, undefined};
"8.0" -> {callback_mode(),state1,undefined};
_ -> {ok, state1, undefined}
end.
@ -45,11 +44,3 @@ handle_event({call, _From}, timeout, _Arg, _Data) ->
{keep_state_and_data, [{state_timeout, 0, timeout}]};
handle_event({call, _From}, {stop, Reason}, state1, _Data) ->
{stop, Reason}.
-else.
-export([start/0, crash/0]).
start() -> ok.
crash() -> ok.
-endif.

+ 533
- 523
test/lager_test_backend.erl
文件差异内容过多而无法显示
查看文件


+ 46
- 94
test/lager_trace_test.erl 查看文件

@ -1,103 +1,55 @@
-module(lager_trace_test).
-module(pr_stacktrace_test).
-compile([{parse_transform, lager_transform}]).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
% Our expectation is that the first log entry will appear so we won't actually
% wait out ?FIRST_LOG_ENTRY_TIMEOUT. On the other hand, the second log entry is
% expected never to arrive, so the test will wait out ?SECOND_LOG_ENTRY_TIMEOUT;
% that's why it is shorter.
-define(FIRST_LOG_ENTRY_TIMEOUT, (10 * 1000)). % 10 seconds
-define(SECOND_LOG_ENTRY_TIMEOUT, 1000). % 1 second
make_throw() ->
throw({test, exception}).
-define(FNAME, "test/test1.log").
bad_arity() ->
lists:concat([], []).
trace_test_() ->
{timeout,
10,
{foreach,
fun() ->
file:write_file(?FNAME, ""),
error_logger:tty(false),
application:load(lager),
application:set_env(lager, log_root, "test"),
application:set_env(lager, handlers,
[{lager_file_backend,
[{file, "test1.log"},
{level, none},
{formatter, lager_default_formatter},
{formatter_config, [message, "\n"]}
]}]),
application:set_env(lager, traces,
[{{lager_file_backend, "test1.log"},
[{tag, mytag}], info}]),
application:set_env(lager, error_logger_redirect, false),
application:set_env(lager, async_threshold, undefined),
eRum:start()
bad_arg() ->
integer_to_list(1.0).
pr_stacktrace_throw_test() ->
Got = try
make_throw()
catch
Class:Reason:Stacktrace ->
lager:pr_stacktrace(Stacktrace, {Class, Reason})
end,
fun(_) ->
file:delete(?FNAME),
application:stop(lager),
application:stop(goldrush),
application:unset_env(lager, log_root),
application:unset_env(lager, handlers),
application:unset_env(lager, traces),
application:unset_env(lager, error_logger_redirect),
application:unset_env(lager, async_threshold),
error_logger:tty(true)
Want = "pr_stacktrace_test:pr_stacktrace_throw_test/0 line 26\n pr_stacktrace_test:make_throw/0 line 16\nthrow:{test,exception}",
?assertNotEqual(nomatch, string:find(Got, Want)).
pr_stacktrace_bad_arg_test() ->
Got = try
bad_arg()
catch
Class:Reason:Stacktrace ->
lager:pr_stacktrace(Stacktrace, {Class, Reason})
end,
[{"Trace combined with log_root",
fun() ->
eRum:info([{tag, mytag}], "Test message"),
% Wait until we have the expected log entry in the log file.
case wait_until(fun() ->
count_lines(?FNAME) >= 1
end, ?FIRST_LOG_ENTRY_TIMEOUT) of
ok ->
ok;
{error, timeout} ->
throw({file_empty, file:read_file(?FNAME)})
end,
% Let's wait a little to see that we don't get a duplicate log
% entry.
case wait_until(fun() ->
count_lines(?FNAME) >= 2
end, ?SECOND_LOG_ENTRY_TIMEOUT) of
ok ->
throw({too_many_entries, file:read_file(?FNAME)});
{error, timeout} ->
ok
end
end}
]}}.
% Wait until Fun() returns true.
wait_until(Fun, Timeout) ->
wait_until(Fun, Timeout, {8, 13}).
wait_until(_Fun, Timeout, {T1, _}) when T1 > Timeout ->
{error, timeout};
wait_until(Fun, Timeout, {T1, T2}) ->
case Fun() of
true ->
ok;
false ->
timer:sleep(T1),
wait_until(Fun, Timeout, {T2, T1 + T2})
end.
% Return the number of lines in a file. Return 0 for a non-existent file.
count_lines(Filename) ->
case file:read_file(Filename) of
{ok, Content} ->
Lines = binary:split(Content, <<"\n">>, [global, trim]),
length(Lines);
{error, _} ->
0
end.
-endif.
Want = "pr_stacktrace_test:pr_stacktrace_bad_arg_test/0 line 36\n pr_stacktrace_test:bad_arg/0 line 22\nerror:badarg",
?assertNotEqual(nomatch, string:find(Got, Want)).
pr_stacktrace_bad_arity_test() ->
Got = try
bad_arity()
catch
Class:Reason:Stacktrace ->
lager:pr_stacktrace(Stacktrace, {Class, Reason})
end,
Want = "pr_stacktrace_test:pr_stacktrace_bad_arity_test/0 line 46\n lists:concat([], [])\nerror:undef",
?assertNotEqual(nomatch, string:find(Got, Want)).
pr_stacktrace_no_reverse_test() ->
application:set_env(lager, reverse_pretty_stacktrace, false),
Got = try
bad_arity()
catch
Class:Reason:Stacktrace ->
lager:pr_stacktrace(Stacktrace, {Class, Reason})
end,
Want = "error:undef\n lists:concat([], [])\n pr_stacktrace_test:pr_stacktrace_bad_arity_test/0 line 57",
?assertEqual(nomatch, string:find(Got, Want)).

正在加载...
取消
保存