fix(log): respect payload encoding settings when formatting packets
This commit is contained in:
parent
51530588ef
commit
74c346f9d1
|
@ -88,7 +88,7 @@
|
||||||
?_DO_TRACE(Tag, Msg, Meta),
|
?_DO_TRACE(Tag, Msg, Meta),
|
||||||
?SLOG(
|
?SLOG(
|
||||||
Level,
|
Level,
|
||||||
(emqx_trace_formatter:format_meta_map(Meta))#{msg => Msg, tag => Tag},
|
(Meta)#{msg => Msg, tag => Tag},
|
||||||
#{is_trace => false}
|
#{is_trace => false}
|
||||||
)
|
)
|
||||||
end).
|
end).
|
||||||
|
|
|
@ -105,7 +105,7 @@ format(Msg, Meta, Config) ->
|
||||||
maybe_format_msg(undefined, _Meta, _Config) ->
|
maybe_format_msg(undefined, _Meta, _Config) ->
|
||||||
#{};
|
#{};
|
||||||
maybe_format_msg({report, Report0} = Msg, #{report_cb := Cb} = Meta, Config) ->
|
maybe_format_msg({report, Report0} = Msg, #{report_cb := Cb} = Meta, Config) ->
|
||||||
Report = emqx_logger_textfmt:try_encode_payload(Report0, Config),
|
Report = emqx_logger_textfmt:try_encode_meta(Report0, Config),
|
||||||
case is_map(Report) andalso Cb =:= ?DEFAULT_FORMATTER of
|
case is_map(Report) andalso Cb =:= ?DEFAULT_FORMATTER of
|
||||||
true ->
|
true ->
|
||||||
%% reporting a map without a customised format function
|
%% reporting a map without a customised format function
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
-export([format/2]).
|
-export([format/2]).
|
||||||
-export([check_config/1]).
|
-export([check_config/1]).
|
||||||
-export([try_format_unicode/1, try_encode_payload/2]).
|
-export([try_format_unicode/1, try_encode_meta/2]).
|
||||||
%% Used in the other log formatters
|
%% Used in the other log formatters
|
||||||
-export([evaluate_lazy_values_if_dbg_level/1, evaluate_lazy_values/1]).
|
-export([evaluate_lazy_values_if_dbg_level/1, evaluate_lazy_values/1]).
|
||||||
|
|
||||||
|
@ -105,7 +105,7 @@ is_list_report_acceptable(_) ->
|
||||||
enrich_report(ReportRaw0, Meta, Config) ->
|
enrich_report(ReportRaw0, Meta, Config) ->
|
||||||
%% clientid and peername always in emqx_conn's process metadata.
|
%% clientid and peername always in emqx_conn's process metadata.
|
||||||
%% topic and username can be put in meta using ?SLOG/3, or put in msg's report by ?SLOG/2
|
%% topic and username can be put in meta using ?SLOG/3, or put in msg's report by ?SLOG/2
|
||||||
ReportRaw = try_encode_payload(ReportRaw0, Config),
|
ReportRaw = try_encode_meta(ReportRaw0, Config),
|
||||||
Topic =
|
Topic =
|
||||||
case maps:get(topic, Meta, undefined) of
|
case maps:get(topic, Meta, undefined) of
|
||||||
undefined -> maps:get(topic, ReportRaw, undefined);
|
undefined -> maps:get(topic, ReportRaw, undefined);
|
||||||
|
@ -172,9 +172,22 @@ enrich_topic({Fmt, Args}, #{topic := Topic}) when is_list(Fmt) ->
|
||||||
enrich_topic(Msg, _) ->
|
enrich_topic(Msg, _) ->
|
||||||
Msg.
|
Msg.
|
||||||
|
|
||||||
try_encode_payload(#{payload := Payload} = Report, #{payload_encode := Encode}) ->
|
try_encode_meta(Report, Config) ->
|
||||||
|
lists:foldl(
|
||||||
|
fun(Meta, Acc) ->
|
||||||
|
try_encode_meta(Meta, Acc, Config)
|
||||||
|
end,
|
||||||
|
Report,
|
||||||
|
[payload, packet]
|
||||||
|
).
|
||||||
|
|
||||||
|
try_encode_meta(payload, #{payload := Payload} = Report, #{payload_encode := Encode}) ->
|
||||||
Report#{payload := encode_payload(Payload, Encode)};
|
Report#{payload := encode_payload(Payload, Encode)};
|
||||||
try_encode_payload(Report, _Config) ->
|
try_encode_meta(packet, #{packet := Packet} = Report, #{payload_encode := Encode}) when
|
||||||
|
is_tuple(Packet)
|
||||||
|
->
|
||||||
|
Report#{packet := emqx_packet:format(Packet, Encode)};
|
||||||
|
try_encode_meta(_, Report, _Config) ->
|
||||||
Report.
|
Report.
|
||||||
|
|
||||||
encode_payload(Payload, text) ->
|
encode_payload(Payload, text) ->
|
||||||
|
@ -182,4 +195,5 @@ encode_payload(Payload, text) ->
|
||||||
encode_payload(_Payload, hidden) ->
|
encode_payload(_Payload, hidden) ->
|
||||||
"******";
|
"******";
|
||||||
encode_payload(Payload, hex) ->
|
encode_payload(Payload, hex) ->
|
||||||
binary:encode_hex(Payload).
|
Bin = emqx_utils_conv:bin(Payload),
|
||||||
|
binary:encode_hex(Bin).
|
||||||
|
|
|
@ -51,7 +51,6 @@
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
format/1,
|
|
||||||
format/2
|
format/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
@ -481,10 +480,6 @@ will_msg(#mqtt_packet_connect{
|
||||||
headers = #{username => Username, properties => Props}
|
headers = #{username => Username, properties => Props}
|
||||||
}.
|
}.
|
||||||
|
|
||||||
%% @doc Format packet
|
|
||||||
-spec format(emqx_types:packet()) -> iolist().
|
|
||||||
format(Packet) -> format(Packet, emqx_trace_handler:payload_encode()).
|
|
||||||
|
|
||||||
%% @doc Format packet
|
%% @doc Format packet
|
||||||
-spec format(emqx_types:packet(), hex | text | hidden) -> iolist().
|
-spec format(emqx_types:packet(), hex | text | hidden) -> iolist().
|
||||||
format(#mqtt_packet{header = Header, variable = Variable, payload = Payload}, PayloadEncode) ->
|
format(#mqtt_packet{header = Header, variable = Variable, payload = Payload}, PayloadEncode) ->
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
-include("emqx_mqtt.hrl").
|
-include("emqx_mqtt.hrl").
|
||||||
|
|
||||||
-export([format/2]).
|
-export([format/2]).
|
||||||
-export([format_meta_map/1]).
|
|
||||||
|
|
||||||
%% logger_formatter:config/0 is not exported.
|
%% logger_formatter:config/0 is not exported.
|
||||||
-type config() :: map().
|
-type config() :: map().
|
||||||
|
@ -43,10 +42,6 @@ format(
|
||||||
format(Event, Config) ->
|
format(Event, Config) ->
|
||||||
emqx_logger_textfmt:format(Event, Config).
|
emqx_logger_textfmt:format(Event, Config).
|
||||||
|
|
||||||
format_meta_map(Meta) ->
|
|
||||||
Encode = emqx_trace_handler:payload_encode(),
|
|
||||||
format_meta_map(Meta, Encode).
|
|
||||||
|
|
||||||
format_meta_map(Meta, Encode) ->
|
format_meta_map(Meta, Encode) ->
|
||||||
format_meta_map(Meta, Encode, [
|
format_meta_map(Meta, Encode, [
|
||||||
{packet, fun format_packet/2},
|
{packet, fun format_packet/2},
|
||||||
|
|
|
@ -820,7 +820,7 @@ serialize_and_inc_stats_fun(#state{serialize = Serialize}) ->
|
||||||
?LOG(warning, #{
|
?LOG(warning, #{
|
||||||
msg => "packet_discarded",
|
msg => "packet_discarded",
|
||||||
reason => "frame_too_large",
|
reason => "frame_too_large",
|
||||||
packet => emqx_packet:format(Packet)
|
packet => Packet
|
||||||
}),
|
}),
|
||||||
ok = emqx_metrics:inc('delivery.dropped.too_large'),
|
ok = emqx_metrics:inc('delivery.dropped.too_large'),
|
||||||
ok = emqx_metrics:inc('delivery.dropped'),
|
ok = emqx_metrics:inc('delivery.dropped'),
|
||||||
|
|
|
@ -377,42 +377,60 @@ t_will_msg(_) ->
|
||||||
|
|
||||||
t_format(_) ->
|
t_format(_) ->
|
||||||
io:format("~ts", [
|
io:format("~ts", [
|
||||||
emqx_packet:format(#mqtt_packet{
|
emqx_packet:format(
|
||||||
header = #mqtt_packet_header{type = ?CONNACK, retain = true, dup = 0},
|
#mqtt_packet{
|
||||||
variable = undefined
|
header = #mqtt_packet_header{type = ?CONNACK, retain = true, dup = 0},
|
||||||
})
|
variable = undefined
|
||||||
]),
|
},
|
||||||
io:format("~ts", [
|
text
|
||||||
emqx_packet:format(#mqtt_packet{
|
)
|
||||||
header = #mqtt_packet_header{type = ?CONNACK}, variable = 1, payload = <<"payload">>
|
|
||||||
})
|
|
||||||
]),
|
]),
|
||||||
|
io:format(
|
||||||
|
"~ts",
|
||||||
|
[
|
||||||
|
emqx_packet:format(
|
||||||
|
#mqtt_packet{
|
||||||
|
header = #mqtt_packet_header{type = ?CONNACK},
|
||||||
|
variable = 1,
|
||||||
|
payload = <<"payload">>
|
||||||
|
},
|
||||||
|
text
|
||||||
|
)
|
||||||
|
]
|
||||||
|
),
|
||||||
io:format("~ts", [
|
io:format("~ts", [
|
||||||
emqx_packet:format(
|
emqx_packet:format(
|
||||||
?CONNECT_PACKET(#mqtt_packet_connect{
|
?CONNECT_PACKET(
|
||||||
will_flag = true,
|
#mqtt_packet_connect{
|
||||||
will_retain = true,
|
will_flag = true,
|
||||||
will_qos = ?QOS_2,
|
will_retain = true,
|
||||||
will_topic = <<"topic">>,
|
will_qos = ?QOS_2,
|
||||||
will_payload = <<"payload">>
|
will_topic = <<"topic">>,
|
||||||
})
|
will_payload = <<"payload">>
|
||||||
|
}
|
||||||
|
),
|
||||||
|
text
|
||||||
)
|
)
|
||||||
]),
|
]),
|
||||||
io:format("~ts", [
|
io:format("~ts", [
|
||||||
emqx_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{password = password}))
|
emqx_packet:format(?CONNECT_PACKET(#mqtt_packet_connect{password = password}), text)
|
||||||
]),
|
]),
|
||||||
io:format("~ts", [emqx_packet:format(?CONNACK_PACKET(?CONNACK_SERVER))]),
|
io:format("~ts", [emqx_packet:format(?CONNACK_PACKET(?CONNACK_SERVER), text)]),
|
||||||
io:format("~ts", [emqx_packet:format(?PUBLISH_PACKET(?QOS_1, 1))]),
|
io:format("~ts", [emqx_packet:format(?PUBLISH_PACKET(?QOS_1, 1), text)]),
|
||||||
io:format("~ts", [emqx_packet:format(?PUBLISH_PACKET(?QOS_2, <<"topic">>, 10, <<"payload">>))]),
|
|
||||||
io:format("~ts", [emqx_packet:format(?PUBACK_PACKET(?PUBACK, 98))]),
|
|
||||||
io:format("~ts", [emqx_packet:format(?PUBREL_PACKET(99))]),
|
|
||||||
io:format("~ts", [
|
io:format("~ts", [
|
||||||
emqx_packet:format(?SUBSCRIBE_PACKET(15, [{<<"topic">>, ?QOS_0}, {<<"topic1">>, ?QOS_1}]))
|
emqx_packet:format(?PUBLISH_PACKET(?QOS_2, <<"topic">>, 10, <<"payload">>), text)
|
||||||
]),
|
]),
|
||||||
io:format("~ts", [emqx_packet:format(?SUBACK_PACKET(40, [?QOS_0, ?QOS_1]))]),
|
io:format("~ts", [emqx_packet:format(?PUBACK_PACKET(?PUBACK, 98), text)]),
|
||||||
io:format("~ts", [emqx_packet:format(?UNSUBSCRIBE_PACKET(89, [<<"t">>, <<"t2">>]))]),
|
io:format("~ts", [emqx_packet:format(?PUBREL_PACKET(99), text)]),
|
||||||
io:format("~ts", [emqx_packet:format(?UNSUBACK_PACKET(90))]),
|
io:format("~ts", [
|
||||||
io:format("~ts", [emqx_packet:format(?DISCONNECT_PACKET(128))]).
|
emqx_packet:format(
|
||||||
|
?SUBSCRIBE_PACKET(15, [{<<"topic">>, ?QOS_0}, {<<"topic1">>, ?QOS_1}]), text
|
||||||
|
)
|
||||||
|
]),
|
||||||
|
io:format("~ts", [emqx_packet:format(?SUBACK_PACKET(40, [?QOS_0, ?QOS_1]), text)]),
|
||||||
|
io:format("~ts", [emqx_packet:format(?UNSUBSCRIBE_PACKET(89, [<<"t">>, <<"t2">>]), text)]),
|
||||||
|
io:format("~ts", [emqx_packet:format(?UNSUBACK_PACKET(90), text)]),
|
||||||
|
io:format("~ts", [emqx_packet:format(?DISCONNECT_PACKET(128), text)]).
|
||||||
|
|
||||||
t_parse_empty_publish(_) ->
|
t_parse_empty_publish(_) ->
|
||||||
%% 52: 0011(type=PUBLISH) 0100 (QoS=2)
|
%% 52: 0011(type=PUBLISH) 0100 (QoS=2)
|
||||||
|
|
Loading…
Reference in New Issue