Github user rnewson commented on a diff in the pull request:
https://github.com/apache/couchdb-couch-log/pull/4#discussion_r39839167
--- Diff: src/couch_log.erl ---
@@ -12,41 +12,108 @@
-module(couch_log).
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+-endif.
+
-export([debug/2, info/2, notice/2, warning/2, error/2, critical/2,
alert/2, emergency/2]).
-export([set_level/1]).
+-export([behaviour_info/1]).
+
+behaviour_info(callbacks) ->
+ [{debug, 2}, {info, 2}, {notice, 2}, {warning, 2},
+ {error, 2}, {critical, 2}, {alert, 2}, {set_level, 1}];
+behaviour_info(_) ->
+ undefined.
+
+-spec debug(string(), list()) -> ok.
debug(Fmt, Args) ->
+ {ok, Backend} = get_backend(),
catch couch_stats:increment_counter([couch_log, level, debug]),
- lager:debug(Fmt, Args).
+ Backend:debug(Fmt, Args).
+-spec info(string(), list()) -> ok.
info(Fmt, Args) ->
+ {ok, Backend} = get_backend(),
catch couch_stats:increment_counter([couch_log, level, info]),
- lager:info(Fmt, Args).
+ Backend:info(Fmt, Args).
+-spec notice(string(), list()) -> ok.
notice(Fmt, Args) ->
+ {ok, Backend} = get_backend(),
catch couch_stats:increment_counter([couch_log, level, notice]),
- lager:notice(Fmt, Args).
+ Backend:notice(Fmt, Args).
+-spec warning(string(), list()) -> ok.
warning(Fmt, Args) ->
+ {ok, Backend} = get_backend(),
catch couch_stats:increment_counter([couch_log, level, warning]),
- lager:warning(Fmt, Args).
+ Backend:warning(Fmt, Args).
+-spec error(string(), list()) -> ok.
error(Fmt, Args) ->
+ {ok, Backend} = get_backend(),
catch couch_stats:increment_counter([couch_log, level, 'error']),
- lager:error(Fmt, Args).
+ Backend:error(Fmt, Args).
+-spec critical(string(), list()) -> ok.
critical(Fmt, Args) ->
+ {ok, Backend} = get_backend(),
catch couch_stats:increment_counter([couch_log, level, critical]),
- lager:critical(Fmt, Args).
+ Backend:critical(Fmt, Args).
+-spec alert(string(), list()) -> ok.
alert(Fmt, Args) ->
+ {ok, Backend} = get_backend(),
catch couch_stats:increment_counter([couch_log, level, alert]),
- lager:alert(Fmt, Args).
+ Backend:alert(Fmt, Args).
+-spec emergency(string(), list()) -> ok.
emergency(Fmt, Args) ->
+ {ok, Backend} = get_backend(),
catch couch_stats:increment_counter([couch_log, level, emergency]),
- lager:emergency(Fmt, Args).
+ Backend:emergency(Fmt, Args).
+-spec set_level(atom()) -> ok.
set_level(Level) ->
- {ok, Handlers} = application:get_env(lager, handlers),
- [lager:set_loglevel(Handler, Level) || {Handler, _} <- Handlers].
+ {ok, Backend} = get_backend(),
+ Backend:set_level(Level).
+
+-spec get_backend() -> {ok, atom()}.
+get_backend() ->
+ application:get_env(?MODULE, backend).
+
+-ifdef(TEST).
+
+callbacks_test_() ->
+ {setup,
+ fun setup/0,
+ fun cleanup/1,
+ [
+ ?_assertEqual({ok, couch_log_stderr}, get_backend()),
+ ?_assertEqual(ok, debug("message", [])),
+ ?_assertEqual(ok, info("message", [])),
+ ?_assertEqual(ok, notice("message", [])),
+ ?_assertEqual(ok, warning("message", [])),
+ ?_assertEqual(ok, error("message", [])),
+ ?_assertEqual(ok, critical("message", [])),
+ ?_assertEqual(ok, alert("message", [])),
+ ?_assertEqual(ok, emergency("message", [])),
+ ?_assertEqual(ok, set_level(info))
+ ]
+ }.
+
+setup() ->
+ ok = application:start(folsom),
--- End diff --
it would be better to mock out couch_stats:increment_counter here.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---