fix endpoint_log
This commit is contained in:
parent
037eb775b4
commit
29f5bfb17c
@ -16,8 +16,7 @@
|
|||||||
|
|
||||||
-define(SERVER, ?MODULE).
|
-define(SERVER, ?MODULE).
|
||||||
-define(LOG_NAME, endpoint_unmatched_publish_log).
|
-define(LOG_NAME, endpoint_unmatched_publish_log).
|
||||||
-define(DEFAULT_PATH_TEMPLATE, "${endpoint_root}/endpoint_log/unmatched_publish.log").
|
-define(LOG_FILE_NAME, "unmatched_publish.log").
|
||||||
-define(DEFAULT_ENDPOINT_ROOT_DIR, "log").
|
|
||||||
-define(DEFAULT_MAX_BYTES, 10485760).
|
-define(DEFAULT_MAX_BYTES, 10485760).
|
||||||
-define(DEFAULT_MAX_FILES, 10).
|
-define(DEFAULT_MAX_FILES, 10).
|
||||||
-define(MAX_LOG_CONTENT_BYTES, 32 * 1024).
|
-define(MAX_LOG_CONTENT_BYTES, 32 * 1024).
|
||||||
@ -104,22 +103,27 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
|
|||||||
-spec open_log() -> ok | {error, term()}.
|
-spec open_log() -> ok | {error, term()}.
|
||||||
open_log() ->
|
open_log() ->
|
||||||
Config = log_config(),
|
Config = log_config(),
|
||||||
Path = log_path(Config),
|
case log_dir(Config) of
|
||||||
ok = filelib:ensure_dir(Path),
|
{ok, Path} ->
|
||||||
case disk_log:open([
|
File = filename:join(Path, ?LOG_FILE_NAME),
|
||||||
{name, ?LOG_NAME},
|
ok = filelib:ensure_dir(File),
|
||||||
{file, Path},
|
case disk_log:open([
|
||||||
{type, wrap},
|
{name, ?LOG_NAME},
|
||||||
{format, external},
|
{file, File},
|
||||||
{size, {config_pos_integer(max_bytes, Config, ?DEFAULT_MAX_BYTES),
|
{type, wrap},
|
||||||
config_pos_integer(max_files, Config, ?DEFAULT_MAX_FILES)}},
|
{format, external},
|
||||||
{linkto, self()},
|
{size, {config_pos_integer(max_bytes, Config, ?DEFAULT_MAX_BYTES),
|
||||||
{repair, true}
|
config_pos_integer(max_files, Config, ?DEFAULT_MAX_FILES)}},
|
||||||
]) of
|
{linkto, self()},
|
||||||
{ok, ?LOG_NAME} ->
|
{repair, true}
|
||||||
ok;
|
]) of
|
||||||
{repaired, ?LOG_NAME, _Recovered, _BadBytes} ->
|
{ok, ?LOG_NAME} ->
|
||||||
ok;
|
ok;
|
||||||
|
{repaired, ?LOG_NAME, _Recovered, _BadBytes} ->
|
||||||
|
ok;
|
||||||
|
{error, Reason} ->
|
||||||
|
{error, Reason}
|
||||||
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
@ -133,18 +137,13 @@ log_config() ->
|
|||||||
[]
|
[]
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec log_path(proplists:proplist()) -> file:filename_all().
|
-spec log_dir(proplists:proplist()) -> {ok, file:filename_all()} | {error, term()}.
|
||||||
log_path(Config) ->
|
log_dir(Config) ->
|
||||||
Template = proplists:get_value(path, Config, ?DEFAULT_PATH_TEMPLATE),
|
case proplists:get_value(path, Config) of
|
||||||
expand_path_template(Template).
|
Path when is_list(Path); is_binary(Path) ->
|
||||||
|
{ok, Path};
|
||||||
-spec endpoint_root_dir() -> file:filename_all().
|
_ ->
|
||||||
endpoint_root_dir() ->
|
{error, {missing_config, endpoint_log, path}}
|
||||||
case application:get_env(endpoint, endpoints) of
|
|
||||||
{ok, Endpoints} ->
|
|
||||||
proplists:get_value(root_dir, Endpoints, ?DEFAULT_ENDPOINT_ROOT_DIR);
|
|
||||||
undefined ->
|
|
||||||
?DEFAULT_ENDPOINT_ROOT_DIR
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec config_pos_integer(atom(), proplists:proplist(), pos_integer()) -> pos_integer().
|
-spec config_pos_integer(atom(), proplists:proplist(), pos_integer()) -> pos_integer().
|
||||||
@ -156,33 +155,6 @@ config_pos_integer(Key, Config, Default) ->
|
|||||||
Default
|
Default
|
||||||
end.
|
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().
|
-spec encode_unmatched_publish(binary(), binary()) -> binary().
|
||||||
encode_unmatched_publish(RouteKey, Content) ->
|
encode_unmatched_publish(RouteKey, Content) ->
|
||||||
{LoggedContent, Truncated} = maybe_truncate(Content),
|
{LoggedContent, Truncated} = maybe_truncate(Content),
|
||||||
@ -210,14 +182,6 @@ format_local_time() ->
|
|||||||
[Year, Month, Day, Hour, Minute, Second, MilliSeconds]
|
[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()}.
|
-spec maybe_truncate(binary()) -> {binary(), boolean()}.
|
||||||
maybe_truncate(Content) when byte_size(Content) =< ?MAX_LOG_CONTENT_BYTES ->
|
maybe_truncate(Content) when byte_size(Content) =< ?MAX_LOG_CONTENT_BYTES ->
|
||||||
{Content, false};
|
{Content, false};
|
||||||
|
|||||||
@ -31,7 +31,7 @@
|
|||||||
]},
|
]},
|
||||||
|
|
||||||
{endpoint_log, [
|
{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_bytes, ${ENDPOINT_LOG_MAX_BYTES:-10485760}},
|
||||||
{max_files, ${ENDPOINT_LOG_MAX_FILES:-10}}
|
{max_files, ${ENDPOINT_LOG_MAX_FILES:-10}}
|
||||||
]}
|
]}
|
||||||
|
|||||||
6
env.file
6
env.file
@ -1,17 +1,17 @@
|
|||||||
[dev]
|
[dev]
|
||||||
IOT_API_URL="http://127.0.0.1:18090/simulator"
|
IOT_API_URL="http://127.0.0.1:18090/simulator"
|
||||||
ENDPOINT_ROOT_DIR="/usr/local/code/database/"
|
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/"
|
IOT_MNESIA_DIR="/var/lib/iot/mnesia/"
|
||||||
|
|
||||||
[test]
|
[test]
|
||||||
IOT_API_URL="http://127.0.0.1/api/v1"
|
IOT_API_URL="http://127.0.0.1/api/v1"
|
||||||
ENDPOINT_ROOT_DIR="/usr/local/code/database/"
|
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/"
|
IOT_MNESIA_DIR="/var/lib/iot/mnesia/"
|
||||||
|
|
||||||
[prod]
|
[prod]
|
||||||
IOT_API_URL="https://lgsiot.njau.edu.cn/api/v1/taskLog"
|
IOT_API_URL="https://lgsiot.njau.edu.cn/api/v1/taskLog"
|
||||||
ENDPOINT_ROOT_DIR="/var/lib/endpoint/database/"
|
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/"
|
IOT_MNESIA_DIR="/var/lib/iot/mnesia/"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user