Skip to content

Commit 48002b8

Browse files
committed
Merge branch 'improve-logging' into 'develop'
Improve signal logging, add timestamp to platform's See merge request programaker-project/programaker-core!202
2 parents e2faff7 + 9f91e2d commit 48002b8

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

backend/apps/automate_logging/src/automate_logging.erl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ log_signal_to_bridge_and_owner(Signal, BridgeId, {OwnerType, OwnerId}) ->
7171
} ->
7272
Url = lists:flatten(io_lib:format("~s/~s_~p_~s", [BaseURL, BridgeId, OwnerType, OwnerId])),
7373
Type = "application/json",
74-
Body = jiffy:encode(Signal),
74+
Body = list_to_binary([jiffy:encode(Signal)]),
7575
Headers = [],
7676
HTTPOptions = [],
7777
Options = [],
78-
{ok, _} = httpc:request(post, {Url, Headers, Type, Body}, HTTPOptions, Options);
78+
case httpc:request(post, {Url, Headers, Type, Body}, HTTPOptions, Options) of
79+
{ok, _} -> ok;
80+
{error, Reason} ->
81+
log_platform(error, list_to_binary(io_lib:format("Error logging signal: ~p", [Reason])))
82+
end;
7983
undefined ->
8084
io:fwrite("[Error] Signal logging configuration not set")
8185
end.
@@ -149,26 +153,26 @@ log_program_error(LogEntry=#user_program_log_entry{ severity=Severity, program_i
149153

150154
-spec log_platform(log_severity(), _, _, _) -> ok.
151155
log_platform(warning, ErrorNS, Error, _StackTrace) ->
152-
io:fwrite("[~p] ~p:~p~n", [warning, ErrorNS, Error]);
156+
io:fwrite("~s [~p] ~p:~p~n", [get_time_string(), warning, ErrorNS, Error]);
153157
log_platform(debug, _ErrorNS, _Error, _StackTrace) ->
154158
ok; %% Ignored for now
155159

156160
log_platform(Severity, ErrorNS, Error, StackTrace) ->
157-
io:fwrite("[~p] ~p:~p || ~p~n", [Severity, ErrorNS, Error, StackTrace]).
161+
io:fwrite("~s [~p] ~p:~p || ~p~n", [get_time_string(), Severity, ErrorNS, Error, StackTrace]).
158162

159163
-spec log_platform(atom(), _) -> ok.
160164
log_platform(Severity, Msg) when is_list(Msg) ->
161-
io:fwrite("[~p] ~s~n", [Severity, binary:list_to_bin(lists:flatten(Msg))]);
165+
io:fwrite("~s [~p] ~s~n", [get_time_string(), Severity, binary:list_to_bin(lists:flatten(Msg))]);
162166
log_platform(Severity, Msg) ->
163-
io:fwrite("[~p] ~p~n", [Severity, Msg]).
167+
io:fwrite("~s [~p] ~p~n", [get_time_string(), Severity, Msg]).
164168

165169
-spec log_api(log_severity(), _, _) -> ok.
166170
log_api(debug, _, _) ->
167171
ok; %% Ignored for now
168172
log_api(Severity, Endpoint, Error) when is_binary(Error) ->
169-
io:fwrite("[~p@~p] ~s~n", [Severity, Endpoint, Error]);
173+
io:fwrite("~s [~p@~p] ~s~n", [get_time_string(), Severity, Endpoint, Error]);
170174
log_api(Severity, Endpoint, Error) ->
171-
io:fwrite("[~p@~p] ~p~n", [Severity, Endpoint, Error]).
175+
io:fwrite("~s [~p@~p] ~p~n", [get_time_string(), Severity, Endpoint, Error]).
172176

173177

174178
%%====================================================================
@@ -192,3 +196,7 @@ get_signal_storage_config() ->
192196

193197
get_timestamp() ->
194198
erlang:system_time(millisecond).
199+
200+
get_time_string() ->
201+
{{Year,Month,Day},{Hour,Min,Sec}} = erlang:localtime(),
202+
io_lib:format("~4..0B/~2..0B/~2..0B ~2..0B:~2..0B:~2..0B", [Year, Month, Day, Hour, Min, Sec]).

0 commit comments

Comments
 (0)