fix endpoint_log
This commit is contained in:
parent
037eb775b4
commit
29f5bfb17c
@ -16,8 +16,7 @@
|
||||
|
||||
-define(SERVER, ?MODULE).
|
||||
-define(LOG_NAME, endpoint_unmatched_publish_log).
|
||||
-define(DEFAULT_PATH_TEMPLATE, "${endpoint_root}/endpoint_log/unmatched_publish.log").
|
||||
-define(DEFAULT_ENDPOINT_ROOT_DIR, "log").
|
||||
-define(LOG_FILE_NAME, "unmatched_publish.log").
|
||||
-define(DEFAULT_MAX_BYTES, 10485760).
|
||||
-define(DEFAULT_MAX_FILES, 10).
|
||||
-define(MAX_LOG_CONTENT_BYTES, 32 * 1024).
|
||||
@ -104,11 +103,13 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
|
||||
-spec open_log() -> ok | {error, term()}.
|
||||
open_log() ->
|
||||
Config = log_config(),
|
||||
Path = log_path(Config),
|
||||
ok = filelib:ensure_dir(Path),
|
||||
case log_dir(Config) of
|
||||
{ok, Path} ->
|
||||
File = filename:join(Path, ?LOG_FILE_NAME),
|
||||
ok = filelib:ensure_dir(File),
|
||||
case disk_log:open([
|
||||
{name, ?LOG_NAME},
|
||||
{file, Path},
|
||||
{file, File},
|
||||
{type, wrap},
|
||||
{format, external},
|
||||
{size, {config_pos_integer(max_bytes, Config, ?DEFAULT_MAX_BYTES),
|
||||
@ -122,6 +123,9 @@ open_log() ->
|
||||
ok;
|
||||
{error, Reason} ->
|
||||
{error, Reason}
|
||||
end;
|
||||
{error, Reason} ->
|
||||
{error, Reason}
|
||||
end.
|
||||
|
||||
-spec log_config() -> proplists:proplist().
|
||||
@ -133,18 +137,13 @@ log_config() ->
|
||||
[]
|
||||
end.
|
||||
|
||||
-spec log_path(proplists:proplist()) -> file:filename_all().
|
||||
log_path(Config) ->
|
||||
Template = proplists:get_value(path, Config, ?DEFAULT_PATH_TEMPLATE),
|
||||
expand_path_template(Template).
|
||||
|
||||
-spec endpoint_root_dir() -> file:filename_all().
|
||||
endpoint_root_dir() ->
|
||||
case application:get_env(endpoint, endpoints) of
|
||||
{ok, Endpoints} ->
|
||||
proplists:get_value(root_dir, Endpoints, ?DEFAULT_ENDPOINT_ROOT_DIR);
|
||||
undefined ->
|
||||
?DEFAULT_ENDPOINT_ROOT_DIR
|
||||
-spec log_dir(proplists:proplist()) -> {ok, file:filename_all()} | {error, term()}.
|
||||
log_dir(Config) ->
|
||||
case proplists:get_value(path, Config) of
|
||||
Path when is_list(Path); is_binary(Path) ->
|
||||
{ok, Path};
|
||||
_ ->
|
||||
{error, {missing_config, endpoint_log, path}}
|
||||
end.
|
||||
|
||||
-spec config_pos_integer(atom(), proplists:proplist(), pos_integer()) -> pos_integer().
|
||||
@ -156,33 +155,6 @@ config_pos_integer(Key, Config, Default) ->
|
||||
Default
|
||||
end.
|
||||
|
||||
-spec expand_path_template(file:filename_all()) -> file:filename_all().
|
||||
expand_path_template(Template) ->
|
||||
TemplateBin = iolist_to_binary(Template),
|
||||
Vars = path_vars(),
|
||||
Expanded = lists:foldl(fun({Name, Value}, Acc) ->
|
||||
replace_path_var(Name, Value, Acc)
|
||||
end, TemplateBin, Vars),
|
||||
binary_to_list(Expanded).
|
||||
|
||||
-spec path_vars() -> [{binary(), binary()}].
|
||||
path_vars() ->
|
||||
{{Year, Month, Day}, {Hour, _Minute, _Second}} = calendar:local_time(),
|
||||
[
|
||||
{<<"endpoint_root">>, iolist_to_binary(endpoint_root_dir())},
|
||||
{<<"date">>, format_date(Year, Month, Day)},
|
||||
{<<"year">>, integer_to_binary(Year)},
|
||||
{<<"month">>, two_digits(Month)},
|
||||
{<<"day">>, two_digits(Day)},
|
||||
{<<"hour">>, two_digits(Hour)},
|
||||
{<<"node">>, atom_to_binary(node(), utf8)}
|
||||
].
|
||||
|
||||
-spec replace_path_var(binary(), binary(), binary()) -> binary().
|
||||
replace_path_var(Name, Value, Template) ->
|
||||
Template0 = binary:replace(Template, <<"${", Name/binary, "}">>, Value, [global]),
|
||||
binary:replace(Template0, <<"{", Name/binary, "}">>, Value, [global]).
|
||||
|
||||
-spec encode_unmatched_publish(binary(), binary()) -> binary().
|
||||
encode_unmatched_publish(RouteKey, Content) ->
|
||||
{LoggedContent, Truncated} = maybe_truncate(Content),
|
||||
@ -210,14 +182,6 @@ format_local_time() ->
|
||||
[Year, Month, Day, Hour, Minute, Second, MilliSeconds]
|
||||
)).
|
||||
|
||||
-spec format_date(integer(), integer(), integer()) -> binary().
|
||||
format_date(Year, Month, Day) ->
|
||||
iolist_to_binary(io_lib:format("~4..0B-~2..0B-~2..0B", [Year, Month, Day])).
|
||||
|
||||
-spec two_digits(integer()) -> binary().
|
||||
two_digits(Value) ->
|
||||
iolist_to_binary(io_lib:format("~2..0B", [Value])).
|
||||
|
||||
-spec maybe_truncate(binary()) -> {binary(), boolean()}.
|
||||
maybe_truncate(Content) when byte_size(Content) =< ?MAX_LOG_CONTENT_BYTES ->
|
||||
{Content, false};
|
||||
|
||||
@ -31,7 +31,7 @@
|
||||
]},
|
||||
|
||||
{endpoint_log, [
|
||||
{path, "${ENDPOINT_LOG_PATH:-/var/lib/endpoint/endpoint_log/unmatched_publish.log}"},
|
||||
{path, "${ENDPOINT_LOG_PATH:-/var/lib/endpoint/endpoint_log}"},
|
||||
{max_bytes, ${ENDPOINT_LOG_MAX_BYTES:-10485760}},
|
||||
{max_files, ${ENDPOINT_LOG_MAX_FILES:-10}}
|
||||
]}
|
||||
|
||||
6
env.file
6
env.file
@ -1,17 +1,17 @@
|
||||
[dev]
|
||||
IOT_API_URL="http://127.0.0.1:18090/simulator"
|
||||
ENDPOINT_ROOT_DIR="/usr/local/code/database/"
|
||||
ENDPOINT_LOG_PATH="/usr/local/code/database/endpoint_log/unmatched_publish.log"
|
||||
ENDPOINT_LOG_PATH="/usr/local/code/database/endpoint_log"
|
||||
IOT_MNESIA_DIR="/var/lib/iot/mnesia/"
|
||||
|
||||
[test]
|
||||
IOT_API_URL="http://127.0.0.1/api/v1"
|
||||
ENDPOINT_ROOT_DIR="/usr/local/code/database/"
|
||||
ENDPOINT_LOG_PATH="/usr/local/code/database/endpoint_log/unmatched_publish.log"
|
||||
ENDPOINT_LOG_PATH="/usr/local/code/database/endpoint_log"
|
||||
IOT_MNESIA_DIR="/var/lib/iot/mnesia/"
|
||||
|
||||
[prod]
|
||||
IOT_API_URL="https://lgsiot.njau.edu.cn/api/v1/taskLog"
|
||||
ENDPOINT_ROOT_DIR="/var/lib/endpoint/database/"
|
||||
ENDPOINT_LOG_PATH="/var/lib/endpoint/endpoint_log/unmatched_publish.log"
|
||||
ENDPOINT_LOG_PATH="/var/lib/endpoint/endpoint_log"
|
||||
IOT_MNESIA_DIR="/var/lib/iot/mnesia/"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user