Packages
API for the Bunyan distributed and pluggable logging system (error, warn, info, and debug functions)
Current section
3 Versions
Jump to
Current section
3 Versions
Compare versions
4
files changed
+135
additions
-63
deletions
| @@ -4,10 +4,9 @@ | |
| 4 4 | <<"API for the Bunyan distributed and pluggable logging system (error, warn, info, and debug functions)">>}. |
| 5 5 | {<<"elixir">>,<<"~> 1.6">>}. |
| 6 6 | {<<"files">>, |
| 7 | - [<<"lib">>,<<"lib/api">>,<<"lib/api.ex">>,<<"lib/api/injector.ex">>, |
| 8 | - <<"lib/api/server.ex">>,<<"lib/api/state.ex">>,<<"mix.exs">>, |
| 9 | - <<"README.md">>,<<"shared_build_stuff/LICENSE.md">>, |
| 10 | - <<"shared_build_stuff/mix.exs">>]}. |
| 7 | + [<<"lib">>,<<"lib/api">>,<<"lib/api.ex">>,<<"lib/api/server.ex">>, |
| 8 | + <<"lib/api/state.ex">>,<<"mix.exs">>,<<"README.md">>, |
| 9 | + <<"shared_build_stuff/LICENSE.md">>,<<"shared_build_stuff/mix.exs">>]}. |
| 11 10 | {<<"licenses">>,[<<"Apache 2.0">>]}. |
| 12 11 | {<<"links">>, |
| 13 12 | [{<<"Bunyan">>,<<"https://github.com/pragdave/bunyan">>}, |
| @@ -20,4 +19,4 @@ | |
| 20 19 | {<<"optional">>,false}, |
| 21 20 | {<<"repository">>,<<"hexpm">>}, |
| 22 21 | {<<"requirement">>,<<">= 0.0.0">>}]]}. |
| 23 | - {<<"version">>,<<"0.5.2">>}. |
| 22 | + {<<"version">>,<<"0.5.3">>}. |
| @@ -1,3 +1,133 @@ | |
| 1 | + # This is a little funky... |
| 2 | + |
| 3 | + # We want the API functions to be available in the top-level Bunyan module, |
| 4 | + # but they are an optional dependency. |
| 5 | + # |
| 6 | + # So we test to see if the api has been given as a dependency in our |
| 7 | + # host assembly, and if so we get it to inject the API functions |
| 8 | + # into this module. |
| 9 | + |
| 10 | + defmodule Bunyan do |
| 11 | + |
| 12 | + @moduledoc """ |
| 13 | + See BunyanLogger for details |
| 14 | + """ |
| 15 | + |
| 16 | + alias Bunyan.Shared.Level |
| 17 | + |
| 18 | + |
| 19 | + @doc """ |
| 20 | + Generate a debug-level log message. |
| 21 | + |
| 22 | + The first parameter may be a string or a zero-arity function that returns a |
| 23 | + string. The function is only called if the message is to be generated. |
| 24 | + |
| 25 | + The second parameter is any Elixir term, which is formatted and printed below |
| 26 | + the log message. |
| 27 | + |
| 28 | + ### Example |
| 29 | + |
| 30 | + ~~~ elixir |
| 31 | + debug("Enter formatting funcion", args) |
| 32 | + |
| 33 | + debug(fn -> some_expensive_operation end) |
| 34 | + ~~~ |
| 35 | + """ |
| 36 | + |
| 37 | + defmacro debug(msg_or_fun, extra \\ nil), do: maybe_generate(:debug, msg_or_fun, extra) |
| 38 | + |
| 39 | + @doc """ |
| 40 | + Generate an info-level log message. |
| 41 | + |
| 42 | + The first parameter may be a string or a zero-arity function that returns a |
| 43 | + string. The function is only called if the message is to be generated. |
| 44 | + |
| 45 | + The second parameter is any Elixir term, which is formatted and printed below |
| 46 | + the log message. |
| 47 | + |
| 48 | + ### Example |
| 49 | + |
| 50 | + ~~~ elixir |
| 51 | + info("Stale carts removed", statistics) |
| 52 | + ~~~ |
| 53 | + """ |
| 54 | + |
| 55 | + defmacro info(msg_or_fun, extra \\ nil), do: maybe_generate(:info, msg_or_fun, extra) |
| 56 | + |
| 57 | + |
| 58 | + @doc """ |
| 59 | + Generate an warning-level log message. |
| 60 | + |
| 61 | + The first parameter may be a string or a zero-arity function that returns a |
| 62 | + string. The function is only called if the message is to be generated. |
| 63 | + |
| 64 | + The second parameter is any Elixir term, which is formatted and printed below |
| 65 | + the log message. |
| 66 | + |
| 67 | + ### Example |
| 68 | + |
| 69 | + ~~~ elixir |
| 70 | + warn("Disk is 85% full") |
| 71 | + ~~~ |
| 72 | + """ |
| 73 | + |
| 74 | + defmacro warn(msg_or_fun, extra \\ nil), do: maybe_generate(:warn, msg_or_fun, extra) |
| 75 | + |
| 76 | + |
| 77 | + @doc """ |
| 78 | + Generate an error-level log message. |
| 79 | + |
| 80 | + The first parameter may be a string or a zero-arity function that returns a |
| 81 | + string. The function is only called if the message is to be generated. |
| 82 | + |
| 83 | + The second parameter is any Elixir term, which is formatted and printed below |
| 84 | + the log message. |
| 85 | + |
| 86 | + ### Example |
| 87 | + |
| 88 | + ~~~ elixir |
| 89 | + error("Database connection lost") |
| 90 | + ~~~ |
| 91 | + """ |
| 92 | + |
| 93 | + defmacro error(msg_or_fun, extra \\ nil), do: maybe_generate(:error, msg_or_fun, extra) |
| 94 | + |
| 95 | + |
| 96 | + defp compile_time_log_level() do |
| 97 | + with sources when is_list(sources) <- Application.get_env(:bunyan, :sources), |
| 98 | + api when is_list(api) <- sources[Bunyan.Source.Api], |
| 99 | + level = api[:compile_time_log_level] |
| 100 | + do |
| 101 | + level |
| 102 | + else |
| 103 | + _ -> if (Mix.env() == :dev), do: :debug, else: :info |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + defp compile_time_log_level_number() do |
| 108 | + Level.of(compile_time_log_level()) |
| 109 | + end |
| 110 | + |
| 111 | + defp maybe_generate(level, msg_or_fun, extra) do |
| 112 | + |
| 113 | + if compile_time_level_not_less_than?(level) do |
| 114 | + quote do |
| 115 | + Bunyan.Source.Api.unquote(level)(unquote(msg_or_fun), unquote(extra)) |
| 116 | + end |
| 117 | + else |
| 118 | + quote do |
| 119 | + _avoid_warning_about_unused_variables = fn -> { unquote(msg_or_fun), unquote(extra) } end |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + defp compile_time_level_not_less_than?(target) do |
| 125 | + compile_time_log_level_number() <= Level.of(target) |
| 126 | + end |
| 127 | + |
| 128 | + end |
| 129 | + |
| 130 | + |
| 1 131 | defmodule Bunyan.Source.Api do |
| 2 132 | |
| 3 133 | alias Bunyan.Shared.{ Level, Readable } |
| @@ -1,57 +0,0 @@ | |
| 1 | - defmodule Bunyan.Source.Api.Injector do |
| 2 | - |
| 3 | - @moduledoc """ |
| 4 | - We want the api functions such as `debug` and `warn` to be available at the |
| 5 | - top level, but at the same time we don't want to couple the top level to |
| 6 | - us. |
| 7 | - |
| 8 | - So the top-level `Bunyan` module checks to see it the application that uses it |
| 9 | - also has us as a dependency. It so, it calls our `inject_into_this_module` |
| 10 | - macro, which adds the necessary calls. |
| 11 | - |
| 12 | - """ |
| 13 | - |
| 14 | - defmacro inject_into_this_module() do |
| 15 | - quote do |
| 16 | - alias Bunyan.Shared.Level |
| 17 | - |
| 18 | - defmacro debug(msg_or_fun, extra \\ nil), do: maybe_generate(:debug, msg_or_fun, extra) |
| 19 | - defmacro info(msg_or_fun, extra \\ nil), do: maybe_generate(:info, msg_or_fun, extra) |
| 20 | - defmacro warn(msg_or_fun, extra \\ nil), do: maybe_generate(:warn, msg_or_fun, extra) |
| 21 | - defmacro error(msg_or_fun, extra \\ nil), do: maybe_generate(:error, msg_or_fun, extra) |
| 22 | - |
| 23 | - |
| 24 | - defp compile_time_log_level() do |
| 25 | - with sources when is_list(sources) <- Application.get_env(:bunyan, :sources), |
| 26 | - api when is_list(api) <- sources[Bunyan.Source.Api], |
| 27 | - level = api[:compile_time_log_level] |
| 28 | - do |
| 29 | - level |
| 30 | - else |
| 31 | - _ -> if (Mix.env() == :dev), do: :debug, else: :info |
| 32 | - end |
| 33 | - end |
| 34 | - |
| 35 | - defp compile_time_log_level_number() do |
| 36 | - Level.of(compile_time_log_level()) |
| 37 | - end |
| 38 | - |
| 39 | - defp maybe_generate(level, msg_or_fun, extra) do |
| 40 | - |
| 41 | - if compile_time_level_not_less_than?(level) do |
| 42 | - quote do |
| 43 | - Bunyan.Source.Api.unquote(level)(unquote(msg_or_fun), unquote(extra)) |
| 44 | - end |
| 45 | - else |
| 46 | - quote do |
| 47 | - _avoid_warning_about_unused_variables = fn -> { unquote(msg_or_fun), unquote(extra) } end |
| 48 | - end |
| 49 | - end |
| 50 | - end |
| 51 | - |
| 52 | - defp compile_time_level_not_less_than?(target) do |
| 53 | - compile_time_log_level_number() <= Level.of(target) |
| 54 | - end |
| 55 | - end |
| 56 | - end |
| 57 | - end |
| @@ -1,6 +1,6 @@ | |
| 1 1 | defmodule Bunyan.Shared.Build do |
| 2 2 | |
| 3 | - @version "0.5.2" |
| 3 | + @version "0.5.3" |
| 4 4 | |
| 5 5 | @moduledoc """ |
| 6 6 | This file is manually included in each Bunyan mix.exs. It provides a |