Created
May 4, 2017 19:50
-
-
Save cjbottaro/3d9c98d3f1192f00e86d293c20ceb21f to your computer and use it in GitHub Desktop.
Different log level for different otp apps
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
config :gnat, logger: [ | |
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
defmodule Gnat.Stream.Logger do | |
use Gnat.Logging, otp_app: :gnat | |
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
defmodule Gnat.Logging do | |
defmacro __using__(options) do | |
quote bind_quoted: [otp_app: options[:otp_app]] do | |
require Logger | |
def debug(message, metadata \\ []), do: log(:debug, message, metadata) | |
def info(message, metadata \\ []), do: log(:info, message, metadata) | |
def warn(message, metadata \\ []), do: log(:warn, message, metadata) | |
def error(message, metadata \\ []), do: log(:error, message, metadata) | |
@otp_app otp_app | |
def log(level, message, metadata \\ []) do | |
config = Application.get_env(@otp_app, :logger, []) | |
config_level = Keyword.get(config, :level, :debug) | |
if level_to_int(config_level) <= level_to_int(level) do | |
Logger.log(level, message, metadata) | |
end | |
end | |
defp level_to_int(:debug), do: 0 | |
defp level_to_int(:info), do: 1 | |
defp level_to_int(:warn), do: 2 | |
defp level_to_int(:error), do: 3 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment