diff --git a/README.md b/README.md index d2f9ba3..a3cdfaf 100644 --- a/README.md +++ b/README.md @@ -95,8 +95,8 @@ include a list of sink names (without the `_lager_event` suffix) in `erl_opts`: To be useful, sinks must be configured at runtime with backends. In `app.config` for the project that requires lager, for example, extend the lager configuration to include -an `extra_sinks` tuple with backends (aka "handlers") and optionally `async_threshold` and -`async_threshold_window` values (see **Overload Protection** +an `extra_sinks` tuple with backends (aka "handlers") and optionally `asyncThreshold` and +`asyncThresholdWindow` values (see **Overload Protection** below). If async values are not configured, no overload protection will be applied on that sink. ```erlang @@ -113,8 +113,8 @@ below). If async values are not configured, no overload protection will be appli {extra_sinks, [ {audit_lager_event, [{handlers, [{lager_file_backend, [{file, "sink1.log"}, {level, info}]}]}, - {async_threshold, 500}, - {async_threshold_window, 50}] + {asyncThreshold, 500}, + {asyncThresholdWindow, 50}] }]} ]}]. ``` @@ -205,8 +205,8 @@ will send all `error_logger` messages to `error_logger.log` file. 它轮询自己的邮箱大小,并根据邮箱大小在同步和异步之间切换消息传递。 ```erlang -{async_threshold, 20}, -{async_threshold_window, 5} +{asyncThreshold, 20}, +{asyncThresholdWindow, 5} ``` 这将使用异步消息传递,直到邮箱超过20条消息为止,此时将使用同步消息传递,并在大小减小为时切换回异步20 - 5 = 15。 diff --git a/eRum.sample.config b/eRum.sample.config index d1a74de..fcd1c72 100644 --- a/eRum.sample.config +++ b/eRum.sample.config @@ -17,10 +17,11 @@ %%*********************************************** 异步日志相关 ***************************************************** %% 异步切换到同步时模式gen_event邮箱的最大消息数,此值仅应用于默认接收器; 额外的接收器也可以自己配置。 - {async_threshold, 20}, - %%当gen_event 邮箱大小从 async_threshold 减小到 async_threshold-async_threshold_window 时,切换回异步模式。 + %% 格式 integer() | undefined 当值为undefined 禁用此功能 + {asyncThreshold, 20}, + %%当gen_event 邮箱大小从 asyncThreshold 减小到 asyncThreshold-asyncThresholdWindow 时,切换回异步模式。 %% 此值仅适用于默认接收器。额外的接收器可以自己配置。 - {async_threshold_window, 5}, + {asyncThresholdWindow, 5}, %%*********************************************** handler与接收器(sink) ***************************************** %% 消息处理模块可以存在多个,[{Name, [Optins]}]=Handler() {lager_file_backend, [{file, "console.log"}, {level, info}]} 消息处理模块可以存在多个 diff --git a/src/backend/rumBackendFile.erl b/src/backend/rumBackendFile.erl index 09e9d24..ca64933 100644 --- a/src/backend/rumBackendFile.erl +++ b/src/backend/rumBackendFile.erl @@ -591,7 +591,7 @@ filesystem_test_() -> ok = rumUtil:safe_application_load(lager), ok = application:set_env(lager, handlers, [{lager_test_backend, info}]), ok = application:set_env(lager, error_logger_redirect, false), - ok = application:set_env(lager, async_threshold, undefined), + ok = application:set_env(lager, asyncThreshold, undefined), {ok, _TestDir} = rumUtil:create_test_dir(), ok = eRum:start(), %% race condition where lager logs its own start up @@ -1026,7 +1026,7 @@ trace_files_test_() -> {lager_file_backend, Events}, [{module, ?MODULE}], notice } ]), - ok = application:set_env(lager, async_threshold, undefined), + ok = application:set_env(lager, asyncThreshold, undefined), ok = eRum:start(), {Log, Debug, Events} end, diff --git a/src/eRum_app.erl b/src/eRum_app.erl index 06e87c5..9e1503a 100644 --- a/src/eRum_app.erl +++ b/src/eRum_app.erl @@ -7,8 +7,8 @@ -export([ start/0 , start/2 - , boot/1 , stop/1 + , doStart/1 , start_handler/3 , configure_sink/2 ]). @@ -21,14 +21,14 @@ start() -> start(_StartType, _StartArgs) -> {ok, Pid} = eRum_sup:start_link(), SavedHandlers = boot(), - _ = boot('__all_extra'), - _ = boot('__traces'), + _ = doStart('__all_extra'), + _ = doStart('__traces'), clean_up_config_checks(), {ok, Pid, SavedHandlers}. boot() -> %% Handle the default sink. - determine_async_behavior(?RumDefSink, application:get_env(lager, async_threshold, undefined), application:get_env(lager, async_threshold_window, undefined)), + tryStartAsyncer(?RumDefSink, rumUtil:get_env(?RumAppName, asyncThreshold, undefined), rumUtil:get_env(?RumAppName, asyncThresholdWindow, undefined)), _ = maybe_install_sink_killer(?RumDefSink, application:get_env(lager, killer_hwm, undefined), application:get_env(lager, killer_reinstall_after, undefined)), @@ -45,15 +45,15 @@ boot() -> SavedHandlers. -boot('__traces') -> +doStart('__traces') -> _ = rumUtil:trace_filter(none), ok = add_configured_traces(); -boot('__all_extra') -> +doStart('__all_extra') -> configure_extra_sinks(application:get_env(lager, extra_sinks, [])); -boot(?RumDefSink) -> boot(); -boot(Sink) -> +doStart(?RumDefSink) -> boot(); +doStart(Sink) -> AllSinksDef = application:get_env(lager, extra_sinks, []), boot_sink(Sink, lists:keyfind(Sink, 1, AllSinksDef)). @@ -68,21 +68,21 @@ stop(Handlers) -> end, Handlers), rumConfig:cleanup(). -determine_async_behavior(_Sink, undefined, _Window) -> +tryStartAsyncer(_Sink, undefined, _Window) -> ok; -determine_async_behavior(_Sink, Threshold, _Window) when not is_integer(Threshold) orelse Threshold < 0 -> - error_logger:error_msg("Invalid value for 'async_threshold': ~p~n", [Threshold]), +tryStartAsyncer(_Sink, Threshold, _Window) when not is_integer(Threshold) orelse Threshold < 0 -> + error_logger:error_msg("Invalid value for 'asyncThreshold': ~p~n", [Threshold]), throw({error, bad_config}); -determine_async_behavior(Sink, Threshold, undefined) -> +tryStartAsyncer(Sink, Threshold, undefined) -> startThrottle(Sink, Threshold, erlang:trunc(Threshold * 0.2)); -determine_async_behavior(_Sink, Threshold, Window) when not is_integer(Window) orelse Window > Threshold orelse Window < 0 -> - error_logger:error_msg("Invalid value for 'async_threshold_window': ~p~n", [Window]), +tryStartAsyncer(_Sink, Threshold, Window) when not is_integer(Window) orelse Window > Threshold orelse Window < 0 -> + error_logger:error_msg("Invalid value for 'asyncThresholdWindow': ~p~n", [Window]), throw({error, bad_config}); -determine_async_behavior(Sink, Threshold, Window) -> +tryStartAsyncer(Sink, Threshold, Window) -> startThrottle(Sink, Threshold, Window). startThrottle(Sink, Threshold, Window) -> - _ = supervisor:start_child(rumHandlerWatcherSup, [Sink, ?RumBackendThrottle, [Threshold, Window]]), + _ = supervisor:start_child(rumHWatcherSup, [Sink, ?RumBackendThrottle, [Threshold, Window]]), ok. start_handlers(_Sink, undefined) -> @@ -214,7 +214,7 @@ configure_sink(Sink, SinkDef) -> rumConfig:initSink(Sink), ChildId = rumUtil:make_internal_sink_name(Sink), _ = supervisor:start_child(eRum_sup, {ChildId, {gen_event, start_link, [{local, Sink}]}, permanent, 5000, worker, dynamic}), - determine_async_behavior(Sink, proplists:get_value(async_threshold, SinkDef), proplists:get_value(async_threshold_window, SinkDef)), + tryStartAsyncer(Sink, proplists:get_value(asyncThreshold, SinkDef), proplists:get_value(asyncThresholdWindow, SinkDef)), _ = maybe_install_sink_killer(Sink, proplists:get_value(killer_hwm, SinkDef), proplists:get_value(killer_reinstall_after, SinkDef)), start_handlers(Sink, proplists:get_value(handlers, SinkDef, [])), diff --git a/test/lager_manager_killer_test.erl b/test/lager_manager_killer_test.erl index 59a6f2b..f9fd816 100644 --- a/test/lager_manager_killer_test.erl +++ b/test/lager_manager_killer_test.erl @@ -18,7 +18,7 @@ overload_test_() -> KillerHWM = 10, % kill the manager if there are more than 10 pending logs KillerReinstallAfter = 1000, % reinstall killer after 1 sec application:set_env(lager, handlers, [{lager_slow_backend, [{delay, Delay}]}]), - application:set_env(lager, async_threshold, undefined), + application:set_env(lager, asyncThreshold, undefined), application:set_env(lager, error_logger_redirect, true), application:set_env(lager, killer_hwm, KillerHWM), application:set_env(lager, killer_reinstall_after, KillerReinstallAfter), @@ -59,7 +59,7 @@ overload_alternate_sink_test_() -> {handlers, [{lager_slow_backend, [{delay, Delay}]}]}, {killer_hwm, KillerHWM}, {killer_reinstall_after, KillerReinstallAfter}, - {async_threshold, undefined} + {asyncThreshold, undefined} ]}]), application:set_env(lager, error_logger_redirect, true), ensure_started(lager), diff --git a/test/lager_rotate.erl b/test/lager_rotate.erl index da494b0..d205090 100644 --- a/test/lager_rotate.erl +++ b/test/lager_rotate.erl @@ -65,7 +65,7 @@ rotate_test_() -> [{lager_file_backend, [{file, Sink}, {level, info}]}]} ]}]), application:set_env(lager, error_logger_redirect, false), - application:set_env(lager, async_threshold, undefined), + application:set_env(lager, asyncThreshold, undefined), eRum:start(), timer:sleep(1000), State diff --git a/test/lager_test_backend.erl b/test/lager_test_backend.erl index 01c20da..b3f6bd8 100644 --- a/test/lager_test_backend.erl +++ b/test/lager_test_backend.erl @@ -1758,7 +1758,7 @@ async_threshold_test_() -> _ = error_logger:tty(false), _ = application:stop(lager), _ = application:stop(goldrush), - _ = application:unset_env(lager, async_threshold), + _ = application:unset_env(lager, asyncThreshold), if Reset -> true = ets:delete(async_threshold_test), @@ -1777,8 +1777,8 @@ async_threshold_test_() -> ?assertEqual(true, ets:insert_new(async_threshold_test, {async_toggled, 0})), _ = application:load(lager), ok = application:set_env(lager, error_logger_redirect, false), - ok = application:set_env(lager, async_threshold, 2), - ok = application:set_env(lager, async_threshold_window, 1), + ok = application:set_env(lager, asyncThreshold, 2), + ok = application:set_env(lager, asyncThresholdWindow, 1), ok = application:set_env(lager, handlers, [{?MODULE, info}]), ok = lager:start(), true @@ -1874,7 +1874,7 @@ high_watermark_test_() -> application:load(lager), application:set_env(lager, error_logger_redirect, true), application:set_env(lager, handlers, [{lager_test_backend, info}]), - application:set_env(lager, async_threshold, undefined), + application:set_env(lager, asyncThreshold, undefined), lager:start() end, fun(_) ->