Skip to content

Commit

Permalink
Fix test, print logging errors to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
svetob committed Aug 1, 2016
1 parent c30018e commit 454187b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/connection/tcp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ defmodule LogstashJson.TCP.Connection do
{:close, from} ->
Connection.reply(from, :ok)
{:error, :closed} ->
Logger.error("#{host}:#{Integer.to_string(port)} connection closed")
IO.puts "#{__MODULE__}: #{host}:#{inspect port} connection closed"
{:error, reason} ->
reason = :inet.format_error(reason)
Logger.error("#{host}:#{Integer.to_string(port)} connection error: #{reason}")
IO.puts "#{__MODULE__}: #{host}:#{inspect port} connection error: #{reason}"
end
{:connect, :reconnect, %{state | sock: nil}}
end
Expand Down
11 changes: 9 additions & 2 deletions lib/logstash_json/message.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
defmodule LogstashJson.Message do

def message(level, msg, ts, md, %{metadata: metadata, fields: fields}) do
Poison.encode!(json(level, msg, ts, md, %{metadata: metadata, fields: fields}))
json(level, msg, ts, md, metadata |> print_pids, fields |> print_pids)
|> Poison.encode!()
end

def json(level, msg, ts, md, %{metadata: metadata, fields: fields}) do
defp json(level, msg, ts, md, metadata, fields) do
%{
"@timestamp": LogstashJson.Timestamp.timestamp(ts),
level: level,
Expand All @@ -15,4 +16,10 @@ defmodule LogstashJson.Message do
line: md[:line]
} |> Map.merge(fields)
end

defp print_pids(it) when is_map(it), do: Enum.into it, %{}, fn {k, v} -> {k, print_pids(v)} end
defp print_pids(it) when is_list(it), do: Enum.map it, fn x -> print_pids(x) end
defp print_pids(it) when is_tuple(it), do: List.to_tuple(print_pids(Tuple.to_list(it)))
defp print_pids(it) when is_pid(it), do: inspect(it)
defp print_pids(it), do: it
end
31 changes: 22 additions & 9 deletions lib/logstash_json_tcp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ defmodule LogstashJson.TCP do
alias LogstashJson.TCP

def init({__MODULE__, name}) do
{:ok, configure(name, [])}
if user = Process.whereis(:user) do
Process.group_leader(self(), user)
{:ok, configure(name, [])}
else
{:error, :ignore}
end
end

def handle_call({:configure, opts}, %{name: name}) do
Expand All @@ -30,23 +35,31 @@ defmodule LogstashJson.TCP do
:ok
end


defp configure(name, opts) do
env = Application.get_env(:logger, name, [])
opts = Keyword.merge(env, opts)
Application.put_env(:logger, name, opts)

level = Keyword.get(opts, :level)
host = to_char_list(Keyword.get(opts, :host))
{port,_} = Integer.parse(Keyword.get(opts, :port))
metadata = Keyword.get(opts, :metadata)
fields = Keyword.get(opts, :fields) || %{}
level = Keyword.get(opts, :level) || :debug
host = to_char_list(Keyword.get(opts, :host))
port = env_int(Keyword.get(opts, :port))
metadata = Keyword.get(opts, :metadata) || []
fields = Keyword.get(opts, :fields) || %{}

{:ok, conn} = connect(host, port)

%{metadata: metadata, level: level, host: host, port: port, conn: conn, fields: fields}
%{metadata: metadata,
level: level,
host: host,
port: port,
conn: conn,
fields: fields,
name: name}
end

defp env_int(e) when is_integer(e), do: e
defp env_int(e), do: Integer.parse(e) |> elem(0)

@connection_opts [mode: :binary, keepalive: true]
defp connect(host, port) do
TCP.Connection.start_link(host, port, @connection_opts)
Expand All @@ -58,7 +71,7 @@ defmodule LogstashJson.TCP do
:ok ->
{:noreply, state}
{:error, reason} ->
IO.puts "Error logging over TCP: #{inspect reason}"
IO.puts "#{__MODULE__}: Error when logging: #{inspect reason}"
{:noreply, state}
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/logstash_json_test.exs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule LogstashJsonTest do
use ExUnit.Case
use ExUnit.Case, async: false
require Logger

doctest LogstashJson.TCP

test "Happy case" do
{port, _} = Application.get_env(:logger, :logstash)
|> Keyword.get(:port)
|> Integer.parse()
{:ok, listener} = :gen_tcp.listen 0, [:binary, {:active, false}, {:packet, 0}, {:reuseaddr, true}]
{:ok, port} = :inet.port listener
Logger.configure_backend {LogstashJson.TCP, :logstash}, port: port

{:ok, listener} = :gen_tcp.listen(port, [active: false, packet: 0, mode: :binary, reuseaddr: true])
{:ok, socket} = :gen_tcp.accept listener, 1000
Logger.info "Hello world!"

{:ok, socket} = :gen_tcp.accept(listener, 1000)
{:ok, msg} = :gen_tcp.recv(socket, 0, 1000)
:ok = :gen_tcp.close socket
:ok = :gen_tcp.close listener
Expand Down

0 comments on commit 454187b

Please sign in to comment.