Class Slf4jCollector

java.lang.Object
io.kubfly.sdk.slf4j.Slf4jCollector
All Implemented Interfaces:
Collector

public final class Slf4jCollector extends Object implements Collector
The bridge to SLF4J (bridge mode): events are formatted (with the one-off tags of the event and the scope frame with its tags appended) and passed to the SLF4J logger named by the event path project/module/area/system, so the bound provider (for example Logback) decides levels and output. The one-off tags of an event also go to SLF4J as key-value pairs, for structured output. FATAL goes to SLF4J error. Thresholds follow the SLF4J logger configuration at the time of Collectors.refresh(). Alerts are logged only when the state of a metric changes, to the logger of its project/module/area/system: CRITICAL to error, WARNING to warn, SILENCE as heavy as Alert.silence(), a return to OK to info. Health checks likewise, when the state of a check changes: CRITICAL to error and WARNING to warn with the message and the exception, a return to OK to info.

Events of sdk/"slf4j" came from the SLF4J provider — KubFlyAppender imports the logs of the application — so the bridge neither logs them back nor widens their levels: each line is printed once, and nothing loops.

Found by ServiceLoader with this module; a bridge — the first collector — only when SLF4J is bound to a provider other than KubFly (Logback); without one (the NOP provider) it wants nothing.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The module of events imported from the SLF4J provider, sdk/slf4j/<package>/<class> (decision 90): the bridge skips them.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates the bridge.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    alert(long id, long begin, long end, Metric metric, Severity severity, long warnings, long criticals, long unassessed)
    Logs the alert state of a metric when it changes (the first OK is not a change).
    void
    begin(long id, long time, Frame frame)
    Logs the begin of a frame at TRACE.
    boolean
    A bridge while a foreign provider is bound.
    boolean
    On only with a provider of SLF4J other than KubFly itself: without one there is nobody to print to.
    void
    end(long id, long time, long duration, long threadId, String threadName, Frame frame, Throwable exception)
    Logs the end of a frame at TRACE: the duration, and the exception on a failure.
    void
    event(long id, long time, long threadId, String threadName, Frame frame, Event event, String message, ArgumentKind kind1, long bits1, Object ref1, ArgumentKind kind2, long bits2, Object ref2, Throwable exception)
    Receives one event.
    void
    finish(long id, long time, Environment environment)
    Writes the finish of the process, as an INFO line of the SLF4J logger sdk/api/environment, with the largest number given — if the SLF4J provider still writes at the shutdown of the JVM.
    boolean
    frames(String project, String module, String area, String system, String component)
    Frames are logged at TRACE when it is enabled for the logger of the scope.
    void
    health(long id, long time, long duration, HealthCheck check, Health health)
    Logs a health check when its state changes, to the logger of its project/module/area/system.
    void
    metrics(long id, long begin, long end, Snapshot[] snapshots)
    SLF4J has no metrics: this bridge ignores them.
    void
    profile(long id, long begin, long end, Profile profile)
    Not logged: the profiles are not events of the application.
    void
    start(long id, long time, Environment environment)
    Writes the start of the process, as an INFO line of the SLF4J logger sdk/api/environment, with the environment: where and on what the application runs belongs to ordinary logs as well.
    threshold(String project, String module, String area, String system)
    The most verbose level this collector wants from a project/module/area/system path (Level.INFO — info and everything more severe).
    void
    traffic(long id, long begin, long end, Edge[] edges)
    Not logged: the links are not events of the application.
    void
    usage(long id, long time, Usage usage)
    SLF4J has no place for usage snapshots: this bridge ignores them.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface Collector

    eventN, track
  • Field Details

    • IMPORTED

      public static final String IMPORTED
      The module of events imported from the SLF4J provider, sdk/slf4j/<package>/<class> (decision 90): the bridge skips them.
      See Also:
  • Constructor Details

    • Slf4jCollector

      public Slf4jCollector()
      Creates the bridge.
  • Method Details

    • enabled

      public boolean enabled()
      On only with a provider of SLF4J other than KubFly itself: without one there is nobody to print to.
      Specified by:
      enabled in interface Collector
      Returns:
      whether the bridge works
    • bridge

      public boolean bridge()
      A bridge while a foreign provider is bound.
      Specified by:
      bridge in interface Collector
      Returns:
      true if SLF4J is bound to a provider other than KubFly
    • frames

      public boolean frames(String project, String module, String area, String system, String component)
      Frames are logged at TRACE when it is enabled for the logger of the scope.
      Specified by:
      frames in interface Collector
      Parameters:
      project - the first level of the scope path
      module - the second path level: a module of the build of the project
      area - the third level
      system - the fourth level
      component - the fifth level
      Returns:
      whether TRACE is enabled for project/module/area/system
    • threshold

      public Level threshold(String project, String module, String area, String system)
      The most verbose level this collector wants from a project/module/area/system path (Level.INFO — info and everything more severe). Asked when a KubFly of the path is first created and after Collectors.refresh().
      Specified by:
      threshold in interface Collector
      Parameters:
      project - the first level of the path
      module - the second path level: a module of the build of the project
      area - the third level
      system - the fourth level
      Returns:
      the threshold; Level.OFF — nothing from this path
    • start

      public void start(long id, long time, Environment environment)
      Writes the start of the process, as an INFO line of the SLF4J logger sdk/api/environment, with the environment: where and on what the application runs belongs to ordinary logs as well.
      Specified by:
      start in interface Collector
      Parameters:
      id - always 0
      time - the time
      environment - the environment
    • begin

      public void begin(long id, long time, Frame frame)
      Logs the begin of a frame at TRACE.
      Specified by:
      begin in interface Collector
      Parameters:
      id - the number of the beginning: that of the frame, frame.id()
      time - the begin time
      frame - the frame
    • event

      public void event(long id, long time, long threadId, String threadName, Frame frame, Event event, String message, ArgumentKind kind1, long bits1, Object ref1, ArgumentKind kind2, long bits2, Object ref2, Throwable exception)
      Receives one event.

      Called synchronously on the application thread that logs — including millions of virtual threads — only when this collector's Collector.threshold(String, String, String, String) lets the level through: the implementation must be thread-safe, must not block and should not allocate. The message is a template, never formatted on the way here; arguments arrive as raw values without boxing — a primitive in bits, a reference in ref, its kind in kind. id, time and frame are the same for all collectors.

      References are valid only during the call. After it returns, the application may change the objects it passed — the data of a Blob or an Image is not copied on the way. A collector that stores the event later (in another thread, after a buffer switch) must copy what it needs before returning.

      Specified by:
      event in interface Collector
      Parameters:
      id - the event number: unique within the JVM process, the same for all collectors; it grows in call order within a thread, not dense, no order across threads within one millisecond
      time - the event time (System.currentTimeMillis()), the same for all collectors
      threadId - the thread that logs (Thread.threadId()), the same for all collectors
      threadName - the name of the thread (Thread.getName(); "" — a thread without a name, as a virtual thread by default)
      frame - the frame of the running Scope (tags, call tree) or null outside any scope
      event - the event (project/module/area/system path, level and its one-off tags); the identity of the call site is Event.gate() and Event.level() — a tagged event is a new instance per call, so a collector keys its own per-site data by them, not by this reference; a tag of the event shadows a tag of the frame with the same key
      message - the message template with up to two {} placeholders
      kind1 - the kind of the first argument (ArgumentKind.NONE — no argument)
      bits1 - the first argument if it is a primitive (ArgumentKind), otherwise 0
      ref1 - the first argument if it is a reference, otherwise null
      kind2 - the kind of the second argument
      bits2 - the second argument if it is a primitive, otherwise 0
      ref2 - the second argument if it is a reference, otherwise null
      exception - the event exception or null
    • end

      public void end(long id, long time, long duration, long threadId, String threadName, Frame frame, Throwable exception)
      Logs the end of a frame at TRACE: the duration, and the exception on a failure.
      Specified by:
      end in interface Collector
      Parameters:
      id - the number of the end
      time - the end time
      duration - the duration in nanoseconds
      threadId - the thread (Thread.threadId())
      threadName - the name of the thread (Thread.getName())
      frame - the frame
      exception - the exception or null
    • usage

      public void usage(long id, long time, Usage usage)
      SLF4J has no place for usage snapshots: this bridge ignores them.
      Specified by:
      usage in interface Collector
      Parameters:
      id - the snapshot number
      time - the snapshot time
      usage - the snapshot
    • metrics

      public void metrics(long id, long begin, long end, Snapshot[] snapshots)
      SLF4J has no metrics: this bridge ignores them.
      Specified by:
      metrics in interface Collector
      Parameters:
      id - the number of the interval
      begin - the beginning of the interval
      end - the interval end
      snapshots - the aggregates
    • alert

      public void alert(long id, long begin, long end, Metric metric, Severity severity, long warnings, long criticals, long unassessed)
      Logs the alert state of a metric when it changes (the first OK is not a change).
      Specified by:
      alert in interface Collector
      Parameters:
      id - the number of the alert
      begin - the beginning of the interval
      end - the interval end
      metric - the metric
      severity - the state
      warnings - the observations beyond a warning threshold
      criticals - the observations beyond a critical threshold
      unassessed - the observations that could not be assessed
    • traffic

      public void traffic(long id, long begin, long end, Edge[] edges)
      Not logged: the links are not events of the application.
      Specified by:
      traffic in interface Collector
      Parameters:
      id - the number of the interval of the traffic
      begin - the beginning of the interval
      end - the interval end
      edges - the edges
    • profile

      public void profile(long id, long begin, long end, Profile profile)
      Not logged: the profiles are not events of the application.
      Specified by:
      profile in interface Collector
      Parameters:
      id - the number of the profile
      begin - the beginning of the interval
      end - the interval end
      profile - the profile
    • health

      public void health(long id, long time, long duration, HealthCheck check, Health health)
      Logs a health check when its state changes, to the logger of its project/module/area/system.
      Specified by:
      health in interface Collector
      Parameters:
      id - the number of the result
      time - the time of the round
      duration - the duration of the run, in nanoseconds
      check - the check
      health - the result
    • finish

      public void finish(long id, long time, Environment environment)
      Writes the finish of the process, as an INFO line of the SLF4J logger sdk/api/environment, with the largest number given — if the SLF4J provider still writes at the shutdown of the JVM.
      Specified by:
      finish in interface Collector
      Parameters:
      id - the largest number given
      time - the time
      environment - the environment with its finish set