Adobe-Consulting-Services/acs-aem-commons — pull request #230 by adamcin (2014-06-06 00:46:12+00:00)
Title: Added EventLogger service config factory for logging OSGi Events to SLF4j as JSON objects

This EventLogger service provides a configurable bridge between the OSGi Event mechanism and the SLF4j/Logback facility. Events for an arbitrary set of topics matching an arbitrary event filter can be logged to an arbitrary category at an arbitrary log level, in a JSON format. Used in conjunction with the SyslogAppender, the EventLogger allows sysadmins to send a structured OSGi event data stream to an external log aggregator for further post-processing.

For example, create a config at `/apps/system/config/com.adobe.acs.commons.logging.impl.EventLogger-RESOURCE.config` with the following to start sending Resource events as JSON messages to the `osgi.events.resource` logging category at the INFO level:

event.topics=["org/apache/sling/api/resource/Resource/*"]
event.logger.category="osgi.events.resource"
event.filter="(event.topics\=*)"
event.logger.level="INFO"

Then use an Apache Sling Logging Logger configuration to enable logging for that category:

org.apache.sling.commons.log.pattern="{5}"
org.apache.sling.commons.log.names=["osgi.events.resource"]
org.apache.sling.commons.log.file="logs/osgi.events.resource.log"
org.apache.sling.commons.log.level="info"

The log file output will look something like this:

{"path":"/var/eventing/jobs/assigned/376e48ac-b010-4905-8a35-f5413cf6a930/com.day.cq.replication.job.publish/2014/6/4/16/59/com.day.cq.replication.job.publish_376e48ac-b010-4905-8a35-f5413cf6a930_0","userid":"admin","resourceAddedAttributes":["event.job.started.time"],"resourceType":"slingevent:Job","event.topics":"org/apache/sling/api/resource/Resource/CHANGED"}
{"userid":"admin","path":"/var/eventing/jobs/assigned/376e48ac-b010-4905-8a35-f5413cf6a930/com.day.cq.replication.job.publish/2014/6/4/16/59/com.day.cq.replication.job.publish_376e48ac-b010-4905-8a35-f5413cf6a930_0","resourceChangedAttributes":["event.job.retrycount"],"resourceRemovedAttributes":["event.job.started.time"],"resourceType":"slingevent:Job","event.topics":"org/apache/sling/api/resource/Resource/CHANGED"}

Finally, use a SyslogAppender to stream those messages to a remote aggregation server:

suffix.pattern="%logger{36}:\ %msg%n"
port=I"514"
loggers=["osgi.events.resource"]
host="127.0.0.1"