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-7a97f578-00bf-48bc-a217-c9ca914d84cd@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)

Thanks! I came up with (for now):

```
  @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)) {
      enableLogging();

      // This is triggered when reconfiguring e.g. in set-level running reset
      LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
      ctx.addPropertyChangeListener(
          evt -> {
            if ("config".equals(evt.getPropertyName())) {
              synchronized (lock) {
                enableLogging();
              }
            }
          });
    }
  }

  /** Returns true if a change in state has occurred */
  public boolean enableLogging() {
    synchronized (lock) {
      if (async == null || !async.isStarted()) {
        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration cfg = ctx.getConfiguration();

        if (async != null) {
          async.stop();
          async = null;
        }

        if (text) cfg.addAppender(systemLog.createAsyncAppender(LOG_NAME, new SshLogLayout()));
        if (json)
          cfg.addAppender(
              systemLog.createAsyncAppender(LOG_NAME + JSON_SUFFIX, new SshLogJsonLayout()));

        List<AppenderRef> refList = new ArrayList<>();
        if (text) refList.add(AppenderRef.createAppenderRef(LOG_NAME, null, null));
        if (json) refList.add(AppenderRef.createAppenderRef(LOG_NAME + JSON_SUFFIX, 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();
        return true;
      }
      return false;
    }
  }

```

For number 1 do you mean create a xml file for sshlog to define things? Wouldn't reconfigure just get rid of that, because we use log4j2.xml?

But I'm not sure how to do the recommendations exactly.

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

----
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.