From 4166d3fda8231d0d23e409c6b55fe36edb8e89fd Mon Sep 17 00:00:00 2001 From: GalaxyGorilla Date: Tue, 6 Oct 2015 20:17:54 +0200 Subject: [PATCH] log message formatting --- include/hello_log.hrl | 4 ++-- src/hello_log.erl | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/hello_log.hrl b/include/hello_log.hrl index 2160208..d4ab6f0 100644 --- a/include/hello_log.hrl +++ b/include/hello_log.hrl @@ -32,8 +32,8 @@ -define(LOG_REQUEST_request_stop(CallbackModule, HandlerId, Request, Response, Reason, Time, LogId), lager:info(lists:append(?REQ_TRACES(CallbackModule, HandlerId, Request, ok, Response, LogId), [{hello_error_reason, Reason}]), - "Hello handler with callback '~p' and service id '~p' answered synced request on method(s) '~p' and stopped in '~w' ms.", - [CallbackModule, HandlerId, hello_log:get_method(Request), Time])). + "~p ~p / ~p : answered synced request.", + [node(), hello_log:stringify(CallbackModule), hello_log:get_method(Request)])). -define(LOG_REQUEST_request_no_reply(CallbackModule, HandlerId, Request, Time, LogId), lager:debug(?REQ_TRACES(CallbackModule, HandlerId, Request, ok, LogId), diff --git a/src/hello_log.erl b/src/hello_log.erl index 20bd7aa..eb9e9bc 100644 --- a/src/hello_log.erl +++ b/src/hello_log.erl @@ -35,7 +35,7 @@ format([ Request = #request{} | Requests]) -> "[ " ++ format(Request) ++ " ], " ++ format(Requests); format(#request{id = ID, method = Method, args = Args}) -> lists:append(["ID: ", stringify(ID), "; METHOD: ", stringify(Method), - "; ARGS: ", stringify(Args)]); + "; ARGS: ", prettify_json(Args)]); %% -- response formatting; first for record responses, then for arbitrary data blobs format([ Response = #response{} ]) -> @@ -43,7 +43,7 @@ format([ Response = #response{} ]) -> format([ Response = #response{} | Responses]) -> "[ " ++ format(Response) ++ " ], " ++ format(Responses); format(#response{id = ID, response = CallbackResponse}) -> - lists:append(["ID: ", stringify(ID), "; RESPONSE: ", stringify(CallbackResponse)]); + lists:append(["ID: ", stringify(ID), "; RESPONSE: ", prettify_json(CallbackResponse)]); format(ignore) -> ["ignored"]; format({ok, CallbackResponse}) -> stringify(CallbackResponse); format(Msg) -> stringify(Msg). @@ -64,5 +64,16 @@ get_method([ #request{method = Method} | Requests]) -> get_method(#request{method = Method}) -> stringify(Method). -stringify(Term) -> +stringify(Term) when is_binary(Term) -> + stringify(binary_to_list(Term)); +stringify(Term) -> lists:flatten(io_lib:format("~p", [Term])). + +prettify_json(Term) -> + try + PrettyJson = jsx:format(jsx:encode(Term), [{space, 1}]), + binary_to_list(PrettyJson) + catch + _:_ -> + stringify(Term) + end.