-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
92 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ use Mix.Config | |
config :logger, | ||
level: :info, | ||
compile_time_purge_level: :debug | ||
|
||
config :json, log_level: :info |
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,49 @@ | ||
defmodule Json.Logger do | ||
@moduledoc """ | ||
Exposes separate log level configuration so developers can set logging | ||
verbosity for json library | ||
To configure log level only for json library, add folowing line in config file | ||
use Mix.Config | ||
# to make json be very verbose | ||
config :json, log_level: :debug | ||
# to make json be silent | ||
config :json, log_level: :error | ||
""" | ||
require Logger | ||
@levels [:debug, :info, :warn, :error] | ||
@level Application.get_env(:json, :log_level, :info) | ||
|
||
@allowed_levels @levels | ||
|> Enum.reverse() | ||
|> Enum.reduce_while([], fn l, acc -> | ||
if l == @level do | ||
{:halt, [@level | acc]} | ||
else | ||
{:cont, [l | acc]} | ||
end | ||
end) | ||
|> Enum.reverse() | ||
|
||
@doc false | ||
@spec allowed_levels() :: [:debug | :error | :info | :warn, ...] | ||
def allowed_levels(), do: @allowed_levels | ||
|
||
@doc """ | ||
Logs given message to logger at given log level | ||
Supported log levels are: | ||
- `:debug` - All messages are logged | ||
- `:info` - only :info, :warn and :error messages are logged | ||
- `:warn` - only :warn and :error messages are logged | ||
- `:error` - only :error messages are logged | ||
""" | ||
defmacro log(level, message) do | ||
quote bind_quoted: [level: level, message: message] do | ||
if level in Json.Logger.allowed_levels() do | ||
Logger.log(level, message) | ||
end | ||
end | ||
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