[erlang-questions] Architecture question: logging inside library

Jay Nelson jay@REDACTED
Thu Jan 14 18:24:21 CET 2016


The OTP gen_event behaviour is often overlooked by the OSS
community when specifying erlang system architectures. It allows
you to dynamically install handlers for notifications, and is
especially useful as an organizing construct when side-effects
may vary in different situations. At unit testing time you can
replace handlers with collectors and verify that data is flowing
properly.

As earlier posters commented, try to avoid side-effects and use
tracing when you temporarily want to watch values (don’t leave
tracing on all the time in production for logging), but if you have
specific places you want to periodically spy on, you can use
gen_event:notify/2 and not care whether a callback is installed
or not. Your callback can later be installed dynamically and it
can even forward to other nodes, or you can use distributed
erlang to notify a gen_event running on another node.

Tips:
  - Keep your event handlers short.
  - Pass on any computation to another process.
  - Use meaningful semantic names where notify happens.

When providing a library, you allow the integrator to do late
binding on the logging utility. The dependency is bundled with
the handler manager, not the base library.

jay




More information about the erlang-questions mailing list