Re: [D] Help with migrating from log4j to log4j2 (we also use a slf4j -> log4j) [logging-log4j2]

paladox (via GitHub) <[email protected]>
Newsgroups gmane.comp.jakarta.log4j.devel
Message-ID <ghd-D_kwDOAKJSSM4AhuMF-b8479293-71ae-40d1-a29e-fdd331e1353c@gitbox.apache.org>
GitHub user paladox added a comment to the discussion: Help with migrating from log4j to log4j2 (we also use a slf4j -> log4j)

What about something like:

```java
  private static final AtomicBoolean reconfiguring = new AtomicBoolean(false);

  @Inject
  SshLog(
      final Provider<SshSession> session,
      final Provider<Context> context,
      SystemLog systemLog,
      @GerritServerConfig Config config,
      LogConfig logConfig,
      GroupAuditService auditService) {
    this.session = session;
    this.context = context;
    this.auditService = auditService;
    this.systemLog = systemLog;
    this.json = logConfig.isJsonLogging();
    this.text = logConfig.isTextLogging();

    if (config.getBoolean("sshd", "requestLog", true)) {
      initLogger();

      // Prevent stack overflow on Log4j reconfiguration
      LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
      ctx.addPropertyChangeListener(
          evt -> {
            if ("config".equals(evt.getPropertyName())) {
              initLogger();
            }
          });
    }
  }

  /** Initialize the SSH logger appenders safely */
  private void initLogger() {
    synchronized (lock) {
      if (reconfiguring.get()) return;
      reconfiguring.set(true);
      try {
        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration cfg = ctx.getConfiguration();

        // Stop previous appender if exists
        if (async != null) {
          async.stop();
          async = null;
        }

        List<AppenderRef> refList = new ArrayList<>();

        if (text) {
          String name = LOG_NAME;
          if (!cfg.getAppenders().containsKey(name)) {
            cfg.addAppender(systemLog.createAsyncAppender(name, new SshLogLayout()));
          }
          refList.add(AppenderRef.createAppenderRef(name, null, null));
        }

        if (json) {
          String name = LOG_NAME + JSON_SUFFIX;
          if (!cfg.getAppenders().containsKey(name)) {
            cfg.addAppender(systemLog.createAsyncAppender(name, new SshLogJsonLayout()));
          }
          refList.add(AppenderRef.createAppenderRef(name, null, null));
        }

        AppenderRef[] refs = refList.toArray(new AppenderRef[0]);

        async =
            AsyncAppender.newBuilder()
                .setName("SshAsync")
                .setAppenderRefs(refs)
                .setConfiguration(cfg)
                .build();

        async.start();
        cfg.addAppender(async);
        ctx.updateLoggers();
      } finally {
        reconfiguring.set(false);
      }
    }
  }
```

GitHub link: https://github.com/apache/logging-log4j2/discussions/3914#discussioncomment-14332921

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.