jschniper / gelf_logger

An Elixir Logger backend for GELF
MIT License
29 stars 27 forks source link

Feature/add formatter #22

Closed Kociamber closed 5 years ago

Kociamber commented 5 years ago

I needed to be able to use "external" formatter for the message and metadata before sending them to Greylog. I made a short mod in order to be able to use formatter "callback" and I thought other ppl might find it useful too.

jschniper commented 5 years ago

I was going to try to merge this with the other outstanding pull request for supporting an option for an inline format. Is there anyway you could send me a sample formatter so I can build out a test case to make sure everything works as expected.

Kociamber commented 5 years ago

Sure, I'm pasting short code below. Would you like me to add tests to my PR as well? I can do it quickly. Cheers!

defmodule LogFormatter do
  @moduledoc """
  Provides a set of functions for logged message transformation.
  """

  @doc """
  Main function of the formatter.
  """
  @spec format(atom(), list(), tuple(), list()) :: {atom(), list(),
tuple(), list()}
  def format(level, message, timestamp, metadata) do
    metadata = add_us_precision_timestamp_to_metadata(metadata)
    {level, message, timestamp, metadata}
  end

  # helper functions
  defp add_us_precision_timestamp_to_metadata(metadata) do
    Keyword.merge(metadata, timestamp_us: :os.system_time(:micro_seconds))
  end
end

EDIT

Was carried away and added the test ;)

jschniper commented 5 years ago

Quick question: what was the reason for including the arity in the configuration? From the Logger docs it looks as though it's always called with four arguments

jschniper commented 5 years ago

Fundamentally the code is the same but I reworked a few pieces and removed the arity part of the tuple since it didn't seem necessary. I haven't pushed a new build to hex but you should be able to run the code in master right now with little change. Let me know if you run into any issues.

Kociamber commented 5 years ago

Hi! Thanks :) Indeed, arity wasn't in the end necessary at all so that's fine.

czw., 2 maj 2019 o 19:59 Joshua Schniper notifications@github.com napisał(a):

Fundamentally the code is the same but I reworked a few pieces and removed the arity part of the tuple since it didn't seem necessary. I haven't pushed a new build to hex but you should be able to run the code in master right now with little change. Let me know if you run into any issues.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jschniper/gelf_logger/pull/22#issuecomment-488771031, or mute the thread https://github.com/notifications/unsubscribe-auth/ACTENIB6E43AERYSY3HXJVTPTMTXJANCNFSM4HJKUMBQ .

Kociamber commented 5 years ago

Hi, would you be able to push it to hex today? Cheers, Rafał

czw., 2 maj 2019 o 20:37 Rafał Kociszewski rafal.kociszewski@gmail.com napisał(a):

Hi! Thanks :) Indeed, arity wasn't in the end necessary at all so that's fine.

czw., 2 maj 2019 o 19:59 Joshua Schniper notifications@github.com napisał(a):

Fundamentally the code is the same but I reworked a few pieces and removed the arity part of the tuple since it didn't seem necessary. I haven't pushed a new build to hex but you should be able to run the code in master right now with little change. Let me know if you run into any issues.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jschniper/gelf_logger/pull/22#issuecomment-488771031, or mute the thread https://github.com/notifications/unsubscribe-auth/ACTENIB6E43AERYSY3HXJVTPTMTXJANCNFSM4HJKUMBQ .

jschniper commented 5 years ago

Just pushed it. Let me know if you run into any problems.