fix logger

This commit is contained in:
anlicheng 2025-03-17 17:29:14 +08:00
parent e5773800e3
commit 83fe003b05
4 changed files with 47 additions and 8 deletions

View File

@ -25,11 +25,12 @@
%% %%
-define(MAX_QUEUE_SIZE, 5_000_000). -define(MAX_QUEUE_SIZE, 5_000_000).
%%
-define(log(Msg), north_data:info(Msg)).
-record(state, { -record(state, {
mqtt_opts = [], mqtt_opts = [],
postman_pid :: undefined | pid(), postman_pid :: undefined | pid(),
logger_pid :: pid(),
%% %%
iot_queue, iot_queue,
@ -79,10 +80,8 @@ init([]) ->
erlang:process_flag(trap_exit, true), erlang:process_flag(trap_exit, true),
%% , %% ,
erlang:start_timer(0, self(), create_postman), 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 %% @private
%% @doc This function is called by a gen_statem when it needs to find out %% @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; 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 Actions = case StateName =:= connected of
true -> [{next_event, info, fetch_next}]; true -> [{next_event, info, fetch_next}];

View File

@ -158,7 +158,26 @@
{lager_file_backend, [{file, "notice.log"}, {level, notice}, {size, 314572800}]}, {lager_file_backend, [{file, "notice.log"}, {level, notice}, {size, 314572800}]},
{lager_file_backend, [{file, "error.log"}, {level, error}, {size, 314572800}]}, {lager_file_backend, [{file, "error.log"}, {level, error}, {size, 314572800}]},
{lager_file_backend, [{file, "info.log"}, {level, info}, {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}
]} ]}
]} ]}
]}
]. ].

View File

@ -146,6 +146,24 @@
{lager_file_backend, [{file, "notice.log"}, {level, notice}, {size, 314572800}]}, {lager_file_backend, [{file, "notice.log"}, {level, notice}, {size, 314572800}]},
{lager_file_backend, [{file, "error.log"}, {level, error}, {size, 314572800}]}, {lager_file_backend, [{file, "error.log"}, {level, error}, {size, 314572800}]},
{lager_file_backend, [{file, "info.log"}, {level, info}, {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}
]}
]} ]}
]} ]}

View File

@ -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"}. {rebar_packages_cdn, "https://hexpm.upyun.com"}.