[erlang-questions] Architecture question: logging inside library

Christoffer Vikström cvi@REDACTED
Mon Jan 11 14:29:18 CET 2016


I would suggest using a hook system, where you can register hooks (funs) at
important points in the code. That way you can provide means to do both
logging and instrumentation. Something like:

library:add_hook(pre_request, fun(Request) ->
    leg:dbg("request: ~p~n", [Request]),
    folsom_metrics:notify(requests, {inc, 1})
end).

Be careful with any performance impacts this might have though.

-cvik

On Mon, Jan 11, 2016 at 2:07 PM Chandru <
chandrashekhar.mullaparthi@REDACTED> wrote:

> Have a log function such as this inside your library code.
>
> log_msg(Fmt, Args) ->
>     log_msg(application:get_env(my_library, logging_mf), Fmt, Args).
>
> log_msg({ok, {Mod, Fun}}, Fmt, Args) when is_atom(Mod), is_atom(Fun) ->
>     catch apply(Mod, Fun, [Fmt, Args]);
> log_msg(_, _, _) ->
>     ok.
>
> That way, the user can control how the messages get logged.
>
> cheers,
> Chandru
>
>
> On 11 January 2016 at 07:34, Max Lapshin <max.lapshin@REDACTED> wrote:
>
>> Hi.
>>
>> As we know, it is good when library doesn't have any hardcoded
>> dependencies at all.
>>
>> I don't understand what is a good way to remove lager (or any other
>> logging library) from library dependencies.
>>
>>
>> For example, we have an mpegts decoder/encoder library. This library
>> takes binary and unpacks it to list of frames or packs back:
>>
>> mpegts_decoder:decode(Binary,  mpegts_decoder:init(Options)) ->  {ok,
>> Frames, State}
>>
>>
>> There are calls to lager in some places of code that are used for
>> indicating some statuses:
>>
>> dump_psi(Decoder, NewPMT),
>> lager:info("PMT: ~p", [Descriptors]),
>> ...
>>
>>
>> What is the proper way to refuse from adding logger as a dependency for
>> such pure library?
>> Pass a callback into  initialisation options?
>>
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160111/7355743f/attachment.htm>


More information about the erlang-questions mailing list