-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic formatter and refactored common functions (#35)
* Added basic formatter and refactored common functions * Added tests for basic logger and made it the default * Fixed tests and added option to disable callback * Removed unused import * Setting formatter in ecto+plug tests * Trying to fix test race condition * Fixed testing errors * Fixing Elixir 1.6 formatting error
- Loading branch information
Showing
10 changed files
with
239 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
defmodule LoggerJSON.FormatterUtils do | ||
@moduledoc """ | ||
This module contains functions that can be used across different | ||
`LoggerJSON.Formatter` implementations to provide common functionality. | ||
""" | ||
|
||
import Jason.Helpers, only: [json_map: 1] | ||
|
||
@doc """ | ||
Format an exception for use within a log entry | ||
""" | ||
def format_process_crash(md) do | ||
if crash_reason = Keyword.get(md, :crash_reason) do | ||
initial_call = Keyword.get(md, :initial_call) | ||
|
||
json_map( | ||
initial_call: format_initial_call(initial_call), | ||
reason: format_crash_reason(crash_reason) | ||
) | ||
end | ||
end | ||
|
||
@doc """ | ||
RFC3339 UTC "Zulu" format | ||
""" | ||
def format_timestamp({date, time}) do | ||
[format_date(date), ?T, format_time(time), ?Z] | ||
|> IO.iodata_to_binary() | ||
end | ||
|
||
@doc """ | ||
Provide a string output of the MFA log entry | ||
""" | ||
def format_function(nil, function), do: function | ||
def format_function(module, function), do: "#{module}.#{function}" | ||
def format_function(module, function, arity), do: "#{module}.#{function}/#{arity}" | ||
|
||
@doc """ | ||
""" | ||
def maybe_put(map, _key, nil), do: map | ||
def maybe_put(map, key, value), do: Map.put(map, key, value) | ||
|
||
defp format_initial_call(nil), do: nil | ||
defp format_initial_call({module, function, arity}), do: format_function(module, function, arity) | ||
|
||
defp format_crash_reason({:throw, reason}) do | ||
Exception.format(:throw, reason) | ||
end | ||
|
||
defp format_crash_reason({:exit, reason}) do | ||
Exception.format(:exit, reason) | ||
end | ||
|
||
defp format_crash_reason({%{} = exception, stacktrace}) do | ||
Exception.format(:error, exception, stacktrace) | ||
end | ||
|
||
defp format_crash_reason(other) do | ||
inspect(other) | ||
end | ||
|
||
defp format_time({hh, mi, ss, ms}) do | ||
[pad2(hh), ?:, pad2(mi), ?:, pad2(ss), ?., pad3(ms)] | ||
end | ||
|
||
defp format_date({yy, mm, dd}) do | ||
[Integer.to_string(yy), ?-, pad2(mm), ?-, pad2(dd)] | ||
end | ||
|
||
defp pad3(int) when int < 10, do: [?0, ?0, Integer.to_string(int)] | ||
defp pad3(int) when int < 100, do: [?0, Integer.to_string(int)] | ||
defp pad3(int), do: Integer.to_string(int) | ||
|
||
defp pad2(int) when int < 10, do: [?0, Integer.to_string(int)] | ||
defp pad2(int), do: Integer.to_string(int) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule LoggerJSON.Formatters.BasicLogger do | ||
@moduledoc """ | ||
Basic JSON log formatter with no vender specific formatting | ||
""" | ||
|
||
import Jason.Helpers, only: [json_map: 1] | ||
|
||
alias LoggerJSON.FormatterUtils | ||
|
||
@behaviour LoggerJSON.Formatter | ||
|
||
@processed_metadata_keys ~w[pid file line function module application]a | ||
|
||
@impl true | ||
def format_event(level, msg, ts, md, md_keys) do | ||
json_map( | ||
time: FormatterUtils.format_timestamp(ts), | ||
severity: Atom.to_string(level), | ||
message: IO.iodata_to_binary(msg), | ||
metadata: format_metadata(md, md_keys) | ||
) | ||
end | ||
|
||
defp format_metadata(md, md_keys) do | ||
md | ||
|> LoggerJSON.take_metadata(md_keys, @processed_metadata_keys) | ||
|> FormatterUtils.maybe_put(:error, FormatterUtils.format_process_crash(md)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.