You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@logging.apache.org by "voddan (via GitHub)" <gi...@apache.org> on 2025/03/26 13:11:56 UTC

[D] How do I register a StatusListener in configuration? [logging-log4j2]

GitHub user voddan created a discussion: How do I register a StatusListener in configuration?

Hi, 
Documentation on [StatusLogger](https://logging.apache.org/log4j/2.x/javadoc/log4j-api/org/apache/logging/log4j/status/StatusLogger.html) mentions that I can create a custom StatusListener and register it.

How do I do that? 
I could not find a cmd parameter or a configuration that would do that. The example in the documentation is in Java, but I don't understand where I would even put that code

I have a Spring 5 Java application, with Tomcat, developed on Windows. We use log4j.xml and command line parameters, for example -Dlog4j2.debug, to configure logging.

Changing the code of the app is out of the question, but I could add a separate Maven module if I have to build a plugin or something of this nature.

GitHub link: https://github.com/apache/logging-log4j2/discussions/3576

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org


Re: [D] How do I register a StatusListener in configuration? [logging-log4j2]

Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
GitHub user ppkarwasz added a comment to the discussion: How do I register a StatusListener in configuration?

We could probably register status listeners automatically using `ServiceLoader`, which is in practice the only way to ensure that they are registered **before** `StatusLogger` is used for the first time. The behavior could be similar to a JUnit [`LauncherSessionListener`](https://junit.org/junit5/docs/current/user-guide/#launcher-api-launcher-session-listeners-custom).

Of course for security reasons, there should also be a configuration property to turn on the auto-registration. Personally, I am open to a PR in this direction.

GitHub link: https://github.com/apache/logging-log4j2/discussions/3576#discussioncomment-12644421

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org


Re: [D] How do I register a StatusListener in configuration? [logging-log4j2]

Posted by "vy (via GitHub)" <gi...@apache.org>.
GitHub user vy edited a comment on the discussion: How do I register a StatusListener in configuration?

This is explained in [the Status Logger documentation](https://logging.apache.org/log4j/2.x/manual/status-logger.html#listeners). You need to programmatically do it. This is also stated in the JavaDoc you linked:

> You can programmatically register listeners using `registerListener(StatusListener)`

GitHub link: https://github.com/apache/logging-log4j2/discussions/3576#discussioncomment-12638488

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org


Re: [D] How do I register a StatusListener in configuration? [logging-log4j2]

Posted by "voddan (via GitHub)" <gi...@apache.org>.
GitHub user voddan added a comment to the discussion: How do I register a StatusListener in configuration?

@vy How would it look in a project? If you can share an example project where you do it like that, it would be great.

Simply put, I don't understand where I should put this line of code `statusLogger.registerListener(StatusListener)`, considering that I can't put it into the code of the app.

GitHub link: https://github.com/apache/logging-log4j2/discussions/3576#discussioncomment-12639233

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org


Re: [D] How do I register a StatusListener in configuration? [logging-log4j2]

Posted by "vy (via GitHub)" <gi...@apache.org>.
GitHub user vy added a comment to the discussion: How do I register a StatusListener in configuration?

```
StatusLogger.getLogger().registerListener(...)
```

You need to place this snippet as early as possible in your application initialization, e.g.,

```
static {
    StatusLogger.getLogger().registerListener(...)
}

public static void main(String[] args) { ... }
```

GitHub link: https://github.com/apache/logging-log4j2/discussions/3576#discussioncomment-12639529

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org


Re: [D] How do I register a StatusListener in configuration? [logging-log4j2]

Posted by "vy (via GitHub)" <gi...@apache.org>.
GitHub user vy added a comment to the discussion: How do I register a StatusListener in configuration?

This is explained in [the Status Logger documentation](https://logging.apache.org/log4j/2.x/manual/status-logger.html#listeners). You need to programmatically do it.

GitHub link: https://github.com/apache/logging-log4j2/discussions/3576#discussioncomment-12638488

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscribe@logging.apache.org