This commit is contained in:
anlicheng 2026-04-18 16:07:07 +08:00
parent f2847053b7
commit d3decb00b5
16 changed files with 47 additions and 8 deletions

View File

@ -85,6 +85,10 @@
{handler, default, logger_std_h,
#{
level => debug,
filter_default => stop,
filters => [
{iot_only, {fun logger_filters:domain/2, {log, sub, [iot]}}}
],
formatter => {logger_formatter, #{template => [time, " [", level, "] ", msg, "\n"]}}
}
},
@ -92,6 +96,10 @@
{handler, disk, logger_disk_log_h,
#{
level => debug,
filter_default => stop,
filters => [
{iot_only, {fun logger_filters:domain/2, {log, sub, [iot]}}}
],
config => #{
file => "log/debug.log",
max_no_files => 10,

View File

@ -55,6 +55,10 @@
{handler, default, logger_std_h,
#{
level => debug,
filter_default => stop,
filters => [
{iot_only, {fun logger_filters:domain/2, {log, sub, [iot]}}}
],
formatter => {logger_formatter, #{template => [time, " [", level, "] ", msg, "\n"]}}
}
},
@ -62,6 +66,10 @@
{handler, disk, logger_disk_log_h,
#{
level => debug,
filter_default => stop,
filters => [
{iot_only, {fun logger_filters:domain/2, {log, sub, [iot]}}}
],
config => #{
file => "log/debug.log",
max_no_files => 10,

View File

@ -47,7 +47,7 @@
%% exist but can be set manually, which
%% is required if the names aren't exactly
%% sys.config and vm.args
{sys_config, "./config/sys.config"},
{sys_config, "./config/sys-dev.config"},
{vm_args, "./config/vm.args"}
%% the .src form of the configuration files do
@ -60,7 +60,8 @@
[%% prod is the default mode when prod
%% profile is used, so does not have
%% to be explicitly included like this
{mode, prod}
{mode, prod},
{sys_config, "./config/sys-prod.config"}
%% use minimal mode to exclude ERTS
%% {mode, minimal}

View File

@ -101,6 +101,7 @@ start_link(Opts) when is_list(Opts) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([InfluxProps]) ->
ok = iot_log:set_metadata(),
Token = proplists:get_value(token, InfluxProps),
Host = proplists:get_value(host, InfluxProps),
Port = proplists:get_value(port, InfluxProps),

View File

@ -45,6 +45,7 @@ start_link(LocalName, Endpoint = #endpoint{config = #http_endpoint{}}) when is_a
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([Endpoint = #endpoint{matcher = Matcher}]) ->
ok = iot_log:set_metadata(),
endpoint_subscription:subscribe(Matcher, self()),
Buffer = endpoint_buffer:new(Endpoint, 10),
{ok, #state{endpoint = Endpoint, buffer = Buffer}}.

View File

@ -51,6 +51,7 @@ start_link(LocalName, Endpoint = #endpoint{}) when is_atom(LocalName) ->
%% gen_statem:start_link/[3,4], this function is called by the new
%% process to initialize.
init([Endpoint = #endpoint{id = Id, matcher = Matcher}]) ->
ok = iot_log:set_metadata(),
endpoint_subscription:subscribe(Matcher, self()),
erlang:process_flag(trap_exit, true),
@ -181,4 +182,4 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
%%%===================================================================
retry_connect() ->
erlang:start_timer(?RETRY_INTERVAL, self(), connect).
erlang:start_timer(?RETRY_INTERVAL, self(), connect).

View File

@ -53,6 +53,7 @@ start_link(LocalName, Endpoint = #endpoint{}) when is_atom(LocalName) ->
%% gen_statem:start_link/[3,4], this function is called by the new
%% process to initialize.
init([Endpoint = #endpoint{matcher = Matcher}]) ->
ok = iot_log:set_metadata(),
% erlang:process_flag(trap_exit, true),
endpoint_subscription:subscribe(Matcher, self()),
@ -200,4 +201,4 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
%%%===================================================================
%%% Internal functions
%%%===================================================================
%%%===================================================================

View File

@ -69,6 +69,7 @@ start_link() ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
ok = iot_log:set_metadata(),
{ok, #state{}}.
%% @private
@ -203,4 +204,4 @@ order_num([<<$*>>|_]) ->
order_num([<<$+>>|_]) ->
3;
order_num([_|Tail]) ->
order_num(Tail).
order_num(Tail).

View File

@ -54,6 +54,7 @@ start_link(RetryInterval) when is_integer(RetryInterval) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([RetryInterval]) ->
ok = iot_log:set_metadata(),
{ok, #state{retry_interval = RetryInterval}}.
%% @private

View File

@ -56,6 +56,7 @@ start_link() ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) ->
ok = iot_log:set_metadata(),
{ok, #state{}}.
%% @private

View File

@ -174,6 +174,7 @@ start_link(Name, UUID) when is_atom(Name), is_binary(UUID) ->
%% gen_statem:start_link/[3,4], this function is called by the new
%% process to initialize.
init([UUID]) ->
ok = iot_log:set_metadata(),
case iot_api_client:get_host_by_uuid(UUID) of
{ok, #{<<"id">> := HostId, <<"authorize_status">> := AuthorizeStatus}} ->
%% host_id注册别名, HostPid

View File

@ -9,6 +9,7 @@
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
ok = iot_log:set_metadata(),
io:setopts([{encoding, unicode}]),
%%
erlang:system_flag(fullsweep_after, 16),
@ -102,4 +103,4 @@ ensure_mnesia_schema() ->
logger:debug("[iot_app] create mnesia schema failed with error: ~p", [Error]),
throw({init_schema, Error})
end
end.
end.

10
src/iot_log.erl Normal file
View File

@ -0,0 +1,10 @@
%%%-------------------------------------------------------------------
%%% @doc Logger helper for project-scoped metadata.
%%%-------------------------------------------------------------------
-module(iot_log).
-export([set_metadata/0]).
-spec set_metadata() -> ok.
set_metadata() ->
logger:set_process_metadata(#{domain => [iot]}).

View File

@ -13,6 +13,7 @@
-export([init/2]).
init(Req0, Opts) ->
ok = iot_log:set_metadata(),
Method = binary_to_list(cowboy_req:method(Req0)),
Path = binary_to_list(cowboy_req:path(Req0)),
GetParams0 = cowboy_req:parse_qs(Req0),
@ -44,4 +45,4 @@ receiver_events(TaskId, Req) ->
{stream_close, TaskId, Reason} ->
CloseFrame = iolist_to_binary([<<"event: close\n">>, <<"data: ", Reason/binary, "\n">>, <<"\n">>]),
ok = cowboy_req:stream_body(CloseFrame, fin, Req)
end.
end.

View File

@ -13,6 +13,7 @@
-export([init/2]).
init(Req0, Opts = [Mod|_]) ->
ok = iot_log:set_metadata(),
Method = binary_to_list(cowboy_req:method(Req0)),
Path = binary_to_list(cowboy_req:path(Req0)),
GetParams0 = cowboy_req:parse_qs(Req0),
@ -84,4 +85,4 @@ read_body(Req, AccData) ->
{ok, <<AccData/binary, Data/binary>>, Req1};
{more, Data, Req1} ->
read_body(Req1, <<AccData/binary, Data/binary>>)
end.
end.

View File

@ -64,6 +64,7 @@ start_link(Ref, Transport, Opts) ->
{ok, proc_lib:spawn_link(?MODULE, init, [Ref, Transport, Opts])}.
init(Ref, Transport, _Opts = []) ->
ok = iot_log:set_metadata(),
{ok, Socket} = ranch:handshake(Ref),
logger:debug("[sdlan_channel] get a new connection: ~p", [Socket]),
Transport:setopts(Socket, [binary, {active, true}, {packet, 4}]),