pekko/akka-docs/general/event-handler.rst

102 lines
2.9 KiB
ReStructuredText
Raw Normal View History

.. _event-handler:
2011-04-09 19:55:46 -06:00
Event Handler
=============
There is an Event Handler which takes the place of a logging system in Akka:
.. code-block:: scala
akka.event.EventHandler
You can configure which event handlers should be registered at boot time. That is done using the 'event-handlers' element in
the :ref:`configuration`. Here you can also define the log level.
2011-04-09 19:55:46 -06:00
.. code-block:: ruby
akka {
2011-04-26 21:42:11 +02:00
# event handlers to register at boot time (EventHandler$DefaultListener logs to STDOUT)
event-handlers = ["akka.event.EventHandler$DefaultListener"]
loglevel = "DEBUG" # Options: ERROR, WARNING, INFO, DEBUG
2011-04-09 19:55:46 -06:00
}
The default one logs to STDOUT and is registered by default. It is not intended to be used for production. There is also an :ref:`slf4j` event handler available in the 'akka-slf4j' module.
2011-04-09 19:55:46 -06:00
Example of creating a listener from Scala (from Java you just have to create an 'UntypedActor' and create a handler for these messages):
.. code-block:: scala
val errorHandlerEventListener = Actor.actorOf(Props(new Actor {
2011-04-09 19:55:46 -06:00
self.dispatcher = EventHandler.EventHandlerDispatcher
def receive = {
case EventHandler.Error(cause, instance, message) => ...
case EventHandler.Warning(instance, message) => ...
case EventHandler.Info(instance, message) => ...
case EventHandler.Debug(instance, message) => ...
case genericEvent => ...
}
})
To add the listener:
.. code-block:: scala
EventHandler.addListener(errorHandlerEventListener)
To remove the listener:
.. code-block:: scala
EventHandler.removeListener(errorHandlerEventListener)
To log an event:
.. code-block:: scala
EventHandler.notify(EventHandler.Error(exception, this, message))
EventHandler.notify(EventHandler.Warning(this, message))
EventHandler.notify(EventHandler.Info(this, message))
EventHandler.notify(EventHandler.Debug(this, message))
EventHandler.notify(object)
You can also use one of the direct methods (for a bit better performance):
.. code-block:: scala
EventHandler.error(exception, this, message)
EventHandler.error(this, message)
EventHandler.warning(this, message)
EventHandler.info(this, message)
EventHandler.debug(this, message)
The event handler allows you to send an arbitrary object to the handler which you can handle in your event handler listener. The default listener prints it's toString String out to STDOUT.
.. code-block:: scala
EventHandler.notify(anyRef)
The methods take a call-by-name parameter for the message to avoid object allocation and execution if level is disabled. The following formatting function will not be evaluated if level is INFO, WARNING, or ERROR.
.. code-block:: scala
EventHandler.debug(this, "Processing took %s ms".format(duration))
From Java you need to nest the call in an if statement to achieve the same thing.
2011-04-26 21:42:11 +02:00
.. code-block:: java
if (EventHandler.isDebugEnabled()) {
EventHandler.debug(this, String.format("Processing took %s ms", duration));
}
2011-04-09 19:55:46 -06:00