From 3b8eb413475ece8e347280eb4a0b86949700da30 Mon Sep 17 00:00:00 2001 From: terry-xiaoyu <506895667@qq.com> Date: Thu, 3 Jan 2019 18:23:33 +0800 Subject: [PATCH] Remove the log header macro --- include/logger.hrl | 19 +++---------------- src/emqx_connection.erl | 3 ++- src/emqx_protocol.erl | 4 ++-- src/emqx_session.erl | 37 +++++++++++++++++++------------------ src/emqx_ws_connection.erl | 4 ++-- 5 files changed, 28 insertions(+), 39 deletions(-) diff --git a/include/logger.hrl b/include/logger.hrl index db93d7f56..71f1a3bb6 100644 --- a/include/logger.hrl +++ b/include/logger.hrl @@ -1,23 +1,10 @@ %%-------------------------------------------------------------------- -%% Logs with a header prefixed to the log message. -%% And the log args are puted into report_cb for lazy evaluation. +%% The args are put into report_cb for lazy evaluation. %%-------------------------------------------------------------------- --ifdef(LOG_HEADER). -%% with header --define(LOG(Level, Format, Args), - begin - (logger:log(Level,#{},#{report_cb => - fun(_) -> - {?LOG_HEADER ++ " "++ (Format), (Args)} - end})) - end). --else. -%% without header --define(LOG(Level, Format, Args), +-define(LOG_LZ(Level, Format, Args), begin (logger:log(Level,#{},#{report_cb => fun(_) -> {(Format), (Args)} end})) - end). --endif. \ No newline at end of file + end). \ No newline at end of file diff --git a/src/emqx_connection.erl b/src/emqx_connection.erl index 7eb68e329..47162b009 100644 --- a/src/emqx_connection.erl +++ b/src/emqx_connection.erl @@ -19,7 +19,6 @@ -include("emqx.hrl"). -include("emqx_mqtt.hrl"). --define(LOG_HEADER, "[MQTT]"). -include("logger.hrl"). -export([start_link/3]). @@ -53,6 +52,8 @@ -define(DEFAULT_ACTIVE_N, 100). -define(SOCK_STATS, [recv_oct, recv_cnt, send_oct, send_cnt, send_pend]). +-define(LOG(Level, Format, Args), ?LOG_LZ(Level, "[MQTT] " ++ Format, Args)). + start_link(Transport, Socket, Options) -> {ok, proc_lib:spawn_link(?MODULE, init, [[Transport, Socket, Options]])}. diff --git a/src/emqx_protocol.erl b/src/emqx_protocol.erl index 13eff95bf..f76d2a322 100644 --- a/src/emqx_protocol.erl +++ b/src/emqx_protocol.erl @@ -14,8 +14,6 @@ -module(emqx_protocol). --define(LOG_HEADER, "[MQTT]"). - -include("emqx.hrl"). -include("emqx_mqtt.hrl"). -include("logger.hrl"). @@ -79,6 +77,8 @@ -define(NO_PROPS, undefined). +-define(LOG(Level, Format, Args), ?LOG_LZ(Level, "[MQTT] " ++ Format, Args)). + %%------------------------------------------------------------------------------ %% Init %%------------------------------------------------------------------------------ diff --git a/src/emqx_session.erl b/src/emqx_session.erl index ed33df3ce..8375a6e02 100644 --- a/src/emqx_session.erl +++ b/src/emqx_session.erl @@ -43,6 +43,8 @@ -include("emqx.hrl"). -include("emqx_mqtt.hrl"). +-include("logger.hrl"). + -export([start_link/1]). -export([info/1, attrs/1]). -export([stats/1]). @@ -155,8 +157,7 @@ -export_type([attr/0]). --define(LOG(Level, Format, Args, _State), - emqx_logger:Level("[Session] " ++ Format, Args)). +-define(LOG(Level, Format, Args), ?LOG_LZ(Level, "[Session] " ++ Format, Args)). %% @doc Start a session proc. -spec(start_link(SessAttrs :: map()) -> {ok, pid()}). @@ -390,11 +391,11 @@ handle_call(stats, _From, State) -> reply(stats(State), State); handle_call({discard, ByPid}, _From, State = #state{conn_pid = undefined}) -> - ?LOG(warning, "Discarded by ~p", [ByPid], State), + ?LOG(warning, "Discarded by ~p", [ByPid]), {stop, {shutdown, discarded}, ok, State}; handle_call({discard, ByPid}, _From, State = #state{client_id = ClientId, conn_pid = ConnPid}) -> - ?LOG(warning, "Conn ~p is discarded by ~p", [ConnPid, ByPid], State), + ?LOG(warning, "Conn ~p is discarded by ~p", [ConnPid, ByPid]), ConnPid ! {shutdown, discard, {ClientId, ByPid}}, {stop, {shutdown, discarded}, ok, State}; @@ -413,7 +414,7 @@ handle_call({register_publish_packet_id, PacketId, Ts}, _From, {ok, ensure_stats_timer(ensure_await_rel_timer(State1))} end; true -> - ?LOG(warning, "Dropped qos2 packet ~w for too many awaiting_rel", [PacketId], State), + ?LOG(warning, "Dropped qos2 packet ~w for too many awaiting_rel", [PacketId]), emqx_metrics:trans(inc, 'messages/qos2/dropped'), {{error, ?RC_RECEIVE_MAXIMUM_EXCEEDED}, State} end); @@ -425,7 +426,7 @@ handle_call({pubrec, PacketId, _ReasonCode}, _From, State = #state{inflight = In true -> {ok, ensure_stats_timer(acked(pubrec, PacketId, State))}; false -> - ?LOG(warning, "The PUBREC PacketId ~w is not found.", [PacketId], State), + ?LOG(warning, "The PUBREC PacketId ~w is not found.", [PacketId]), emqx_metrics:trans(inc, 'packets/pubrec/missed'), {{error, ?RC_PACKET_IDENTIFIER_NOT_FOUND}, State} end); @@ -437,7 +438,7 @@ handle_call({pubrel, PacketId, _ReasonCode}, _From, State = #state{awaiting_rel {_Ts, AwaitingRel1} -> {ok, ensure_stats_timer(State#state{awaiting_rel = AwaitingRel1})}; error -> - ?LOG(warning, "The PUBREL PacketId ~w is not found", [PacketId], State), + ?LOG(warning, "The PUBREL PacketId ~w is not found", [PacketId]), emqx_metrics:trans(inc, 'packets/pubrel/missed'), {{error, ?RC_PACKET_IDENTIFIER_NOT_FOUND}, State} end); @@ -496,7 +497,7 @@ handle_cast({puback, PacketId, _ReasonCode}, State = #state{inflight = Inflight} true -> ensure_stats_timer(dequeue(acked(puback, PacketId, State))); false -> - ?LOG(warning, "The PUBACK PacketId ~w is not found", [PacketId], State), + ?LOG(warning, "The PUBACK PacketId ~w is not found", [PacketId]), emqx_metrics:trans(inc, 'packets/puback/missed'), State end); @@ -508,7 +509,7 @@ handle_cast({pubcomp, PacketId, _ReasonCode}, State = #state{inflight = Inflight true -> ensure_stats_timer(dequeue(acked(pubcomp, PacketId, State))); false -> - ?LOG(warning, "The PUBCOMP PacketId ~w is not found", [PacketId], State), + ?LOG(warning, "The PUBCOMP PacketId ~w is not found", [PacketId]), emqx_metrics:trans(inc, 'packets/pubcomp/missed'), State end); @@ -526,14 +527,14 @@ handle_cast({resume, #{conn_pid := ConnPid, expiry_timer = ExpireTimer, will_delay_timer = WillDelayTimer}) -> - ?LOG(info, "Resumed by connection ~p ", [ConnPid], State), + ?LOG(info, "Resumed by connection ~p ", [ConnPid]), %% Cancel Timers lists:foreach(fun emqx_misc:cancel_timer/1, [RetryTimer, AwaitTimer, ExpireTimer, WillDelayTimer]), case kick(ClientId, OldConnPid, ConnPid) of - ok -> ?LOG(warning, "Connection ~p kickout ~p", [ConnPid, OldConnPid], State); + ok -> ?LOG(warning, "Connection ~p kickout ~p", [ConnPid, OldConnPid]); ignore -> ok end, @@ -614,12 +615,12 @@ handle_info({timeout, Timer, emit_stats}, GcState1 = emqx_gc:reset(GcState), {noreply, NewState#state{gc_state = GcState1}, hibernate}; {shutdown, Reason} -> - ?LOG(warning, "shutdown due to ~p", [Reason], NewState), + ?LOG(warning, "shutdown due to ~p", [Reason]), shutdown(Reason, NewState) end; handle_info({timeout, Timer, expired}, State = #state{expiry_timer = Timer}) -> - ?LOG(info, "expired, shutdown now.", [], State), + ?LOG(info, "expired, shutdown now.", []), shutdown(expired, State); handle_info({timeout, Timer, will_delay}, State = #state{will_msg = WillMsg, will_delay_timer = Timer}) -> @@ -645,7 +646,7 @@ handle_info({'EXIT', OldPid, _Reason}, State = #state{old_conn_pid = OldPid}) -> handle_info({'EXIT', Pid, Reason}, State = #state{conn_pid = ConnPid}) -> ?LOG(error, "Unexpected EXIT: conn_pid=~p, exit_pid=~p, reason=~p", - [ConnPid, Pid, Reason], State), + [ConnPid, Pid, Reason]), {noreply, State}; handle_info(Info, State) -> @@ -780,7 +781,7 @@ expire_awaiting_rel([{PacketId, Ts} | More], Now, case (timer:now_diff(Now, Ts) div 1000) of Age when Age >= Timeout -> emqx_metrics:trans(inc, 'messages/qos2/expired'), - ?LOG(warning, "Dropped qos2 packet ~s for await_rel_timeout", [PacketId], State), + ?LOG(warning, "Dropped qos2 packet ~s for await_rel_timeout", [PacketId]), expire_awaiting_rel(More, Now, State#state{awaiting_rel = maps:remove(PacketId, AwaitingRel)}); Age -> ensure_await_rel_timer(Timeout - max(0, Age), State) @@ -895,7 +896,7 @@ acked(puback, PacketId, State = #state{client_id = ClientId, username = Username emqx_hooks:run('message.acked', [#{client_id => ClientId, username => Username}], Msg), State#state{inflight = emqx_inflight:delete(PacketId, Inflight)}; none -> - ?LOG(warning, "Duplicated PUBACK PacketId ~w", [PacketId], State), + ?LOG(warning, "Duplicated PUBACK PacketId ~w", [PacketId]), State end; @@ -905,10 +906,10 @@ acked(pubrec, PacketId, State = #state{client_id = ClientId, username = Username emqx_hooks:run('message.acked', [#{client_id => ClientId, username => Username}], Msg), State#state{inflight = emqx_inflight:update(PacketId, {pubrel, PacketId, os:timestamp()}, Inflight)}; {value, {pubrel, PacketId, _Ts}} -> - ?LOG(warning, "Duplicated PUBREC PacketId ~w", [PacketId], State), + ?LOG(warning, "Duplicated PUBREC PacketId ~w", [PacketId]), State; none -> - ?LOG(warning, "Unexpected PUBREC PacketId ~w", [PacketId], State), + ?LOG(warning, "Unexpected PUBREC PacketId ~w", [PacketId]), State end; diff --git a/src/emqx_ws_connection.erl b/src/emqx_ws_connection.erl index 74ef32c0a..df23f50ca 100644 --- a/src/emqx_ws_connection.erl +++ b/src/emqx_ws_connection.erl @@ -14,8 +14,6 @@ -module(emqx_ws_connection). --define(LOG_HEADER, "[WS]"). - -include("emqx.hrl"). -include("emqx_mqtt.hrl"). -include("logger.hrl"). @@ -48,6 +46,8 @@ -define(SOCK_STATS, [recv_oct, recv_cnt, send_oct, send_cnt]). +-define(LOG(Level, Format, Args), ?LOG_LZ(Level, "[MQTT-WS] " ++ Format, Args)). + %%------------------------------------------------------------------------------ %% API %%------------------------------------------------------------------------------