[
https://issues.apache.org/jira/browse/LOG4J2-3437?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17507544#comment-17507544
]
Piotr Karwasz edited comment on LOG4J2-3437 at 3/16/22, 1:50 PM:
-----------------------------------------------------------------
[~PoojaTheCoder]: that API is not implemented (it is almost a no-op).
However you can do the same thing using native Log4j 2.x Core APIs:
{code:java}
final Logger logger = (Logger)
LogManager.getLogger("org.apache.logging.example");
final Configuration config = ((LoggerContext)
LogManager.getContext(false)).getConfiguration();
// FileAppender
final Appender appender = FileAppender.newBuilder()
.setConfiguration(config)
.setName("unique_name")
.withFileName("filename")
.build();
appender.start();
// 'threshold' can be translated in many ways, e.g. an AppenderRef level:
final LoggerConfig loggerConfig = LoggerConfig.newBuilder()
.withConfig(config)
.withLoggerName(logger.getName())
.withRefs(new AppenderRef[0])
.build();
loggerConfig.addAppender(appender, /*AppenderRef level*/ Level.DEBUG,
/*AppenderRef filter*/null);
config.addLogger(loggerConfig.getName(), loggerConfig);
{code}
Except {{LogManager}} and {{{}Level{}}}, all the classes above are from
{{{}org.apache.logging.log4j.core{}}}.
was (Author: pkarwasz):
[~PoojaTheCoder]: that API is not implemented (it is almost a no-op).
However you can do the same thing using native Log4j 2.x Core APIs:
{code:java}
final Logger logger = (Logger)
LogManager.getLogger("org.apache.logging.example");
final Configuration config = ((LoggerContext)
LogManager.getContext(false)).getConfiguration();
// FileAppender
final Appender appender = FileAppender.newBuilder()
.setConfiguration(config)
.setName("unique_name")
.withFileName("filename")
.build();
appender.start();
// 'threshold' can be translated in many ways, e.g. an AppenderRef level:
final LoggerConfig loggerConfig = LoggerConfig.newBuilder()
.withConfig(config)
.withLoggerName(logger.getName())
.build();
loggerConfig.addAppender(appender, /*AppenderRef level*/ Level.DEBUG,
/*AppenderRef filter*/null);
config.addLogger(loggerConfig.getName(), loggerConfig);
{code}
Except {{LogManager}} and {{Level}}, all the classes above are from
{{org.apache.logging.log4j.core}}.
> Log4j 1.x bridge Request to support programmatically configuring Log4j.
> -----------------------------------------------------------------------
>
> Key: LOG4J2-3437
> URL: https://issues.apache.org/jira/browse/LOG4J2-3437
> Project: Log4j 2
> Issue Type: Bug
> Components: Log4j 1.2 bridge
> Affects Versions: 2.17.2
> Reporter: Pooja Pandey
> Priority: Major
>
> As per log4j migration documentation
> ([https://logging.apache.org/log4j/2.x/manual/migration.html),|https://logging.apache.org/log4j/2.x/manual/migration.html)]
> I understand that currently log4j 1.x bridge 2.17.2 doesn't support
> programmatically configuring log4j appenders.
> *#### Limitations of the Log4j 1.x bridge*
> # *They must not programmatically configure Log4j.*
>
> In our application, we have a requirement where we need to add many
> FileAppender programmatically with various log threshold values.
>
> Hoping to get this feature supported in future versions for log4j1.x bridge.
>
--
This message was sent by Atlassian Jira
(v8.20.1#820001)