Skip to content

Commit

Permalink
Migrate from lager to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
silviucpp committed Jan 18, 2023
1 parent 87d99a1 commit d6dbc28
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 71 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
### Changelog:

##### v4.1.0

- Migrate from lager to OTP logger.

##### v4.0.9

- Add support for `cloud_secure_connection_bundle`
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ application:start(erlcass).

### Setting the log level

`Erlcass` is using `lager` for logging the errors. Beside the fact that you can set in lager the desired log level,
`Erlcass` is using OTP `logger` for logging the errors. Beside the fact that you can set in logger the desired log level,
for better performances it's better to set also in `erlcass` the desired level otherwise there will be a lot of
resources consumed by lager to format the messages and then drop them. Also the native driver performances can be
affected because of the time spent in generating the logs and sending them from C++ into Erlang.
resources consumed for messages that are going to be dropped anyway. Also the native driver performances can decrease
because of the time spent in generating the logs and sending them from C++ into Erlang.

Available Log levels are:

```erlang
-define(CASS_LOG_DISABLED, 0).
-define(CASS_LOG_CRITICAL, 1).
-define(CASS_LOG_ERROR, 2).
-define(CASS_LOG_WARN, 3). (default)
-define(CASS_LOG_WARN, 3). % default
-define(CASS_LOG_INFO, 4).
-define(CASS_LOG_DEBUG,5).
-define(CASS_LOG_TRACE, 6).
Expand Down
26 changes: 5 additions & 21 deletions include/erlcass_internals.hrl
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
% data types

-type reason() :: term().

% logs

-define(PRINT_MSG(Format, Args),
io:format(Format, Args)).

-define(DEBUG_MSG(Format, Args),
lager:debug(Format, Args)).

-define(INFO_MSG(Format, Args),
lager:info(Format, Args)).

-define(WARNING_MSG(Format, Args),
lager:warning(Format, Args)).

-define(ERROR_MSG(Format, Args),
lager:error(Format, Args)).

-define(CRITICAL_MSG(Format, Args),
lager:critical(Format, Args)).
-include_lib("kernel/include/logger.hrl").

% timeouts

-define(RESPONSE_TIMEOUT, 20000).
-define(CONNECT_TIMEOUT, 5000).

% data types

-type reason() :: term().
7 changes: 2 additions & 5 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@

{project_plugins, [rebar3_hex]}.

{deps, [
{lager, "3.9.2"}
]}.
{deps, []}.

{erl_opts, [
warnings_as_errors,
warn_export_all,
{parse_transform, lager_transform}
warn_export_all
]}.

{profiles, [
Expand Down
12 changes: 0 additions & 12 deletions rebar.config.script

This file was deleted.

12 changes: 1 addition & 11 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
{"1.2.0",
[{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1},
{<<"lager">>,{pkg,<<"lager">>,<<"3.9.2">>},0}]}.
[
{pkg_hash,[
{<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>},
{<<"lager">>, <<"4CAB289120EB24964E3886BD22323CB5FEFE4510C076992A23AD18CF85413D8C">>}]},
{pkg_hash_ext,[
{<<"goldrush">>, <<"99CB4128CFFCB3227581E5D4D803D5413FA643F4EB96523F77D9E6937D994CEB">>},
{<<"lager">>, <<"7F904D9E87A8CB7E66156ED31768D1C8E26EBA1D54F4BC85B1AA4AC1F6340C28">>}]}
].
[].
7 changes: 5 additions & 2 deletions src/erlcass.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
{description, "ErlCass - Erlang Cassandra Driver"},
{licenses, ["MIT"]},
{links,[{"Github","https://github.com/silviucpp/erlcass"}]},
{vsn, "4.0.9"},
{vsn, "4.1.0"},
{registered, []},
{applications, [kernel, stdlib, lager]},
{applications, [
kernel,
stdlib
]},
{mod, {erlcass_app, []}},
{env, []},

Expand Down
18 changes: 9 additions & 9 deletions src/erlcass.erl
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,12 @@ handle_call(get_metrics, _From, #state{session = Session} = State) ->
{reply, erlcass_nif:cass_session_get_metrics(Session), State}.

handle_cast(Request, State) ->
?ERROR_MSG("session ~p received unexpected cast: ~p", [self(), Request]),
?LOG_ERROR("session ~p received unexpected cast: ~p", [self(), Request]),
{noreply, State}.

handle_info({prepared_statement_result, Result, {From, Identifier, Query}}, #state{session = Session} = State) ->

?INFO_MSG("session: ~p prepared statement id: ~p result: ~p", [self(), Identifier, Result]),
?LOG_INFO("session: ~p prepared statement id: ~p result: ~p", [self(), Identifier, Result]),

case Result of
{ok, StmRef} ->
Expand All @@ -380,18 +380,18 @@ handle_info({prepared_statement_result, Result, {From, Identifier, Query}}, #sta
{noreply, State};

handle_info(Info, State) ->
?ERROR_MSG("session ~p received unexpected message: ~p", [self(), Info]),
?LOG_ERROR("session ~p received unexpected message: ~p", [self(), Info]),
{noreply, State}.

terminate(Reason, #state {session = Session}) ->
Self = self(),
?INFO_MSG("closing session ~p with reason: ~p", [Self, Reason]),
?LOG_INFO("closing session ~p with reason: ~p", [Self, Reason]),

case do_close(Session, Self, ?CONNECT_TIMEOUT) of
ok ->
?INFO_MSG("session ~p closed completed", [Self]);
?LOG_INFO("session ~p closed completed", [Self]);
Error ->
?ERROR_MSG("session ~p closed with error: ~p", [Self, Error])
?LOG_ERROR("session ~p closed with error: ~p", [Self, Error])
end.

code_change(_OldVsn, State, _Extra) ->
Expand Down Expand Up @@ -437,11 +437,11 @@ session_create() ->
ok ->
receive
{session_connected, Self, Result} ->
?INFO_MSG("session ~p connection complete result: ~p", [Self, Result]),
?LOG_INFO("session ~p connection complete result: ~p", [Self, Result]),
{ok, Session}

after ?CONNECT_TIMEOUT ->
?ERROR_MSG("session ~p connection timeout", [Self]),
?LOG_ERROR("session ~p connection timeout", [Self]),
{error, connect_session_timeout}
end;
Error ->
Expand All @@ -461,7 +461,7 @@ session_prepare_cached_statements(SessionRef) ->
receive
{prepared_statement_result, Result, Tag} ->

?INFO_MSG("session ~p prepared cached statement id: ~p result: ~p", [Self, Identifier, Result]),
?LOG_INFO("session ~p prepared cached statement id: ~p result: ~p", [Self, Identifier, Result]),

case Result of
{ok, StmRef} ->
Expand Down
10 changes: 5 additions & 5 deletions src/erlcass_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ loop() ->
log_message(#log_msg{message = Msg, function = Fun, file = File, line = Line, severity = Severity}) ->
case Severity of
?CASS_LOG_CRITICAL ->
?CRITICAL_MSG(?MSG_FORMAT, [Msg, Fun, File, Line]);
?LOG_CRITICAL(?MSG_FORMAT, [Msg, Fun, File, Line]);
?CASS_LOG_ERROR ->
?ERROR_MSG(?MSG_FORMAT, [Msg, Fun, File, Line]);
?LOG_ERROR(?MSG_FORMAT, [Msg, Fun, File, Line]);
?CASS_LOG_WARN ->
?WARNING_MSG(?MSG_FORMAT, [Msg, Fun, File, Line]);
?LOG_WARNING(?MSG_FORMAT, [Msg, Fun, File, Line]);
?CASS_LOG_INFO ->
?INFO_MSG(?MSG_FORMAT, [Msg, Fun, File, Line]);
?LOG_INFO(?MSG_FORMAT, [Msg, Fun, File, Line]);
_ ->
?DEBUG_MSG(?MSG_FORMAT, [Msg, Fun, File, Line])
?LOG_DEBUG(?MSG_FORMAT, [Msg, Fun, File, Line])
end.
4 changes: 2 additions & 2 deletions src/erlcass_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

load_nif() ->
SoName = get_priv_path(?MODULE),
?INFO_MSG("loading library: ~p ~n", [SoName]),
?LOG_INFO("loading library: ~p ~n", [SoName]),
ok = erlang:load_nif(SoName, 0).

get_priv_path(File) ->
Expand Down Expand Up @@ -154,4 +154,4 @@ cass_date_from_epoch(_EpochSecs) ->
?NOT_LOADED.

cass_date_time_to_epoch(_Date, _Time) ->
?NOT_LOADED.
?NOT_LOADED.

0 comments on commit d6dbc28

Please sign in to comment.