From: Siraaj Khandkar Date: Thu, 20 Aug 2015 04:47:16 +0000 (-0400) Subject: Log with error logger. X-Git-Tag: 0.0.0~7 X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=f079a56c67f6d3210dc1e4e876eab915fe299013;p=beam_stats.git Log with error logger. --- diff --git a/src/beam_stats_consumer_csv.erl b/src/beam_stats_consumer_csv.erl index bfa61cd..c9d2785 100644 --- a/src/beam_stats_consumer_csv.erl +++ b/src/beam_stats_consumer_csv.erl @@ -1,6 +1,7 @@ -module(beam_stats_consumer_csv). -include("include/beam_stats.hrl"). +-include("beam_stats_logging.hrl"). -behaviour(beam_stats_consumer). @@ -56,14 +57,14 @@ terminate(#state{file=FileOpt}) -> -spec try_to_write(state(), binary()) -> state(). try_to_write(#state{file=none, path=Path}=State, _) -> - io:format("error: file closed: ~s~n", [Path]), + ?log_error("Writing to file (~p) failed: no file in state.", [Path]), State; try_to_write(#state{file={some, File}}=State, Payload) -> case file:write(File, Payload) of ok -> State ; {error, _}=Error -> - io:format("error: file:write/2 failed: ~p~n", [Error]), + ?log_error("file:write(~p, ~p) -> ~p", [File, Payload, Error]), % TODO: Maybe schedule retry? ok = file:close(File), State#state{file=none} @@ -74,11 +75,12 @@ try_to_write(#state{file={some, File}}=State, Payload) -> try_to_open_if_no_file(#state{file={some, _}}=State) -> State; try_to_open_if_no_file(#state{file=none, path=Path}=State) -> - case file:open(Path, [append]) + Options = [append], + case file:open(Path, Options) of {ok, File} -> State#state{file = {some, File}} ; {error, _}=Error -> - io:format("error: file:open/2 failed: ~p~n", [Error]), + ?log_error("file:open(~p, ~p) -> ~p", [Path, Options, Error]), State#state{file = none} end. diff --git a/src/beam_stats_consumer_graphite.erl b/src/beam_stats_consumer_graphite.erl index be4f1f9..e788d29 100644 --- a/src/beam_stats_consumer_graphite.erl +++ b/src/beam_stats_consumer_graphite.erl @@ -1,6 +1,7 @@ -module(beam_stats_consumer_graphite). -include("include/beam_stats.hrl"). +-include("beam_stats_logging.hrl"). -behaviour(beam_stats_consumer). @@ -67,7 +68,7 @@ terminate(#state{sock=SockOpt}) -> -spec try_to_send(state(), binary()) -> state(). try_to_send(#state{sock=none}=State, _) -> - io:format("error: socket closed~n"), + ?log_error("Sending failed. No socket in state."), % TODO: Maybe schedule retry? State; try_to_send(#state{sock={some, Sock}}=State, Payload) -> @@ -75,7 +76,7 @@ try_to_send(#state{sock={some, Sock}}=State, Payload) -> of ok -> State ; {error, _}=Error -> - io:format("error: gen_tcp:send/2 failed: ~p~n", [Error]), + ?log_error("gen_tcp:send(~p, ~p) -> ~p", [Sock, Payload, Error]), % TODO: Maybe schedule retry? ok = gen_tcp:close(Sock), State#state{sock=none} @@ -93,11 +94,15 @@ try_to_connect_if_no_socket( , timeout = Timeout }=State ) -> - case gen_tcp:connect(Host, Port, [binary, {active, false}], Timeout) + Options = [binary, {active, false}], + case gen_tcp:connect(Host, Port, Options, Timeout) of {ok, Sock} -> State#state{sock = {some, Sock}} ; {error, _}=Error -> - io:format("error: gen_tcp:connect/4 failed: ~p~n", [Error]), + ?log_error( + "gen_tcp:connect(~p, ~p, ~p, ~p) -> ~p", + [Host, Port, Options, Timeout, Error] + ), State#state{sock = none} end. diff --git a/src/beam_stats_consumer_statsd.erl b/src/beam_stats_consumer_statsd.erl index 6baf459..97e458d 100644 --- a/src/beam_stats_consumer_statsd.erl +++ b/src/beam_stats_consumer_statsd.erl @@ -1,6 +1,7 @@ -module(beam_stats_consumer_statsd). -include("include/beam_stats.hrl"). +-include("beam_stats_logging.hrl"). -behaviour(beam_stats_consumer). @@ -99,7 +100,7 @@ terminate(#state{sock=SockOpt}) -> -spec try_to_send(state(), binary()) -> state(). try_to_send(#state{sock=none}=State, _) -> - io:format("error: socket closed~n"), + ?log_error("Sending failed. No socket in state."), % TODO: Maybe schedule retry? State; try_to_send( @@ -114,7 +115,10 @@ try_to_send( of ok -> State ; {error, _}=Error -> - io:format("error: gen_udp:send/4 failed: ~p~n", [Error]), + ?log_error( + "gen_udp:send(~p, ~p, ~p, ~p) -> ~p", + [Sock, DstHost, DstPort, Error] + ), % TODO: Do something with unsent messages? ok = gen_udp:close(Sock), State#state{sock=none} @@ -129,7 +133,7 @@ try_to_connect_if_no_socket(#state{sock=none, src_port=SrcPort}=State) -> of {ok, Sock} -> State#state{sock = {some, Sock}} ; {error, _}=Error -> - io:format("error: gen_udp:open/1 failed: ~p~n", [Error]), + ?log_error("gen_udp:open(~p) -> ~p", [SrcPort, Error]), State#state{sock = none} end. diff --git a/src/beam_stats_logging.hrl b/src/beam_stats_logging.hrl new file mode 100644 index 0000000..e2f269d --- /dev/null +++ b/src/beam_stats_logging.hrl @@ -0,0 +1,2 @@ +-define(log_error(Format) , error_logger:error_msg(Format)). +-define(log_error(Format, Args), error_logger:error_msg(Format, Args)).