From 83fe003b05e8a8fee4e27fbdb916f7983952ba37 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 17 Mar 2025 17:29:14 +0800 Subject: [PATCH] fix logger --- apps/iot/src/endpoint/iot_zd_endpoint.erl | 13 ++++++------- config/sys-dev.config | 19 +++++++++++++++++++ config/sys-prod.config | 18 ++++++++++++++++++ rebar.config | 5 ++++- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/apps/iot/src/endpoint/iot_zd_endpoint.erl b/apps/iot/src/endpoint/iot_zd_endpoint.erl index 23806ba..ecff2bd 100644 --- a/apps/iot/src/endpoint/iot_zd_endpoint.erl +++ b/apps/iot/src/endpoint/iot_zd_endpoint.erl @@ -25,11 +25,12 @@ %% 最大数据缓冲量 -define(MAX_QUEUE_SIZE, 5_000_000). +%% 处理日志信息 +-define(log(Msg), north_data:info(Msg)). + -record(state, { mqtt_opts = [], postman_pid :: undefined | pid(), - logger_pid :: pid(), - %% 数据缓存队列 iot_queue, @@ -79,10 +80,8 @@ init([]) -> erlang:process_flag(trap_exit, true), %% 创建转发器, 避免阻塞当前进程的创建,因此采用了延时初始化的机制 erlang:start_timer(0, self(), create_postman), - %% 启动日志记录器 - {ok, LoggerPid} = iot_logger:start_link("north_data"), - {ok, disconnected, #state{mqtt_opts = Opts, iot_queue = iot_queue:new(?MAX_QUEUE_SIZE), postman_pid = undefined, logger_pid = LoggerPid}}. + {ok, disconnected, #state{mqtt_opts = Opts, iot_queue = iot_queue:new(?MAX_QUEUE_SIZE), postman_pid = undefined}}. %% @private %% @doc This function is called by a gen_statem when it needs to find out @@ -133,9 +132,9 @@ handle_event(info, fetch_next, connected, State = #state{postman_pid = PostmanPi end; %% 收到确认消息 -handle_event(info, {ack, AssocMessage}, StateName, State = #state{timer_ref = TimerRef, logger_pid = LoggerPid}) -> +handle_event(info, {ack, AssocMessage}, StateName, State = #state{timer_ref = TimerRef}) -> %% 记录日志信息 - iot_logger:write(LoggerPid, AssocMessage), + ?log(iolist_to_binary(AssocMessage)), Actions = case StateName =:= connected of true -> [{next_event, info, fetch_next}]; diff --git a/config/sys-dev.config b/config/sys-dev.config index d76c4b8..2e8851f 100644 --- a/config/sys-dev.config +++ b/config/sys-dev.config @@ -158,7 +158,26 @@ {lager_file_backend, [{file, "notice.log"}, {level, notice}, {size, 314572800}]}, {lager_file_backend, [{file, "error.log"}, {level, error}, {size, 314572800}]}, {lager_file_backend, [{file, "info.log"}, {level, info}, {size, 314572800}]} + ]}, + + %% 自定义接收器 + {extra_sinks, [ + {north_data_lager_event, [ + {handlers, [ + {lager_file_backend, [ + {file, "north_data.log"}, + {level, info}, + {date, "$D0"}, + {count, 15}, + {formatter, lager_default_formatter}, + {formatter_config, ["[", date, " ", time, "] ", message, "\n"]} + ]} + ]}, + {async_threshold, 500}, + {async_threshold_window, 50} + ]} ]} + ]} ]. diff --git a/config/sys-prod.config b/config/sys-prod.config index c5fdb6c..7f89b44 100644 --- a/config/sys-prod.config +++ b/config/sys-prod.config @@ -146,6 +146,24 @@ {lager_file_backend, [{file, "notice.log"}, {level, notice}, {size, 314572800}]}, {lager_file_backend, [{file, "error.log"}, {level, error}, {size, 314572800}]}, {lager_file_backend, [{file, "info.log"}, {level, info}, {size, 314572800}]} + ]}, + + %% 自定义接收器 + {extra_sinks, [ + {north_data_lager_event, [ + {handlers, [ + {lager_file_backend, [ + {file, "north_data.log"}, + {level, info}, + {date, "$D0"}, + {count, 10}, + {formatter, lager_default_formatter}, + {formatter_config, ["[", date, " ", time, "] ", message, "\n"]} + ]} + ]}, + {async_threshold, 500}, + {async_threshold_window, 50} + ]} ]} ]} diff --git a/rebar.config b/rebar.config index 8ac5bcc..54e9cc6 100644 --- a/rebar.config +++ b/rebar.config @@ -43,7 +43,10 @@ ] }]}]}. -{erl_opts, [{parse_transform,lager_transform}]}. +{erl_opts, [ + {parse_transform,lager_transform}, + {lager_extra_sinks, [north_data]} +]}. {rebar_packages_cdn, "https://hexpm.upyun.com"}.