Class NtfyLog4j2Installer

java.lang.Object
io.github.pimak.ntfy.log4j2.NtfyLog4j2Installer

public final class NtfyLog4j2Installer extends Object
Programmatic one-liner install of the ntfy log4j2 appender from the ambient environment: NtfyLog4j2Installer.install() early in main attaches a started NtfyLog4j2Appender to the root logger at Level.ERROR when the environment configures ntfy (NTFY_URL/NTFY_TOPIC, -Dntfy.*, or a trusted classpath ntfy.properties — see Log4j2AmbientConfig for the classpath-endpoint opt-in guard), and does nothing otherwise.

Prefer the declarative <Ntfy .../> element in log4j2.xml when you already own the logging configuration; this entry point exists for applications that configure ntfy purely from the environment, and for the Spring Boot starter's log4j2 branch.

The install also registers a NtfyLog4j2ReattachListener so a later Configurator.reconfigure() (or a watched log4j2.xml being reloaded) cannot silently drop the appender.

The returned appender owns its engine: call uninstall(LoggerContext, NtfyLog4j2Appender) on shutdown to flush any pending digest, drain the async queue and release the HTTP client. log4j2's own shutdown hook stops the configuration — and with it this appender — on normal JVM exit.

  • Method Details

    • install

      public static Optional<NtfyLog4j2Appender> install()
      Installs on the current LoggerContext (LoggerContext.getContext(false) — the caller's context, without forcing initialization of a new one). See install(LoggerContext).
      Returns:
      the installed appender, or empty when nothing was installed
    • install

      public static Optional<NtfyLog4j2Appender> install(org.apache.logging.log4j.core.LoggerContext context)
      Resolves the ambient config and, when active and vetted, attaches a started NtfyLog4j2Appender to context's root logger at Level.ERROR.

      Empty — with nothing attached — when ntfy is not configured, the classpath-endpoint guard refused, the engine declined to activate (e.g. strict requireHttpsForCredentials), or resolution/construction failed. Like the JUL installer, this entry point never lets an alerting-setup failure propagate into the caller; every failure is reported on log4j2's StatusLogger instead.

      Parameters:
      context - the logger context to install into
      Returns:
      the installed appender, or empty when nothing was installed
    • uninstall

      public static void uninstall(org.apache.logging.log4j.core.LoggerContext context, NtfyLog4j2Appender appender)
      Reverses install(LoggerContext): deregisters the reconfiguration listener, detaches the appender from the root logger by name, refreshes the loggers and stops the appender (which flushes any pending digest and drains the async queue). Safe to call on an appender that was never installed; safe to call twice.
      Parameters:
      context - the logger context the appender was installed into
      appender - the appender returned by install