Merge pull request #10607 from zhongwencool/log-conf-refactor

feat: simplify log configuration
This commit is contained in:
zhongwencool 2023-05-11 16:12:27 +08:00 committed by GitHub
commit a736b633b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 329 additions and 139 deletions

View File

@ -29,7 +29,7 @@
{esockd, {git, "https://github.com/emqx/esockd", {tag, "5.9.6"}}}, {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.9.6"}}},
{ekka, {git, "https://github.com/emqx/ekka", {tag, "0.15.1"}}}, {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.15.1"}}},
{gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.8.1"}}}, {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.8.1"}}},
{hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.39.4"}}}, {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.39.6"}}},
{emqx_http_lib, {git, "https://github.com/emqx/emqx_http_lib.git", {tag, "0.5.2"}}}, {emqx_http_lib, {git, "https://github.com/emqx/emqx_http_lib.git", {tag, "0.5.2"}}},
{pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}}, {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}},
{recon, {git, "https://github.com/ferd/recon", {tag, "2.5.1"}}}, {recon, {git, "https://github.com/ferd/recon", {tag, "2.5.1"}}},

View File

@ -112,8 +112,8 @@ update_log_handler({Action, {handler, Id, Mod, Conf}}) ->
end, end,
ok. ok.
id_for_log(console) -> "log.console_handler"; id_for_log(console) -> "log.console";
id_for_log(Other) -> "log.file_handlers." ++ atom_to_list(Other). id_for_log(Other) -> "log.file." ++ atom_to_list(Other).
atom(Id) when is_binary(Id) -> binary_to_atom(Id, utf8); atom(Id) when is_binary(Id) -> binary_to_atom(Id, utf8);
atom(Id) when is_atom(Id) -> Id. atom(Id) when is_atom(Id) -> Id.
@ -126,12 +126,12 @@ tr_handlers(Conf) ->
%% For the default logger that outputs to console %% For the default logger that outputs to console
tr_console_handler(Conf) -> tr_console_handler(Conf) ->
case conf_get("log.console_handler.enable", Conf) of case conf_get("log.console.enable", Conf) of
true -> true ->
ConsoleConf = conf_get("log.console_handler", Conf), ConsoleConf = conf_get("log.console", Conf),
[ [
{handler, console, logger_std_h, #{ {handler, console, logger_std_h, #{
level => conf_get("log.console_handler.level", Conf), level => conf_get("log.console.level", Conf),
config => (log_handler_conf(ConsoleConf))#{type => standard_io}, config => (log_handler_conf(ConsoleConf))#{type => standard_io},
formatter => log_formatter(ConsoleConf), formatter => log_formatter(ConsoleConf),
filters => log_filter(ConsoleConf) filters => log_filter(ConsoleConf)
@ -150,14 +150,10 @@ tr_file_handler({HandlerName, SubConf}) ->
{handler, atom(HandlerName), logger_disk_log_h, #{ {handler, atom(HandlerName), logger_disk_log_h, #{
level => conf_get("level", SubConf), level => conf_get("level", SubConf),
config => (log_handler_conf(SubConf))#{ config => (log_handler_conf(SubConf))#{
type => type => wrap,
case conf_get("rotation.enable", SubConf) of file => conf_get("to", SubConf),
true -> wrap; max_no_files => conf_get("rotation_count", SubConf),
_ -> halt max_no_bytes => conf_get("rotation_size", SubConf)
end,
file => conf_get("file", SubConf),
max_no_files => conf_get("rotation.count", SubConf),
max_no_bytes => conf_get("max_size", SubConf)
}, },
formatter => log_formatter(SubConf), formatter => log_formatter(SubConf),
filters => log_filter(SubConf), filters => log_filter(SubConf),
@ -165,14 +161,11 @@ tr_file_handler({HandlerName, SubConf}) ->
}}. }}.
logger_file_handlers(Conf) -> logger_file_handlers(Conf) ->
Handlers = maps:to_list(conf_get("log.file_handlers", Conf, #{})),
lists:filter( lists:filter(
fun({_Name, Opts}) -> fun({_Name, Handler}) ->
B = conf_get("enable", Opts), conf_get("enable", Handler, false)
true = is_boolean(B),
B
end, end,
Handlers maps:to_list(conf_get("log.file", Conf, #{}))
). ).
conf_get(Key, Conf) -> emqx_schema:conf_get(Key, Conf). conf_get(Key, Conf) -> emqx_schema:conf_get(Key, Conf).
@ -237,12 +230,8 @@ log_filter(Conf) ->
end. end.
tr_level(Conf) -> tr_level(Conf) ->
ConsoleLevel = conf_get("log.console_handler.level", Conf, undefined), ConsoleLevel = conf_get("log.console.level", Conf, undefined),
FileLevels = [ FileLevels = [conf_get("level", SubConf) || {_, SubConf} <- logger_file_handlers(Conf)],
conf_get("level", SubConf)
|| {_, SubConf} <-
logger_file_handlers(Conf)
],
case FileLevels ++ [ConsoleLevel || ConsoleLevel =/= undefined] of case FileLevels ++ [ConsoleLevel || ConsoleLevel =/= undefined] of
%% warning is the default level we should use %% warning is the default level we should use
[] -> warning; [] -> warning;

View File

@ -129,7 +129,7 @@ assert_upgraded1(Map) ->
?assert(maps:is_key(<<"ssl">>, Map)). ?assert(maps:is_key(<<"ssl">>, Map)).
check(Conf) when is_map(Conf) -> check(Conf) when is_map(Conf) ->
hocon_tconf:check_plain(emqx_bridge_schema, Conf). hocon_tconf:check_plain(emqx_bridge_schema, Conf, #{required => false}).
%% erlfmt-ignore %% erlfmt-ignore
%% this is config generated from v5.0.11 %% this is config generated from v5.0.11

View File

@ -687,11 +687,12 @@ fields("rpc") ->
desc => ?DESC(rpc_mode) desc => ?DESC(rpc_mode)
} }
)}, )},
{"driver", {"protocol",
sc( sc(
hoconsc:enum([tcp, ssl]), hoconsc:enum([tcp, ssl]),
#{ #{
mapping => "gen_rpc.driver", mapping => "gen_rpc.driver",
aliases => [driver],
default => tcp, default => tcp,
desc => ?DESC(rpc_driver) desc => ?DESC(rpc_driver)
} }
@ -866,19 +867,22 @@ fields("rpc") ->
]; ];
fields("log") -> fields("log") ->
[ [
{"console_handler", {"console",
sc(?R_REF("console_handler"), #{
aliases => [console_handler],
importance => ?IMPORTANCE_HIGH
})},
{"file",
sc( sc(
?R_REF("console_handler"), ?UNION([
#{importance => ?IMPORTANCE_HIGH} ?R_REF("log_file_handler"),
)}, ?MAP(handler_name, ?R_REF("log_file_handler"))
{"file_handlers", ]),
sc(
map(name, ?R_REF("log_file_handler")),
#{ #{
desc => ?DESC("log_file_handlers"), desc => ?DESC("log_file_handlers"),
%% because file_handlers is a map converter => fun ensure_file_handlers/2,
%% so there has to be a default value in order to populate the raw configs default => #{<<"level">> => <<"warning">>},
default => #{<<"default">> => #{<<"level">> => <<"warning">>}}, aliases => [file_handlers],
importance => ?IMPORTANCE_HIGH importance => ?IMPORTANCE_HIGH
} }
)} )}
@ -887,50 +891,40 @@ fields("console_handler") ->
log_handler_common_confs(console); log_handler_common_confs(console);
fields("log_file_handler") -> fields("log_file_handler") ->
[ [
{"file", {"to",
sc( sc(
file(), file(),
#{ #{
desc => ?DESC("log_file_handler_file"), desc => ?DESC("log_file_handler_file"),
default => <<"${EMQX_LOG_DIR}/emqx.log">>, default => <<"${EMQX_LOG_DIR}/emqx.log">>,
converter => fun emqx_schema:naive_env_interpolation/1, converter => fun emqx_schema:naive_env_interpolation/1,
validator => fun validate_file_location/1 validator => fun validate_file_location/1,
aliases => [file],
importance => ?IMPORTANCE_HIGH
} }
)}, )},
{"rotation", {"rotation_count",
sc( sc(
?R_REF("log_rotation"), range(1, 128),
#{} #{
aliases => [rotation],
default => 10,
converter => fun convert_rotation/2,
desc => ?DESC("log_rotation_count"),
importance => ?IMPORTANCE_MEDIUM
}
)}, )},
{"max_size", {"rotation_size",
sc( sc(
hoconsc:union([infinity, emqx_schema:bytesize()]), hoconsc:union([infinity, emqx_schema:bytesize()]),
#{ #{
default => <<"50MB">>, default => <<"50MB">>,
desc => ?DESC("log_file_handler_max_size"), desc => ?DESC("log_file_handler_max_size"),
aliases => [max_size],
importance => ?IMPORTANCE_MEDIUM importance => ?IMPORTANCE_MEDIUM
} }
)} )}
] ++ log_handler_common_confs(file); ] ++ log_handler_common_confs(file);
fields("log_rotation") ->
[
{"enable",
sc(
boolean(),
#{
default => true,
desc => ?DESC("log_rotation_enable")
}
)},
{"count",
sc(
range(1, 2048),
#{
default => 10,
desc => ?DESC("log_rotation_count")
}
)}
];
fields("log_overload_kill") -> fields("log_overload_kill") ->
[ [
{"enable", {"enable",
@ -1038,8 +1032,8 @@ translation("ekka") ->
[{"cluster_discovery", fun tr_cluster_discovery/1}]; [{"cluster_discovery", fun tr_cluster_discovery/1}];
translation("kernel") -> translation("kernel") ->
[ [
{"logger_level", fun tr_logger_level/1}, {"logger_level", fun emqx_config_logger:tr_level/1},
{"logger", fun tr_logger_handlers/1}, {"logger", fun emqx_config_logger:tr_handlers/1},
{"error_logger", fun(_) -> silent end} {"error_logger", fun(_) -> silent end}
]; ];
translation("emqx") -> translation("emqx") ->
@ -1113,24 +1107,9 @@ tr_cluster_discovery(Conf) ->
Strategy = conf_get("cluster.discovery_strategy", Conf), Strategy = conf_get("cluster.discovery_strategy", Conf),
{Strategy, filter(cluster_options(Strategy, Conf))}. {Strategy, filter(cluster_options(Strategy, Conf))}.
-spec tr_logger_level(hocon:config()) -> logger:level().
tr_logger_level(Conf) ->
emqx_config_logger:tr_level(Conf).
tr_logger_handlers(Conf) ->
emqx_config_logger:tr_handlers(Conf).
log_handler_common_confs(Handler) -> log_handler_common_confs(Handler) ->
lists:map(
fun
({_Name, #{importance := _}} = F) -> F;
({Name, Sc}) -> {Name, Sc#{importance => ?IMPORTANCE_LOW}}
end,
do_log_handler_common_confs(Handler)
).
do_log_handler_common_confs(Handler) ->
%% we rarely support dynamic defaults like this %% we rarely support dynamic defaults like this
%% for this one, we have build-time defualut the same as runtime default %% for this one, we have build-time default the same as runtime default
%% so it's less tricky %% so it's less tricky
EnableValues = EnableValues =
case Handler of case Handler of
@ -1140,21 +1119,31 @@ do_log_handler_common_confs(Handler) ->
EnvValue = os:getenv("EMQX_DEFAULT_LOG_HANDLER"), EnvValue = os:getenv("EMQX_DEFAULT_LOG_HANDLER"),
Enable = lists:member(EnvValue, EnableValues), Enable = lists:member(EnvValue, EnableValues),
[ [
{"level",
sc(
log_level(),
#{
default => warning,
desc => ?DESC("common_handler_level"),
importance => ?IMPORTANCE_HIGH
}
)},
{"enable", {"enable",
sc( sc(
boolean(), boolean(),
#{ #{
default => Enable, default => Enable,
desc => ?DESC("common_handler_enable"), desc => ?DESC("common_handler_enable"),
importance => ?IMPORTANCE_LOW importance => ?IMPORTANCE_MEDIUM
} }
)}, )},
{"level", {"formatter",
sc( sc(
log_level(), hoconsc:enum([text, json]),
#{ #{
default => warning, default => text,
desc => ?DESC("common_handler_level") desc => ?DESC("common_handler_formatter"),
importance => ?IMPORTANCE_MEDIUM
} }
)}, )},
{"time_offset", {"time_offset",
@ -1173,16 +1162,7 @@ do_log_handler_common_confs(Handler) ->
#{ #{
default => unlimited, default => unlimited,
desc => ?DESC("common_handler_chars_limit"), desc => ?DESC("common_handler_chars_limit"),
importance => ?IMPORTANCE_LOW importance => ?IMPORTANCE_HIDDEN
}
)},
{"formatter",
sc(
hoconsc:enum([text, json]),
#{
default => text,
desc => ?DESC("common_handler_formatter"),
importance => ?IMPORTANCE_MEDIUM
} }
)}, )},
{"single_line", {"single_line",
@ -1191,7 +1171,7 @@ do_log_handler_common_confs(Handler) ->
#{ #{
default => true, default => true,
desc => ?DESC("common_handler_single_line"), desc => ?DESC("common_handler_single_line"),
importance => ?IMPORTANCE_LOW importance => ?IMPORTANCE_HIDDEN
} }
)}, )},
{"sync_mode_qlen", {"sync_mode_qlen",
@ -1199,7 +1179,8 @@ do_log_handler_common_confs(Handler) ->
non_neg_integer(), non_neg_integer(),
#{ #{
default => 100, default => 100,
desc => ?DESC("common_handler_sync_mode_qlen") desc => ?DESC("common_handler_sync_mode_qlen"),
importance => ?IMPORTANCE_HIDDEN
} }
)}, )},
{"drop_mode_qlen", {"drop_mode_qlen",
@ -1207,7 +1188,8 @@ do_log_handler_common_confs(Handler) ->
pos_integer(), pos_integer(),
#{ #{
default => 3000, default => 3000,
desc => ?DESC("common_handler_drop_mode_qlen") desc => ?DESC("common_handler_drop_mode_qlen"),
importance => ?IMPORTANCE_HIDDEN
} }
)}, )},
{"flush_qlen", {"flush_qlen",
@ -1215,17 +1197,19 @@ do_log_handler_common_confs(Handler) ->
pos_integer(), pos_integer(),
#{ #{
default => 8000, default => 8000,
desc => ?DESC("common_handler_flush_qlen") desc => ?DESC("common_handler_flush_qlen"),
importance => ?IMPORTANCE_HIDDEN
} }
)}, )},
{"overload_kill", sc(?R_REF("log_overload_kill"), #{})}, {"overload_kill", sc(?R_REF("log_overload_kill"), #{importance => ?IMPORTANCE_HIDDEN})},
{"burst_limit", sc(?R_REF("log_burst_limit"), #{})}, {"burst_limit", sc(?R_REF("log_burst_limit"), #{importance => ?IMPORTANCE_HIDDEN})},
{"supervisor_reports", {"supervisor_reports",
sc( sc(
hoconsc:enum([error, progress]), hoconsc:enum([error, progress]),
#{ #{
default => error, default => error,
desc => ?DESC("common_handler_supervisor_reports") desc => ?DESC("common_handler_supervisor_reports"),
importance => ?IMPORTANCE_HIDDEN
} }
)}, )},
{"max_depth", {"max_depth",
@ -1233,7 +1217,8 @@ do_log_handler_common_confs(Handler) ->
hoconsc:union([unlimited, non_neg_integer()]), hoconsc:union([unlimited, non_neg_integer()]),
#{ #{
default => 100, default => 100,
desc => ?DESC("common_handler_max_depth") desc => ?DESC("common_handler_max_depth"),
importance => ?IMPORTANCE_HIDDEN
} }
)} )}
]. ].
@ -1355,3 +1340,19 @@ validator_string_re(Val, RE, Error) ->
node_array() -> node_array() ->
hoconsc:union([emqx_schema:comma_separated_atoms(), hoconsc:array(atom())]). hoconsc:union([emqx_schema:comma_separated_atoms(), hoconsc:array(atom())]).
ensure_file_handlers(Conf, _Opts) ->
FileFields = lists:flatmap(
fun({F, Schema}) ->
Alias = [atom_to_binary(A) || A <- maps:get(aliases, Schema, [])],
[list_to_binary(F) | Alias]
end,
fields("log_file_handler")
),
HandlersWithoutName = maps:with(FileFields, Conf),
HandlersWithName = maps:without(FileFields, Conf),
emqx_utils_maps:deep_merge(#{<<"default">> => HandlersWithoutName}, HandlersWithName).
convert_rotation(undefined, _Opts) -> undefined;
convert_rotation(#{} = Rotation, _Opts) -> maps:get(<<"count">>, Rotation, 10);
convert_rotation(Count, _Opts) when is_integer(Count) -> Count.

View File

@ -47,6 +47,198 @@ array_nodes_test() ->
), ),
ok. ok.
%% erlfmt-ignore
-define(OUTDATED_LOG_CONF,
"""
log.console_handler {
burst_limit {
enable = true
max_count = 10000
window_time = 1000
}
chars_limit = unlimited
drop_mode_qlen = 3000
enable = true
flush_qlen = 8000
formatter = text
level = warning
max_depth = 100
overload_kill {
enable = true
mem_size = 31457280
qlen = 20000
restart_after = 5000
}
single_line = true
supervisor_reports = error
sync_mode_qlen = 100
time_offset = \"+02:00\"
}
log.file_handlers {
default {
burst_limit {
enable = true
max_count = 10000
window_time = 1000
}
chars_limit = unlimited
drop_mode_qlen = 3000
enable = true
file = \"log/my-emqx.log\"
flush_qlen = 8000
formatter = text
level = debug
max_depth = 100
max_size = \"1024MB\"
overload_kill {
enable = true
mem_size = 31457280
qlen = 20000
restart_after = 5000
}
rotation {count = 20, enable = true}
single_line = true
supervisor_reports = error
sync_mode_qlen = 100
time_offset = \"+01:00\"
}
}
"""
).
-define(FORMATTER(TimeOffset),
{emqx_logger_textfmt, #{
chars_limit => unlimited,
depth => 100,
single_line => true,
template => [time, " [", level, "] ", msg, "\n"],
time_offset => TimeOffset
}}
).
-define(FILTERS, [{drop_progress_reports, {fun logger_filters:progress/2, stop}}]).
-define(LOG_CONFIG, #{
burst_limit_enable => true,
burst_limit_max_count => 10000,
burst_limit_window_time => 1000,
drop_mode_qlen => 3000,
flush_qlen => 8000,
overload_kill_enable => true,
overload_kill_mem_size => 31457280,
overload_kill_qlen => 20000,
overload_kill_restart_after => 5000,
sync_mode_qlen => 100
}).
outdated_log_test() ->
validate_log(?OUTDATED_LOG_CONF).
validate_log(Conf) ->
BaseConf = to_bin(?BASE_CONF, ["emqx1@127.0.0.1", "emqx1@127.0.0.1"]),
Conf0 = <<BaseConf/binary, (list_to_binary(Conf))/binary>>,
{ok, ConfMap0} = hocon:binary(Conf0, #{format => richmap}),
ConfList = hocon_tconf:generate(emqx_conf_schema, ConfMap0),
Kernel = proplists:get_value(kernel, ConfList),
?assertEqual(silent, proplists:get_value(error_logger, Kernel)),
?assertEqual(debug, proplists:get_value(logger_level, Kernel)),
Loggers = proplists:get_value(logger, Kernel),
FileHandler = lists:keyfind(logger_disk_log_h, 3, Loggers),
?assertEqual(
{handler, default, logger_disk_log_h, #{
config => ?LOG_CONFIG#{
type => wrap,
file => "log/my-emqx.log",
max_no_bytes => 1073741824,
max_no_files => 20
},
filesync_repeat_interval => no_repeat,
filters => ?FILTERS,
formatter => ?FORMATTER("+01:00"),
level => debug
}},
FileHandler
),
ConsoleHandler = lists:keyfind(logger_std_h, 3, Loggers),
?assertEqual(
{handler, console, logger_std_h, #{
config => ?LOG_CONFIG#{type => standard_io},
filters => ?FILTERS,
formatter => ?FORMATTER("+02:00"),
level => warning
}},
ConsoleHandler
).
%% erlfmt-ignore
-define(KERNEL_LOG_CONF,
"""
log.console {
enable = true
formatter = text
level = warning
time_offset = \"+02:00\"
}
log.file {
enable = false
file = \"log/xx-emqx.log\"
formatter = text
level = debug
rotation_count = 20
rotation_size = \"1024MB\"
time_offset = \"+01:00\"
}
log.file_handlers.default {
enable = true
file = \"log/my-emqx.log\"
}
"""
).
log_test() ->
validate_log(?KERNEL_LOG_CONF).
%% erlfmt-ignore
log_rotation_count_limit_test() ->
Format =
"""
log.file {
enable = true
to = \"log/emqx.log\"
formatter = text
level = debug
rotation = {count = ~w}
rotation_size = \"1024MB\"
}
""",
BaseConf = to_bin(?BASE_CONF, ["emqx1@127.0.0.1", "emqx1@127.0.0.1"]),
lists:foreach(fun({Conf, Count}) ->
Conf0 = <<BaseConf/binary, Conf/binary>>,
{ok, ConfMap0} = hocon:binary(Conf0, #{format => richmap}),
ConfList = hocon_tconf:generate(emqx_conf_schema, ConfMap0),
Kernel = proplists:get_value(kernel, ConfList),
Loggers = proplists:get_value(logger, Kernel),
?assertMatch(
{handler, default, logger_disk_log_h, #{
config := #{max_no_files := Count}
}},
lists:keyfind(logger_disk_log_h, 3, Loggers)
)
end,
[{to_bin(Format, [1]), 1}, {to_bin(Format, [128]), 128}]),
lists:foreach(fun({Conf, Count}) ->
Conf0 = <<BaseConf/binary, Conf/binary>>,
{ok, ConfMap0} = hocon:binary(Conf0, #{format => richmap}),
?assertThrow({emqx_conf_schema,
[#{kind := validation_error,
mismatches := #{"handler_name" :=
#{kind := validation_error,
path := "log.file.default.rotation_count",
reason := #{expected_type := "1..128"},
value := Count}
}}]},
hocon_tconf:generate(emqx_conf_schema, ConfMap0))
end, [{to_bin(Format, [0]), 0}, {to_bin(Format, [129]), 129}]).
%% erlfmt-ignore %% erlfmt-ignore
-define(BASE_AUTHN_ARRAY, -define(BASE_AUTHN_ARRAY,
""" """
@ -84,38 +276,50 @@ authn_validations_test() ->
OKHttps = to_bin(?BASE_AUTHN_ARRAY, [post, true, <<"https://127.0.0.1:8080">>]), OKHttps = to_bin(?BASE_AUTHN_ARRAY, [post, true, <<"https://127.0.0.1:8080">>]),
Conf0 = <<BaseConf/binary, OKHttps/binary>>, Conf0 = <<BaseConf/binary, OKHttps/binary>>,
{ok, ConfMap0} = hocon:binary(Conf0, #{format => richmap}), {ok, ConfMap0} = hocon:binary(Conf0, #{format => richmap}),
?assert(is_list(hocon_tconf:generate(emqx_conf_schema, ConfMap0))), {_, Res0} = hocon_tconf:map_translate(emqx_conf_schema, ConfMap0, #{format => richmap}),
Headers0 = authentication_headers(Res0),
?assertEqual(<<"application/json">>, maps:get(<<"content-type">>, Headers0)),
%% accept from converter
?assertEqual(<<"application/json">>, maps:get(<<"accept">>, Headers0)),
OKHttp = to_bin(?BASE_AUTHN_ARRAY, [post, false, <<"http://127.0.0.1:8080">>]), OKHttp = to_bin(?BASE_AUTHN_ARRAY, [post, false, <<"http://127.0.0.1:8080">>]),
Conf1 = <<BaseConf/binary, OKHttp/binary>>, Conf1 = <<BaseConf/binary, OKHttp/binary>>,
{ok, ConfMap1} = hocon:binary(Conf1, #{format => richmap}), {ok, ConfMap1} = hocon:binary(Conf1, #{format => richmap}),
?assert(is_list(hocon_tconf:generate(emqx_conf_schema, ConfMap1))), {_, Res1} = hocon_tconf:map_translate(emqx_conf_schema, ConfMap1, #{format => richmap}),
Headers1 = authentication_headers(Res1),
?assertEqual(<<"application/json">>, maps:get(<<"content-type">>, Headers1), Headers1),
?assertEqual(<<"application/json">>, maps:get(<<"accept">>, Headers1), Headers1),
DisableSSLWithHttps = to_bin(?BASE_AUTHN_ARRAY, [post, false, <<"https://127.0.0.1:8080">>]), DisableSSLWithHttps = to_bin(?BASE_AUTHN_ARRAY, [post, false, <<"https://127.0.0.1:8080">>]),
Conf2 = <<BaseConf/binary, DisableSSLWithHttps/binary>>, Conf2 = <<BaseConf/binary, DisableSSLWithHttps/binary>>,
{ok, ConfMap2} = hocon:binary(Conf2, #{format => richmap}), {ok, ConfMap2} = hocon:binary(Conf2, #{format => richmap}),
?assertThrow( ?assertThrow(
?ERROR(check_http_ssl_opts), ?ERROR(check_http_ssl_opts),
hocon_tconf:generate(emqx_conf_schema, ConfMap2) hocon_tconf:map_translate(emqx_conf_schema, ConfMap2, #{format => richmap})
), ),
BadHeader = to_bin(?BASE_AUTHN_ARRAY, [get, true, <<"https://127.0.0.1:8080">>]), BadHeader = to_bin(?BASE_AUTHN_ARRAY, [get, true, <<"https://127.0.0.1:8080">>]),
Conf3 = <<BaseConf/binary, BadHeader/binary>>, Conf3 = <<BaseConf/binary, BadHeader/binary>>,
{ok, ConfMap3} = hocon:binary(Conf3, #{format => richmap}), {ok, ConfMap3} = hocon:binary(Conf3, #{format => richmap}),
?assertThrow( {_, Res3} = hocon_tconf:map_translate(emqx_conf_schema, ConfMap3, #{format => richmap}),
?ERROR(check_http_headers), Headers3 = authentication_headers(Res3),
hocon_tconf:generate(emqx_conf_schema, ConfMap3) %% remove the content-type header when get method
), ?assertEqual(false, maps:is_key(<<"content-type">>, Headers3), Headers3),
?assertEqual(<<"application/json">>, maps:get(<<"accept">>, Headers3), Headers3),
BadHeaderWithTuple = binary:replace(BadHeader, [<<"[">>, <<"]">>], <<"">>, [global]), BadHeaderWithTuple = binary:replace(BadHeader, [<<"[">>, <<"]">>], <<"">>, [global]),
Conf4 = <<BaseConf/binary, BadHeaderWithTuple/binary>>, Conf4 = <<BaseConf/binary, BadHeaderWithTuple/binary>>,
{ok, ConfMap4} = hocon:binary(Conf4, #{format => richmap}), {ok, ConfMap4} = hocon:binary(Conf4, #{format => richmap}),
?assertThrow( {_, Res4} = hocon_tconf:map_translate(emqx_conf_schema, ConfMap4, #{}),
?ERROR(check_http_headers), Headers4 = authentication_headers(Res4),
hocon_tconf:generate(emqx_conf_schema, ConfMap4) ?assertEqual(false, maps:is_key(<<"content-type">>, Headers4), Headers4),
), ?assertEqual(<<"application/json">>, maps:get(<<"accept">>, Headers4), Headers4),
ok. ok.
authentication_headers(Conf) ->
[#{<<"headers">> := Headers}] = hocon_maps:get("authentication", Conf),
Headers.
doc_gen_test() -> doc_gen_test() ->
%% the json file too large to encode. %% the json file too large to encode.
{ {

View File

@ -151,7 +151,7 @@ log_path() ->
Configs = logger:get_handler_config(), Configs = logger:get_handler_config(),
case get_log_path(Configs) of case get_log_path(Configs) of
undefined -> undefined ->
<<"log.file_handler.default.enable is false, not logging to file.">>; <<"log.file.enable is false, not logging to file.">>;
Path -> Path ->
iolist_to_binary(filename:join(RootDir, Path)) iolist_to_binary(filename:join(RootDir, Path))
end. end.

View File

@ -201,8 +201,6 @@ config(put, #{body := NewConf}, Req) ->
case emqx_conf:update(Path, NewConf, ?OPTS) of case emqx_conf:update(Path, NewConf, ?OPTS) of
{ok, #{raw_config := RawConf}} -> {ok, #{raw_config := RawConf}} ->
{200, RawConf}; {200, RawConf};
{error, {permission_denied, Reason}} ->
{403, #{code => 'UPDATE_FAILED', message => Reason}};
{error, Reason} -> {error, Reason} ->
{400, #{code => 'UPDATE_FAILED', message => ?ERR_MSG(Reason)}} {400, #{code => 'UPDATE_FAILED', message => ?ERR_MSG(Reason)}}
end. end.
@ -247,8 +245,6 @@ config_reset(post, _Params, Req) ->
case emqx_conf:reset(Path, ?OPTS) of case emqx_conf:reset(Path, ?OPTS) of
{ok, _} -> {ok, _} ->
{200}; {200};
{error, {permission_denied, Reason}} ->
{403, #{code => 'REST_FAILED', message => Reason}};
{error, no_default_value} -> {error, no_default_value} ->
{400, #{code => 'NO_DEFAULT_VALUE', message => <<"No Default Value.">>}}; {400, #{code => 'NO_DEFAULT_VALUE', message => <<"No Default Value.">>}};
{error, Reason} -> {error, Reason} ->

View File

@ -99,24 +99,24 @@ t_log(_Config) ->
{ok, Log} = get_config("log"), {ok, Log} = get_config("log"),
File = "log/emqx-test.log", File = "log/emqx-test.log",
%% update handler %% update handler
Log1 = emqx_utils_maps:deep_put([<<"file_handlers">>, <<"default">>, <<"enable">>], Log, true), Log1 = emqx_utils_maps:deep_put([<<"file">>, <<"default">>, <<"enable">>], Log, true),
Log2 = emqx_utils_maps:deep_put([<<"file_handlers">>, <<"default">>, <<"file">>], Log1, File), Log2 = emqx_utils_maps:deep_put([<<"file">>, <<"default">>, <<"to">>], Log1, File),
{ok, #{}} = update_config(<<"log">>, Log2), {ok, #{}} = update_config(<<"log">>, Log2),
{ok, Log3} = logger:get_handler_config(default), {ok, Log3} = logger:get_handler_config(default),
?assertMatch(#{config := #{file := File}}, Log3), ?assertMatch(#{config := #{file := File}}, Log3),
ErrLog1 = emqx_utils_maps:deep_put([<<"file_handlers">>, <<"default">>, <<"enable">>], Log, 1), ErrLog1 = emqx_utils_maps:deep_put([<<"file">>, <<"default">>, <<"enable">>], Log, 1),
?assertMatch({error, {"HTTP/1.1", 400, _}}, update_config(<<"log">>, ErrLog1)), ?assertMatch({error, {"HTTP/1.1", 400, _}}, update_config(<<"log">>, ErrLog1)),
ErrLog2 = emqx_utils_maps:deep_put( ErrLog2 = emqx_utils_maps:deep_put(
[<<"file_handlers">>, <<"default">>, <<"enabfe">>], Log, true [<<"file">>, <<"default">>, <<"enabfe">>], Log, true
), ),
?assertMatch({error, {"HTTP/1.1", 400, _}}, update_config(<<"log">>, ErrLog2)), ?assertMatch({error, {"HTTP/1.1", 400, _}}, update_config(<<"log">>, ErrLog2)),
%% add new handler %% add new handler
File1 = "log/emqx-test1.log", File1 = "log/emqx-test1.log",
Handler = emqx_utils_maps:deep_get([<<"file_handlers">>, <<"default">>], Log2), Handler = emqx_utils_maps:deep_get([<<"file">>, <<"default">>], Log2),
NewLog1 = emqx_utils_maps:deep_put([<<"file_handlers">>, <<"new">>], Log2, Handler), NewLog1 = emqx_utils_maps:deep_put([<<"file">>, <<"new">>], Log2, Handler),
NewLog2 = emqx_utils_maps:deep_put( NewLog2 = emqx_utils_maps:deep_put(
[<<"file_handlers">>, <<"new">>, <<"file">>], NewLog1, File1 [<<"file">>, <<"new">>, <<"to">>], NewLog1, File1
), ),
{ok, #{}} = update_config(<<"log">>, NewLog2), {ok, #{}} = update_config(<<"log">>, NewLog2),
{ok, Log4} = logger:get_handler_config(new), {ok, Log4} = logger:get_handler_config(new),
@ -124,7 +124,7 @@ t_log(_Config) ->
%% disable new handler %% disable new handler
Disable = emqx_utils_maps:deep_put( Disable = emqx_utils_maps:deep_put(
[<<"file_handlers">>, <<"new">>, <<"enable">>], NewLog2, false [<<"file">>, <<"new">>, <<"enable">>], NewLog2, false
), ),
{ok, #{}} = update_config(<<"log">>, Disable), {ok, #{}} = update_config(<<"log">>, Disable),
?assertEqual({error, {not_found, new}}, logger:get_handler_config(new)), ?assertEqual({error, {not_found, new}}, logger:get_handler_config(new)),

View File

@ -34,8 +34,8 @@ init_per_testcase(t_log_path, Config) ->
emqx_config_logger:add_handler(), emqx_config_logger:add_handler(),
Log = emqx_conf:get_raw([log], #{}), Log = emqx_conf:get_raw([log], #{}),
File = "log/emqx-test.log", File = "log/emqx-test.log",
Log1 = emqx_utils_maps:deep_put([<<"file_handlers">>, <<"default">>, <<"enable">>], Log, true), Log1 = emqx_utils_maps:deep_put([<<"file">>, <<"default">>, <<"enable">>], Log, true),
Log2 = emqx_utils_maps:deep_put([<<"file_handlers">>, <<"default">>, <<"file">>], Log1, File), Log2 = emqx_utils_maps:deep_put([<<"file">>, <<"default">>, <<"to">>], Log1, File),
{ok, #{}} = emqx_conf:update([log], Log2, #{rawconf_with_defaults => true}), {ok, #{}} = emqx_conf:update([log], Log2, #{rawconf_with_defaults => true}),
Config; Config;
init_per_testcase(_, Config) -> init_per_testcase(_, Config) ->
@ -43,7 +43,7 @@ init_per_testcase(_, Config) ->
end_per_testcase(t_log_path, Config) -> end_per_testcase(t_log_path, Config) ->
Log = emqx_conf:get_raw([log], #{}), Log = emqx_conf:get_raw([log], #{}),
Log1 = emqx_utils_maps:deep_put([<<"file_handlers">>, <<"default">>, <<"enable">>], Log, false), Log1 = emqx_utils_maps:deep_put([<<"file">>, <<"default">>, <<"enable">>], Log, false),
{ok, #{}} = emqx_conf:update([log], Log1, #{rawconf_with_defaults => true}), {ok, #{}} = emqx_conf:update([log], Log1, #{rawconf_with_defaults => true}),
emqx_config_logger:remove_handler(), emqx_config_logger:remove_handler(),
Config; Config;

View File

@ -875,16 +875,16 @@ tr_log_to_env() {
unset EMQX_LOG__TO unset EMQX_LOG__TO
case "${log_to}" in case "${log_to}" in
console) console)
export EMQX_LOG__CONSOLE_HANDLER__ENABLE='true' export EMQX_LOG__CONSOLE__ENABLE='true'
export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE='false' export EMQX_LOG__FILE__ENABLE='false'
;; ;;
file) file)
export EMQX_LOG__CONSOLE_HANDLER__ENABLE='false' export EMQX_LOG__CONSOLE__ENABLE='false'
export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE='true' export EMQX_LOG__FILE__ENABLE='true'
;; ;;
both) both)
export EMQX_LOG__CONSOLE_HANDLER__ENABLE='true' export EMQX_LOG__CONSOLE__ENABLE='true'
export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE='true' export EMQX_LOG__FILE__ENABLE='true'
;; ;;
default) default)
# want to use config file defaults, do nothing # want to use config file defaults, do nothing

View File

@ -72,7 +72,7 @@ defmodule EMQXUmbrella.MixProject do
# in conflict by emqtt and hocon # in conflict by emqtt and hocon
{:getopt, "1.0.2", override: true}, {:getopt, "1.0.2", override: true},
{:snabbkaffe, github: "kafka4beam/snabbkaffe", tag: "1.0.8", override: true}, {:snabbkaffe, github: "kafka4beam/snabbkaffe", tag: "1.0.8", override: true},
{:hocon, github: "emqx/hocon", tag: "0.39.4", override: true}, {:hocon, github: "emqx/hocon", tag: "0.39.6", override: true},
{:emqx_http_lib, github: "emqx/emqx_http_lib", tag: "0.5.2", override: true}, {:emqx_http_lib, github: "emqx/emqx_http_lib", tag: "0.5.2", override: true},
{:esasl, github: "emqx/esasl", tag: "0.2.0"}, {:esasl, github: "emqx/esasl", tag: "0.2.0"},
{:jose, github: "potatosalad/erlang-jose", tag: "1.11.2"}, {:jose, github: "potatosalad/erlang-jose", tag: "1.11.2"},

View File

@ -75,7 +75,7 @@
, {system_monitor, {git, "https://github.com/ieQu1/system_monitor", {tag, "3.0.3"}}} , {system_monitor, {git, "https://github.com/ieQu1/system_monitor", {tag, "3.0.3"}}}
, {getopt, "1.0.2"} , {getopt, "1.0.2"}
, {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "1.0.8"}}} , {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "1.0.8"}}}
, {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.39.4"}}} , {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.39.6"}}}
, {emqx_http_lib, {git, "https://github.com/emqx/emqx_http_lib.git", {tag, "0.5.2"}}} , {emqx_http_lib, {git, "https://github.com/emqx/emqx_http_lib.git", {tag, "0.5.2"}}}
, {esasl, {git, "https://github.com/emqx/esasl", {tag, "0.2.0"}}} , {esasl, {git, "https://github.com/emqx/esasl", {tag, "0.2.0"}}}
, {jose, {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.11.2"}}} , {jose, {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.11.2"}}}