Skip to content

Instantly share code, notes, and snippets.

@cjbottaro
Created May 4, 2017 19:50
Show Gist options
  • Save cjbottaro/3d9c98d3f1192f00e86d293c20ceb21f to your computer and use it in GitHub Desktop.
Save cjbottaro/3d9c98d3f1192f00e86d293c20ceb21f to your computer and use it in GitHub Desktop.
Different log level for different otp apps
config :gnat, logger: [
level: :info
]
defmodule Gnat.Stream.Logger do
use Gnat.Logging, otp_app: :gnat
end
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