Class JsonInvocationEventEmitter

java.lang.Object
com.fayupable.logged.spring.emitter.JsonInvocationEventEmitter
All Implemented Interfaces:
InvocationEventEmitter

public class JsonInvocationEventEmitter extends Object implements InvocationEventEmitter
InvocationEventEmitter that writes each event as a single line of structured JSON through SLF4J, instead of the human-readable line produced by Slf4jInvocationEventEmitter.

This exists for applications that ship their logs to a structured backend (Loki, Elasticsearch, Datadog, and similar) that parses each log line as JSON. Emitting Slf4jInvocationEventEmitter's prose line into such a backend either fails to parse or gets indexed as one opaque string field, losing the ability to filter or aggregate on individual fields (for example querying every failed call for a given className). This emitter writes every MethodInvocationEvent field as its own JSON property instead, so the backend's own JSON parser indexes them directly.

No JSON library is used or required: every field on MethodInvocationEvent is a primitive, a String, or an Instant, so a small hand-written writer is enough and keeps this class dependency-free, consistent with the rest of this library. String fields are escaped for quotes, backslashes, and control characters before being written, since className, exception type names, and caller identity ultimately originate from application code or an authenticated principal, not arbitrary user input — but escaping them anyway costs nothing and guarantees the emitted line is always valid JSON.

As with Slf4jInvocationEventEmitter, successful calls are logged at INFO and failed calls at WARN, and the exception message is never included, only its simple class name.

  • Constructor Details

    • JsonInvocationEventEmitter

      public JsonInvocationEventEmitter()
  • Method Details

    • emit

      public void emit(MethodInvocationEvent event)
      Description copied from interface: InvocationEventEmitter
      Publishes the given invocation event to whatever sink this implementation targets.

      Implementations must not throw checked or unchecked exceptions that would propagate back into the intercepted method call. Emission failures (for example a logging backend being unavailable) should be handled internally by the implementation.

      Specified by:
      emit in interface InvocationEventEmitter
      Parameters:
      event - the invocation event to publish, never null