You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "brandonburchett (via GitHub)" <gi...@apache.org> on 2024/03/04 17:45:24 UTC
[I] NullPointerException when programmatically creating a logger/appender after file configuration has been loaded (logging-log4j2)
brandonburchett opened a new issue, #2346:
URL: https://github.com/apache/logging-log4j2/issues/2346
## Description
NullPointerException when programmatically creating a logger/appender after file configuration has been loaded.
When configuring a logger at runtime like so, i get a Null Pointer Exception.
`final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
RollingFileAppender appender = RollingFileAppender.newBuilder()
.setName("data-file-appender-for-"+topicName)
.setLayout(PatternLayout.newBuilder().withPattern("%m%n").build())
.withFilePattern(Path.of(directory.toString(), topicName.toLowerCase() + "_%d{UNIX_MILLIS}").toString())
.withCreateOnDemand(true)
.withPolicy(SizeBasedTriggeringPolicy.createPolicy("20MB"))
.withStrategy(DirectWriteRolloverStrategy.newBuilder()
.withConfig(config)
.withMaxFiles("15").build())
.build();
appender.start();
config.addAppender(appender);
LoggerConfig loggerConfig = LoggerConfig.newBuilder()
.withLoggerName(name)
.withAdditivity(false)
.withLevel(Level.ALL)
.build();
loggerConfig.addAppender(appender, null, null);
config.addLogger(name, loggerConfig);
ctx.updateLoggers();`
## Configuration
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" shutdownHookEnabled="false">
<Appenders>
<RollingFile name="default-file" fileName="${sys:log.dir}/appfile.log" filePattern="${sys:log.dir}/appfile%i.log" >
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="5MB" />
</Policies>
<DefaultRolloverStrategy max="5" fileIndex="min" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="${sys:log.level:-info}">
<AppenderRef ref="default-file"/>
</Root>
</Loggers>
</Configuration>
**Version:** 2.22.0
**Operating system:** RHEL 8.1
**JDK:** JDK 17
## Logs
```
java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "pattern" is null
at org.apache.logging.log4j.core.appender.rolling.PatternProcessor.patternContains(PatternProcessor.java:365)
at org.apache.logging.log4j.core.appender.rolling.PatternProcessor.calculateFrequency(PatternProcessor.java:323)
at org.apache.logging.log4j.core.appender.rolling.PatternProcessor.<init>(PatternProcessor.java:96)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.<init>(RollingFileManager.java:147)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory.createManager(RollingFileManager.java:755)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager$RollingFileManagerFactory.createManager(RollingFileManager.java:718)
at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:144)
at org.apache.logging.log4j.core.appender.OutputStreamManager.getManager(OutputStreamManager.java:100)
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:217)
at org.apache.logging.log4j.core.appender.RollingFileAppender$Builder.build(RollingFileAppender.java:135)
at com.company.AbstractConsumer.createDataLogger(AbstractConsumer.java:151)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] NullPointerException when programmatically creating a logger/appender after file configuration has been loaded (logging-log4j2)
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on issue #2346:
URL: https://github.com/apache/logging-log4j2/issues/2346#issuecomment-1977253976
@brandonburchett,
Can you provide the version of `log4j-core` that was used to generate the stack trace? The line numbers don't match version `2.22.0`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] NPE in `PatternProcessor` when using a `UNIX_MILLIS` date pattern (logging-log4j2)
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz closed issue #2346: NPE in `PatternProcessor` when using a `UNIX_MILLIS` date pattern
URL: https://github.com/apache/logging-log4j2/issues/2346
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] NullPointerException when programmatically creating a logger/appender after file configuration has been loaded (logging-log4j2)
Posted by "brandonburchett (via GitHub)" <gi...@apache.org>.
brandonburchett commented on issue #2346:
URL: https://github.com/apache/logging-log4j2/issues/2346#issuecomment-1977192271
When replacing the date lookup with '%d{yyyyMMddhhmmssSSS}', there is no issue.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] NullPointerException when programmatically creating a logger/appender after file configuration has been loaded (logging-log4j2)
Posted by "brandonburchett (via GitHub)" <gi...@apache.org>.
brandonburchett commented on issue #2346:
URL: https://github.com/apache/logging-log4j2/issues/2346#issuecomment-1977641559
@ppkarwasz I apologize, I'm using 2.20.0
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org