From bc3e330c0c254374154fea1779eb5146f0f0bc92 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Fri, 2 Nov 2018 19:23:53 +0800 Subject: [PATCH] Change lager to OTP logger (#3) --- include/hut.hrl | 130 ++--------------------------------- rebar.config | 8 +-- test/gen_rpc.master.config | 12 ---- test/gen_rpc.slave.config | 12 ---- test/gen_rpc_test_helper.erl | 5 -- test/include/ct.hrl | 15 +--- 6 files changed, 8 insertions(+), 174 deletions(-) diff --git a/include/hut.hrl b/include/hut.hrl index e2f42db..19cd67c 100644 --- a/include/hut.hrl +++ b/include/hut.hrl @@ -1,128 +1,6 @@ -%% -*- erlang -*- -%% forked from "https://github.com/tolbrino/hut/tree/v1.2.0" - --ifndef(__HUT_HRL__). --define(__HUT_HRL__, true). - -%% Supported logging levels (taken from lager): --define(log_levels, [debug, info, notice, warning, error, critical, alert, emergency]). --define(default_log_level, info). --define(default_use_log_level_gate, true). - -%% Helper macros --define(__fmt(__Fmt, __Args), lists:flatten(io_lib:format(__Fmt, __Args))). - --define(__maybe_log(__Level, __Fun), - ((fun() -> - __UseGate = application:get_env(hut, use_log_level_gate, ?default_use_log_level_gate), - case __UseGate of - true -> - __CurrentLevel = application:get_env(hut, level, ?default_log_level), - __AllowedLevels = lists:dropwhile(fun(__Element) -> __Element =/= __CurrentLevel end, ?log_levels), - __IsEnabled = lists:member(__Level, __AllowedLevels), - case __IsEnabled of - true -> - __Fun(); - _ -> - ok - end; - _ -> - __Fun() - end - end)())). - -%% Lager support - --ifdef(HUT_LAGER). --define(log_type, "lager"). - --ifndef(HUT_LAGER_SINK). --define(HUT_LAGER_SINK, lager). --endif. - -define(log(__Level, __Fmt), - ?HUT_LAGER_SINK:__Level([], __Fmt, [])). + logger:__Level(__Fmt)). -define(log(__Level, __Fmt, __Args), - ?HUT_LAGER_SINK:__Level([], __Fmt, __Args)). --define(log(__Level, __Fmt, __Args, __Opts), - ?HUT_LAGER_SINK:__Level(__Opts, __Fmt, __Args)). - --else. - -% Using plain `io:format/2`. - --ifdef(HUT_IOFORMAT). --define(log_type, "ioformat"). - --define(log(__Level, __Fmt), - ?__maybe_log(__Level, fun() -> io:format("~p: " ++ __Fmt ++ "~n", [__Level]) end)). --define(log(__Level, __Fmt, __Args), - ?__maybe_log(__Level, fun() -> io:format("~p: " ++ __Fmt ++ "~n", [__Level] ++ __Args) end)). --define(log(__Level, __Fmt, __Args, __Opts), - ?__maybe_log(__Level, fun() -> io:format("~p: " ++ __Fmt ++ "; Opts: ~p~n", [__Level] ++ __Args ++ [__Opts]) end)). - --else. - -% All logging calls are passed into a custom logging callback module given by `HUT_CUSTOM_CB`. - --ifdef(HUT_CUSTOM). --ifdef(HUT_CUSTOM_CB). --define(log_type, "custom"). - --define(log(__Level, __Fmt), - ?__maybe_log(__Level, fun() -> ?HUT_CUSTOM_CB:log(__Level, __Fmt, [], []) end)). --define(log(__Level, __Fmt, __Args), - ?__maybe_log(__Level, fun() -> ?HUT_CUSTOM_CB:log(__Level, __Fmt, __Args, []) end)). --define(log(__Level, __Fmt, __Args, __Opts), - ?__maybe_log(__Level, fun() -> ?HUT_CUSTOM_CB:log(__Level, __Fmt, __Args, __Opts) end)). - --endif. --else. - -% All logging calls are ignored. - --ifdef(HUT_NOOP). --define(log_type, "noop"). - --define(log(__Level, __Fmt), true). --define(log(__Level, __Fmt, __Args), true). --define(log(__Level, __Fmt, __Args, __Opts), true). - --else. - -% If none of the above options was defined, we default to using OTP sasl's error_logger. --define(log_type, "default"). - --define(__log_error_logger(__Level, __Fmt, __Args, __Opts), - ((fun() -> - case __Level of - info -> - error_logger:info_report([{msg, ?__fmt(__Fmt, __Args)}, {options, __Opts}]); - warning -> - error_logger:warning_report([{msg, ?__fmt(__Fmt, __Args)}, {options, __Opts}]); - error -> - error_logger:error_report([{msg, ?__fmt(__Fmt, __Args)}, {options, __Opts}]); - _ when __Level =:= debug; __Level =:= notice -> - error_logger:info_report([{sublevel, __Level}, {msg, ?__fmt(__Fmt, __Args)}, {options, __Opts}]); - _ when __Level =:= critical; __Level =:= alert; __Level =:= emergency -> - error_logger:error_report([{sublevel, __Level}, {msg, ?__fmt(__Fmt, __Args)}, {options, __Opts}]); - _ -> - ok - end - end)())). - --define(log(__Level, __Fmt), - ?__maybe_log(__Level, fun() -> ?__log_error_logger(__Level, __Fmt, [], []) end)). --define(log(__Level, __Fmt, __Args), - ?__maybe_log(__Level, fun() -> ?__log_error_logger(__Level, __Fmt, __Args, []) end)). --define(log(__Level, __Fmt, __Args, __Opts), - ?__maybe_log(__Level, fun() -> ?__log_error_logger(__Level, __Fmt, __Args, __Opts) end)). - -% End of all actual log implementation switches. --endif. --endif. --endif. --endif. - -% End of log declarations --endif. + logger:__Level(__Fmt, __Args)). +-define(log(__Level, __Fmt, __Args, __Meta), + logger:__Level(__Fmt, __Args, __Meta)). \ No newline at end of file diff --git a/rebar.config b/rebar.config index bad82c7..8ca9da2 100644 --- a/rebar.config +++ b/rebar.config @@ -8,7 +8,7 @@ %% Plugins {plugins, [rebar3_hex]}. {shell, [ - {apps, [lager, sync, gen_rpc]} + {apps, [sync, gen_rpc]} ]}. {erl_opts, [debug_info, @@ -39,20 +39,18 @@ {test, [ {erl_opts, [{d,'TEST'}, {d,'HUT_LAGER'}, - {parse_transform, lager_transform}, warnings_as_errors, export_all, no_inline_list_funcs]}, - {deps, [{lager, "~> 3.0"}, + {deps, [ {eunit_formatters, "~> 0.3"} ]} ]}, {dev, [ {erl_opts, [{d,'HUT_LAGER'}, - {parse_transform, lager_transform}, warnings_as_errors, no_inline_list_funcs]}, - {deps, [{lager, "~> 3.0"}, + {deps, [ {sync, {git, "git://github.com/rustyio/sync.git", {branch, "master"}}} ]} ]} diff --git a/test/gen_rpc.master.config b/test/gen_rpc.master.config index ef3a09c..38e2de6 100644 --- a/test/gen_rpc.master.config +++ b/test/gen_rpc.master.config @@ -26,17 +26,5 @@ {sasl, [ {errlog_type, error}, {error_logger_mf_dir, false} - ]}, - {lager, [ - {log_root, "./log"}, - {crash_log, "crash.log"}, - {crash_log_size, 0}, - {colored, true}, - {handlers, [ - {lager_console_backend, [debug, - {lager_default_formatter, ["[", date, " ", time, "] severity=", severity, " node=\"", {node, "undefined"}, "\" pid=\"", pid, - "\" module=", {module, "gen_rpc"}, " function=", {function, "undefined"}, " ", message, "\n"]} - ]} - ]} ]} ]. diff --git a/test/gen_rpc.slave.config b/test/gen_rpc.slave.config index de013a8..247f0f2 100644 --- a/test/gen_rpc.slave.config +++ b/test/gen_rpc.slave.config @@ -26,17 +26,5 @@ {sasl, [ {errlog_type, error}, {error_logger_mf_dir, false} - ]}, - {lager, [ - {log_root, "./log"}, - {crash_log, "crash.log"}, - {crash_log_size, 0}, - {colored, true}, - {handlers, [ - {lager_console_backend, [debug, - {lager_default_formatter, ["[", date, " ", time, "] severity=", severity, " node=\"", {node, "undefined"}, "\" pid=\"", pid, - "\" module=", {module, "gen_rpc"}, " function=", {function, "undefined"}, " ", message, "\n"]} - ]} - ]} ]} ]. diff --git a/test/gen_rpc_test_helper.erl b/test/gen_rpc_test_helper.erl index 16fb966..06e3c69 100644 --- a/test/gen_rpc_test_helper.erl +++ b/test/gen_rpc_test_helper.erl @@ -47,8 +47,6 @@ start_distribution(Node)-> start_master(Driver) -> ok = set_application_environment(?MASTER), ok = set_driver_configuration(Driver, ?MASTER), - %% Start lager - {ok, _LApps} = application:ensure_all_started(lager), %% Start the application remotely {ok, _Apps} = application:ensure_all_started(?APP), ok. @@ -62,8 +60,6 @@ start_slave(Driver) -> ok = rpc:call(?SLAVE, code, add_pathsz, [code:get_path()]), ok = set_application_environment(?SLAVE), ok = set_driver_configuration(Driver, ?SLAVE), - %% Start lager - {ok, _SlaveLApps} = rpc:call(?SLAVE, application, ensure_all_started, [lager]), %% Start the application remotely {ok, _SlaveApps} = rpc:call(?SLAVE, application, ensure_all_started, [?APP]), ok. @@ -133,7 +129,6 @@ restart_application() -> _ = application:stop(?APP), _ = application:unload(?APP), ok = timer:sleep(100), - {ok, _LApps} = application:ensure_all_started(lager), {ok, _Apps} = application:ensure_all_started(?APP), ok. diff --git a/test/include/ct.hrl b/test/include/ct.hrl index 7e10620..267de1a 100644 --- a/test/include/ct.hrl +++ b/test/include/ct.hrl @@ -27,18 +27,5 @@ ?SLAVE => ?SLAVE_PORT }}}, {?APP, connect_timeout, 500}, - {?APP, send_timeout, 500}, - {lager, log_root, "./log"}, - {lager, crash_log, "crash.log"}, - {lager, crash_log_size, 0}, - {lager, colored, false}, - {lager, handlers, [ - %% Commented out to reduce test output polution, uncomment during development - % {lager_common_test_backend, [debug, - % {lager_default_formatter, ["[", date, " ", time, "] severity=", severity, " node=\"", {node, "undefined"}, "\" pid=\"", pid, - % "\" module=", {module, "gen_rpc"}, " function=", {function, "undefined"}, " ", message, "\n"]}]}, - {lager_file_backend, [{file, "messages.log"}, {level, debug}, {formatter, lager_default_formatter}, {size, 0}, {date, "$D0"}, {count, 7}, - {formatter_config, ["[", date, " ", time, "] severity=", severity, " node=\"", {node, "undefined"}, "\" pid=\"", pid, - "\" module=", {module, "gen_rpc"}, " function=", {function, "undefined"}, " ", message, "\n"]}]} - ]} + {?APP, send_timeout, 500} ]).