替换日志框架
This commit is contained in:
parent
f7687129b9
commit
336362c4e4
@ -8,6 +8,7 @@
|
|||||||
sync,
|
sync,
|
||||||
emqtt,
|
emqtt,
|
||||||
jiffy,
|
jiffy,
|
||||||
|
logger,
|
||||||
kernel,
|
kernel,
|
||||||
stdlib
|
stdlib
|
||||||
]},
|
]},
|
||||||
|
|||||||
@ -69,13 +69,13 @@ init([]) ->
|
|||||||
{ok, Metrics} = efka_client:request_metric(),
|
{ok, Metrics} = efka_client:request_metric(),
|
||||||
try convert_metric(Metrics) of
|
try convert_metric(Metrics) of
|
||||||
{ok, MetricMap} ->
|
{ok, MetricMap} ->
|
||||||
lager:debug("[aircon_args] init load metric_map: ~p", [MetricMap]),
|
logger:debug("[aircon_args] init load metric_map: ~p", [MetricMap]),
|
||||||
{ok, Param} = efka_client:request_param(),
|
{ok, Param} = efka_client:request_param(),
|
||||||
|
|
||||||
{ok, #state{metrics = MetricMap, param = Param}}
|
{ok, #state{metrics = MetricMap, param = Param}}
|
||||||
catch
|
catch
|
||||||
_:Error:Stack->
|
_:Error:Stack->
|
||||||
lager:warning("[aircon_args] request_metric get error: ~p, stack: ~p", [Error, Stack]),
|
logger:warning("[aircon_args] request_metric get error: ~p, stack: ~p", [Error, Stack]),
|
||||||
{ok, #state{metrics = #{}, param = #{}}}
|
{ok, #state{metrics = #{}, param = #{}}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ handle_cast({push_param, Param}, State = #state{}) ->
|
|||||||
handle_cast({push_metric, Metrics}, State = #state{}) ->
|
handle_cast({push_metric, Metrics}, State = #state{}) ->
|
||||||
try convert_metric(Metrics) of
|
try convert_metric(Metrics) of
|
||||||
{ok, MetricMap} ->
|
{ok, MetricMap} ->
|
||||||
lager:debug("[aircon_args] push metric_map: ~p", [MetricMap]),
|
logger:debug("[aircon_args] push metric_map: ~p", [MetricMap]),
|
||||||
{noreply, State#state{metrics = MetricMap}}
|
{noreply, State#state{metrics = MetricMap}}
|
||||||
catch _:_ ->
|
catch _:_ ->
|
||||||
{noreply, State}
|
{noreply, State}
|
||||||
|
|||||||
@ -111,9 +111,9 @@ handle_cast({metric_data, Message}, State = #state{device_uuid = DeviceUUID, dat
|
|||||||
|
|
||||||
{noreply, State#state{data_counter = DataCounter + 1, status = 1}};
|
{noreply, State#state{data_counter = DataCounter + 1, status = 1}};
|
||||||
M when is_map(M) ->
|
M when is_map(M) ->
|
||||||
lager:notice("[power_device] invalid map: ~p", [M]);
|
logger:notice("[power_device] invalid map: ~p", [M]);
|
||||||
Error ->
|
Error ->
|
||||||
lager:notice("[power_device] jiffy decode error: ~p", [Error]),
|
logger:notice("[power_device] jiffy decode error: ~p", [Error]),
|
||||||
{noreply, State}
|
{noreply, State}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|||||||
@ -51,23 +51,23 @@ start_link() ->
|
|||||||
init([]) ->
|
init([]) ->
|
||||||
%% 建立到emqx服务器的连接
|
%% 建立到emqx服务器的连接
|
||||||
Opts = emqx_opts(<<"aircon-data-subscriber">>),
|
Opts = emqx_opts(<<"aircon-data-subscriber">>),
|
||||||
lager:debug("[opts] is: ~p", [Opts]),
|
logger:debug("[opts] is: ~p", [Opts]),
|
||||||
case emqtt:start_link(Opts) of
|
case emqtt:start_link(Opts) of
|
||||||
{ok, ConnPid} ->
|
{ok, ConnPid} ->
|
||||||
lager:debug("[mqtt_subscriber] start conntecting, pid: ~p", [ConnPid]),
|
logger:debug("[mqtt_subscriber] start conntecting, pid: ~p", [ConnPid]),
|
||||||
{ok, _} = emqtt:connect(ConnPid),
|
{ok, _} = emqtt:connect(ConnPid),
|
||||||
lager:debug("[mqtt_subscriber] connect success"),
|
logger:debug("[mqtt_subscriber] connect success"),
|
||||||
|
|
||||||
%% 监听和设备的全部事件
|
%% 监听和设备的全部事件
|
||||||
SubscribeResult = emqtt:subscribe(ConnPid, ?Topics),
|
SubscribeResult = emqtt:subscribe(ConnPid, ?Topics),
|
||||||
lager:debug("[mqtt_subscriber] subscribe topics: ~p, result is: ~p", [?Topics, SubscribeResult]),
|
logger:debug("[mqtt_subscriber] subscribe topics: ~p, result is: ~p", [?Topics, SubscribeResult]),
|
||||||
|
|
||||||
{ok, #state{conn_pid = ConnPid}};
|
{ok, #state{conn_pid = ConnPid}};
|
||||||
ignore ->
|
ignore ->
|
||||||
lager:debug("[mqtt_subscriber] connect emqx get ignore"),
|
logger:debug("[mqtt_subscriber] connect emqx get ignore"),
|
||||||
{stop, ignore};
|
{stop, ignore};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
lager:debug("[mqtt_subscriber] connect emqx get error: ~p", [Reason]),
|
logger:debug("[mqtt_subscriber] connect emqx get error: ~p", [Reason]),
|
||||||
{stop, Reason}
|
{stop, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -100,11 +100,11 @@ handle_cast(_Request, State = #state{}) ->
|
|||||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||||
{stop, Reason :: term(), NewState :: #state{}}).
|
{stop, Reason :: term(), NewState :: #state{}}).
|
||||||
handle_info({disconnect, ReasonCode, Properties}, State = #state{}) ->
|
handle_info({disconnect, ReasonCode, Properties}, State = #state{}) ->
|
||||||
lager:debug("[mqtt_subscriber] Recv a DISONNECT packet - ReasonCode: ~p, Properties: ~p", [ReasonCode, Properties]),
|
logger:debug("[mqtt_subscriber] Recv a DISONNECT packet - ReasonCode: ~p, Properties: ~p", [ReasonCode, Properties]),
|
||||||
{stop, disconnected, State};
|
{stop, disconnected, State};
|
||||||
%% 必须要做到消息的快速分发,数据的json反序列需要在host进程进行
|
%% 必须要做到消息的快速分发,数据的json反序列需要在host进程进行
|
||||||
handle_info({publish, #{packet_id := _PacketId, payload := Payload, qos := Qos, topic := Topic}}, State = #state{conn_pid = _ConnPid}) ->
|
handle_info({publish, #{packet_id := _PacketId, payload := Payload, qos := Qos, topic := Topic}}, State = #state{conn_pid = _ConnPid}) ->
|
||||||
lager:debug("[mqtt_subscriber] Recv a topic: ~p, publish packet: ~p, qos: ~p", [Topic, Payload, Qos]),
|
logger:debug("[mqtt_subscriber] Recv a topic: ~p, publish packet: ~p, qos: ~p", [Topic, Payload, Qos]),
|
||||||
%% 将消息分发到对应的publisher进程去处理, 提高消息的处理速度; topic格式: /aircon/20525456021829/data
|
%% 将消息分发到对应的publisher进程去处理, 提高消息的处理速度; topic格式: /aircon/20525456021829/data
|
||||||
case Topic of
|
case Topic of
|
||||||
<<"/aircon/", DeviceInfo/binary>> ->
|
<<"/aircon/", DeviceInfo/binary>> ->
|
||||||
@ -119,11 +119,11 @@ handle_info({publish, #{packet_id := _PacketId, payload := Payload, qos := Qos,
|
|||||||
end,
|
end,
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
handle_info({puback, Packet = #{packet_id := _PacketId}}, State = #state{}) ->
|
handle_info({puback, Packet = #{packet_id := _PacketId}}, State = #state{}) ->
|
||||||
lager:debug("[mqtt_subscriber] receive puback packet: ~p", [Packet]),
|
logger:debug("[mqtt_subscriber] receive puback packet: ~p", [Packet]),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_info(Info, State = #state{}) ->
|
handle_info(Info, State = #state{}) ->
|
||||||
lager:debug("[mqtt_subscriber] get info: ~p", [Info]),
|
logger:debug("[mqtt_subscriber] get info: ~p", [Info]),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
@ -139,10 +139,10 @@ terminate(Reason, _State = #state{conn_pid = ConnPid}) when is_pid(ConnPid) ->
|
|||||||
{ok, _Props, _ReasonCode} = emqtt:unsubscribe(ConnPid, #{}, TopicNames),
|
{ok, _Props, _ReasonCode} = emqtt:unsubscribe(ConnPid, #{}, TopicNames),
|
||||||
|
|
||||||
ok = emqtt:disconnect(ConnPid),
|
ok = emqtt:disconnect(ConnPid),
|
||||||
lager:debug("[iot_mqtt_subscriber] terminate with reason: ~p", [Reason]),
|
logger:debug("[iot_mqtt_subscriber] terminate with reason: ~p", [Reason]),
|
||||||
ok;
|
ok;
|
||||||
terminate(Reason, _State) ->
|
terminate(Reason, _State) ->
|
||||||
lager:debug("[iot_mqtt_subscriber] terminate with reason: ~p", [Reason]),
|
logger:debug("[iot_mqtt_subscriber] terminate with reason: ~p", [Reason]),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
@ -186,14 +186,14 @@ emqx_opts(ClientSuffix) when is_binary(ClientSuffix) ->
|
|||||||
|
|
||||||
-spec dispatch(DeviceMac :: binary(), Message :: binary()) -> no_return().
|
-spec dispatch(DeviceMac :: binary(), Message :: binary()) -> no_return().
|
||||||
dispatch(DeviceMac, Message) when is_binary(DeviceMac), is_binary(Message) ->
|
dispatch(DeviceMac, Message) when is_binary(DeviceMac), is_binary(Message) ->
|
||||||
case power_gateway_args:get_device_uuid(DeviceMac) of
|
case aircon_args:get_device_uuid(DeviceMac) of
|
||||||
error ->
|
error ->
|
||||||
lager:notice("[mqtt_subscriber] device_mac: ~p, device_uuid not found", [DeviceMac]);
|
logger:notice("[mqtt_subscriber] device_mac: ~p, device_uuid not found", [DeviceMac]);
|
||||||
{ok, DeviceUUID} ->
|
{ok, DeviceUUID} ->
|
||||||
case power_device_sup:ensure_device_started(DeviceUUID) of
|
case aircon_device_sup:ensure_device_started(DeviceUUID) of
|
||||||
{ok, DevicePid} ->
|
{ok, DevicePid} ->
|
||||||
power_device:metric_data(DevicePid, Message);
|
aircon_device:metric_data(DevicePid, Message);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
lager:notice("[mqtt_subscriber] start device get error: ~p", [Reason])
|
logger:notice("[mqtt_subscriber] start device get error: ~p", [Reason])
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
@ -90,11 +90,11 @@ read_service_name() ->
|
|||||||
%% 获取配置的名称
|
%% 获取配置的名称
|
||||||
RootDir = code:root_dir(),
|
RootDir = code:root_dir(),
|
||||||
VersionFile = RootDir ++ "/.version",
|
VersionFile = RootDir ++ "/.version",
|
||||||
lager:debug("[aircon_sup] version path is: ~p", [VersionFile]),
|
logger:debug("[aircon_sup] version path is: ~p", [VersionFile]),
|
||||||
case file:read_file(VersionFile) of
|
case file:read_file(VersionFile) of
|
||||||
{ok, RegisterName0} ->
|
{ok, RegisterName0} ->
|
||||||
string:trim(RegisterName0);
|
string:trim(RegisterName0);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
lager:warning("[aircon_sup] read .version file get error: ~p", [Reason]),
|
logger:warning("[aircon_sup] read .version file get error: ~p", [Reason]),
|
||||||
<<"aircon">>
|
<<"aircon">>
|
||||||
end.
|
end.
|
||||||
@ -162,7 +162,7 @@ do_register(RegisterName, Socket) ->
|
|||||||
receive
|
receive
|
||||||
{tcp, Socket, Data} ->
|
{tcp, Socket, Data} ->
|
||||||
RegisterPacket = unpack(Data),
|
RegisterPacket = unpack(Data),
|
||||||
lager:debug("[efka_client] get register reply packet: ~p", [RegisterPacket]),
|
logger:debug("[efka_client] get register reply packet: ~p", [RegisterPacket]),
|
||||||
case RegisterPacket of
|
case RegisterPacket of
|
||||||
#efka_packet{packet_id = PacketId, type = ?PACKET_TYPE_RESPONSE, message = #{<<"c">> := 1, <<"r">> := <<"ok">>}} ->
|
#efka_packet{packet_id = PacketId, type = ?PACKET_TYPE_RESPONSE, message = #{<<"c">> := 1, <<"r">> := <<"ok">>}} ->
|
||||||
ok;
|
ok;
|
||||||
@ -389,7 +389,7 @@ unpack(<<PacketId:16, Type:8, Body/binary>>) ->
|
|||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
|
|
||||||
handle_poll_command(#{<<"device_uuid">> := DeviceUUID, <<"command">> := <<"query_status">>}) when is_binary(DeviceUUID) ->
|
handle_poll_command(#{<<"device_uuid">> := DeviceUUID, <<"command">> := <<"query_status">>}) when is_binary(DeviceUUID) ->
|
||||||
case power_device:get_pid(DeviceUUID) of
|
case aircon_device:get_pid(DeviceUUID) of
|
||||||
undefined ->
|
undefined ->
|
||||||
#{
|
#{
|
||||||
<<"c">> => 1,
|
<<"c">> => 1,
|
||||||
@ -403,7 +403,7 @@ handle_poll_command(#{<<"device_uuid">> := DeviceUUID, <<"command">> := <<"query
|
|||||||
0 => <<"离线"/utf8>>,
|
0 => <<"离线"/utf8>>,
|
||||||
1 => <<"在线"/utf8>>
|
1 => <<"在线"/utf8>>
|
||||||
},
|
},
|
||||||
{ok, Status} = power_device:poll_status(Pid),
|
{ok, Status} = aircon_device:poll_status(Pid),
|
||||||
#{
|
#{
|
||||||
<<"c">> => 1,
|
<<"c">> => 1,
|
||||||
<<"r">> => #{
|
<<"r">> => #{
|
||||||
@ -415,12 +415,12 @@ handle_poll_command(#{<<"device_uuid">> := DeviceUUID, <<"command">> := <<"query
|
|||||||
|
|
||||||
-spec handle_param(Params :: map()) -> ok | {error, Reason :: binary()}.
|
-spec handle_param(Params :: map()) -> ok | {error, Reason :: binary()}.
|
||||||
handle_param(Params) when is_map(Params) ->
|
handle_param(Params) when is_map(Params) ->
|
||||||
power_gateway_args:push_param(Params),
|
aircon_args:push_param(Params),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
-spec handle_metric(Metric :: list()) -> ok | {error, Reason :: binary()}.
|
-spec handle_metric(Metric :: list()) -> ok | {error, Reason :: binary()}.
|
||||||
handle_metric(Metric) when is_list(Metric) ->
|
handle_metric(Metric) when is_list(Metric) ->
|
||||||
power_gateway_args:push_metric(Metric),
|
aircon_args:push_metric(Metric),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
-spec handle_stream_call(ServiceName :: binary(), Fields :: list(), Tag :: map()) ->
|
-spec handle_stream_call(ServiceName :: binary(), Fields :: list(), Tag :: map()) ->
|
||||||
|
|||||||
@ -17,5 +17,21 @@
|
|||||||
{host, "39.98.184.67"},
|
{host, "39.98.184.67"},
|
||||||
{port, 3361}
|
{port, 3361}
|
||||||
]}
|
]}
|
||||||
|
]},
|
||||||
|
|
||||||
|
{logger, [
|
||||||
|
{level, debug},
|
||||||
|
{metadata, [device_id, request_id]},
|
||||||
|
{handlers, [
|
||||||
|
{default, logger_std_h, #{level => info,
|
||||||
|
formatter => {logger_formatter, #{template => "$time [$level] $message $metadata\n"}}}},
|
||||||
|
|
||||||
|
{disk, logger_disk_log_h, #{level => debug,
|
||||||
|
file => "log/debug.log",
|
||||||
|
size => 10485760,
|
||||||
|
max_no_files => 5,
|
||||||
|
formatter => {logger_formatter, #{template => "$time [$level] $message $metadata\n"}}}}
|
||||||
|
]}
|
||||||
]}
|
]}
|
||||||
|
|
||||||
].
|
].
|
||||||
@ -17,6 +17,21 @@
|
|||||||
{host, "39.98.184.67"},
|
{host, "39.98.184.67"},
|
||||||
{port, 3361}
|
{port, 3361}
|
||||||
]}
|
]}
|
||||||
|
]},
|
||||||
|
|
||||||
|
{logger, [
|
||||||
|
{level, debug},
|
||||||
|
{metadata, [device_id, request_id]},
|
||||||
|
{handlers, [
|
||||||
|
{default, logger_std_h, #{level => info,
|
||||||
|
formatter => {logger_formatter, #{template => "$time [$level] $message $metadata\n"}}}},
|
||||||
|
|
||||||
|
{disk, logger_disk_log_h, #{level => debug,
|
||||||
|
file => "log/debug.log",
|
||||||
|
size => 10485760,
|
||||||
|
max_no_files => 5,
|
||||||
|
formatter => {logger_formatter, #{template => "$time [$level] $message $metadata\n"}}}}
|
||||||
|
]}
|
||||||
]}
|
]}
|
||||||
|
|
||||||
].
|
].
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user