Repository: newrelic/elixir_agent (elixir)

Issue:
Logger formatting
**Describe the bug**
Not able to customize NewRelic.Logger formatting

Currently we have unified custom log format(json) across the org. We use [custom formatting](https://hexdocs.pm/logger/Logger.html#module-custom-formatting) and as nearly all the libraries use the standard Logger, this works without any issue.

Any specific reason for rolling custom logger instead of the standard Logger. It would be great if the library can use the standard Logger

**Environment**
* Elixir & Erlang version (`elixir -v`):

Erlang/OTP 21 [erts-10.1.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

Elixir 1.7.3 (compiled with Erlang/OTP 21)
* Agent version (`mix deps | grep new_relic_agent`): 

new_relic_agent 1.2.0


Pull request:
Allow setting the logger to use Logger
Traditionally, New Relic agents push our log messages to a specific file, primarily for easy access to them if someone needs support. This PR adds another setting which enables them to be sent to the standard Elixir `Logger` if someone wants that.

Opt-in by:
```elixir
config :new_relic_agent, log: "Logger"
```

closes #64 

Patch:
diff --git a/lib/new_relic/config.ex b/lib/new_relic/config.ex
index 8bf7f69..e110d61 100644
--- a/lib/new_relic/config.ex
+++ b/lib/new_relic/config.ex
@@ -24,6 +24,7 @@ defmodule NewRelic.Config do
 
   Options:
   - `"stdout"`
+  - `"Logger"` Elixir's Logger
   - `"memory"` (Useful for testing)
   - `"file_name.log"`
   """
diff --git a/lib/new_relic/logger.ex b/lib/new_relic/logger.ex
index abff7bf..e5d0687 100644
--- a/lib/new_relic/logger.ex
+++ b/lib/new_relic/logger.ex
@@ -1,5 +1,6 @@
 defmodule NewRelic.Logger do
   use GenServer
+  require Logger
 
   # Log Agent events to the configured output device
   #  - "tmp/new_relic.log" (Default)
@@ -29,6 +30,11 @@ defmodule NewRelic.Logger do
 
   # Server
 
+  def handle_cast({:log, level, message}, %{io_device: Logger} = state) do
+    Logger.log(level, "new_relic_agent - " <> message)
+    {:noreply, state}
+  end
+
   def handle_cast({:log, level, message}, %{io_device: io_device} = state) do
     IO.write(io_device, formatted(level, message))
     {:noreply, state}
@@ -54,12 +60,14 @@ defmodule NewRelic.Logger do
       nil -> {:file, "tmp/new_relic.log"}
       "stdout" -> :stdio
       "memory" -> :memory
+      "Logger" -> :logger
       log_file_path -> {:file, log_file_path}
     end
   end
 
   def device(:stdio), do: {:ok, :stdio}
   def device(:memory), do: StringIO.open("")
+  def device(:logger), do: {:ok, Logger}
 
   def device({:file, logfile}) do
     log(:info, "Log File: #{Path.absname(logfile)}")


Test patch:
diff --git a/test/config_test.exs b/test/config_test.exs
index f836569..db44f16 100644
--- a/test/config_test.exs
+++ b/test/config_test.exs
@@ -14,6 +14,9 @@ defmodule ConfigTest do
     System.put_env("NEW_RELIC_LOG", "some_file.log")
     assert {:file, "some_file.log"} == NewRelic.Logger.initial_logger()
 
+    System.put_env("NEW_RELIC_LOG", "Logger")
+    assert :logger == NewRelic.Logger.initial_logger()
+
     System.delete_env("NEW_RELIC_LOG")
     assert inital_logger == NewRelic.Logger.initial_logger()
   end
diff --git a/test/logger_test.exs b/test/logger_test.exs
index 3e42490..15fcbe5 100644
--- a/test/logger_test.exs
+++ b/test/logger_test.exs
@@ -30,4 +30,12 @@ defmodule LoggerTest do
       GenServer.call(NewRelic.Logger, {:replace, previous_logger})
     end
   end
+
+  @tag :capture_log
+  test "Logger logger" do
+    previous_logger = GenServer.call(NewRelic.Logger, {:logger, :logger})
+    NewRelic.log(:info, "HELLO")
+    NewRelic.log(:error, "DANG")
+    GenServer.call(NewRelic.Logger, {:replace, previous_logger})
+  end
 end