The snippet you shared tries to add your custom appender to an already
initialized logger context. Do you really need to add your appender
after initialization and programmatically? If not, you can simply use
your appender in `log4j2.xml` given you have its class in a package
scanned by Log4j. You can use the `packages` attribute of the
`Configuration` root XML element to provide the list of packages Log4j
should look for custom plugins. See
https://logging.apache.org/log4j/2.x/manual/configuration.html page
for further details.

If you really need to this programmatically, you better use the
`ConfigurationBuilder`:
https://logging.apache.org/log4j/2.x/manual/customconfig.html

On Fri, Oct 21, 2022 at 11:09 AM Thomas Hartwig
<thomas.hart...@gmail.com> wrote:
>
> I want to add a customized Appender to Log4J 2.x. However there is no
> clear API to use it.
>
> Right now I am using:
>
> ((org.apache.logging.log4j.core.Logger)LogManager.getRootLogger()).addAppender(new
> MyAppender());
>
> This is working, however the API states this:
>
> This method is not exposed through the public API and is used primarily
> for unit testing.
>
> There are a couple of examples in the net where it might be added
> different, but no on is referring to an "official" way.
>
> What is the preferred way of adding such an Appender?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

Reply via email to