Log4j2 Programmatic Override
up vote
0
down vote
favorite
I'm attempting to programmatically override the levels of my log4j2 loggers on the fly, but it doesn't seem to be working. Can anyone point out what I might be doing wrong? This is the code that I am currently using to attempt to reset the levels.
final static Level REQUEST = Level.forName("REQUEST", 450);
errorLog.info("Showing Requests in logs");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig("console");
loggerConfig.setLevel(REQUEST);
LoggerConfig loggerConfigFile = config.getLoggerConfig("file-log");
loggerConfigFile.setLevel(REQUEST);
ctx.updateLoggers();
My configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<RollingFile name="file-log" fileName="$log-path/DataAdapter.log"
filePattern="$log-path/DataAdapter-%dyyyy-MM-dd.log">
<PatternLayout>
<pattern>[%-5level] %dMM-dd-yyyy HH:mm:ss [%t] %c1 - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %dMM-dd-yyyy HH:mm:ss %c1 - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="joy.com.DataAdapter" level="trace" additivity="false">
<appender-ref ref="file-log" level="info"/>
</Logger>
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
</Loggers>
</Configuration>
java log4j2
add a comment |
up vote
0
down vote
favorite
I'm attempting to programmatically override the levels of my log4j2 loggers on the fly, but it doesn't seem to be working. Can anyone point out what I might be doing wrong? This is the code that I am currently using to attempt to reset the levels.
final static Level REQUEST = Level.forName("REQUEST", 450);
errorLog.info("Showing Requests in logs");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig("console");
loggerConfig.setLevel(REQUEST);
LoggerConfig loggerConfigFile = config.getLoggerConfig("file-log");
loggerConfigFile.setLevel(REQUEST);
ctx.updateLoggers();
My configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<RollingFile name="file-log" fileName="$log-path/DataAdapter.log"
filePattern="$log-path/DataAdapter-%dyyyy-MM-dd.log">
<PatternLayout>
<pattern>[%-5level] %dMM-dd-yyyy HH:mm:ss [%t] %c1 - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %dMM-dd-yyyy HH:mm:ss %c1 - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="joy.com.DataAdapter" level="trace" additivity="false">
<appender-ref ref="file-log" level="info"/>
</Logger>
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
</Loggers>
</Configuration>
java log4j2
Hope this will help: stackoverflow.com/a/41993517/2987755
– dkb
Nov 9 at 15:22
Unfortunately it seems like that is a bit more geared towards command line.
– Tacitus86
Nov 9 at 15:33
okay, isn't thislog4j2 loggers based on commandline flags
means that, or you can have a look other answers as well in same post: stackoverflow.com/a/15922945/2987755
– dkb
Nov 9 at 15:34
Oh sorry no. I'm using the java command line arguments to know when to trigger an internal programmatic override. So by default I want my logs to how INFO level and above. But if the application got a 'showreplies' argument, then I want to programmatically change it to say DEBUG and above level inside the code.
– Tacitus86
Nov 9 at 15:38
That link only works for log4j, not the log4j2 api.
– Tacitus86
Nov 9 at 15:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm attempting to programmatically override the levels of my log4j2 loggers on the fly, but it doesn't seem to be working. Can anyone point out what I might be doing wrong? This is the code that I am currently using to attempt to reset the levels.
final static Level REQUEST = Level.forName("REQUEST", 450);
errorLog.info("Showing Requests in logs");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig("console");
loggerConfig.setLevel(REQUEST);
LoggerConfig loggerConfigFile = config.getLoggerConfig("file-log");
loggerConfigFile.setLevel(REQUEST);
ctx.updateLoggers();
My configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<RollingFile name="file-log" fileName="$log-path/DataAdapter.log"
filePattern="$log-path/DataAdapter-%dyyyy-MM-dd.log">
<PatternLayout>
<pattern>[%-5level] %dMM-dd-yyyy HH:mm:ss [%t] %c1 - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %dMM-dd-yyyy HH:mm:ss %c1 - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="joy.com.DataAdapter" level="trace" additivity="false">
<appender-ref ref="file-log" level="info"/>
</Logger>
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
</Loggers>
</Configuration>
java log4j2
I'm attempting to programmatically override the levels of my log4j2 loggers on the fly, but it doesn't seem to be working. Can anyone point out what I might be doing wrong? This is the code that I am currently using to attempt to reset the levels.
final static Level REQUEST = Level.forName("REQUEST", 450);
errorLog.info("Showing Requests in logs");
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig("console");
loggerConfig.setLevel(REQUEST);
LoggerConfig loggerConfigFile = config.getLoggerConfig("file-log");
loggerConfigFile.setLevel(REQUEST);
ctx.updateLoggers();
My configuration file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<RollingFile name="file-log" fileName="$log-path/DataAdapter.log"
filePattern="$log-path/DataAdapter-%dyyyy-MM-dd.log">
<PatternLayout>
<pattern>[%-5level] %dMM-dd-yyyy HH:mm:ss [%t] %c1 - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %dMM-dd-yyyy HH:mm:ss %c1 - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="joy.com.DataAdapter" level="trace" additivity="false">
<appender-ref ref="file-log" level="info"/>
</Logger>
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
</Loggers>
</Configuration>
java log4j2
java log4j2
edited Nov 9 at 15:41
asked Nov 9 at 15:08
Tacitus86
3751213
3751213
Hope this will help: stackoverflow.com/a/41993517/2987755
– dkb
Nov 9 at 15:22
Unfortunately it seems like that is a bit more geared towards command line.
– Tacitus86
Nov 9 at 15:33
okay, isn't thislog4j2 loggers based on commandline flags
means that, or you can have a look other answers as well in same post: stackoverflow.com/a/15922945/2987755
– dkb
Nov 9 at 15:34
Oh sorry no. I'm using the java command line arguments to know when to trigger an internal programmatic override. So by default I want my logs to how INFO level and above. But if the application got a 'showreplies' argument, then I want to programmatically change it to say DEBUG and above level inside the code.
– Tacitus86
Nov 9 at 15:38
That link only works for log4j, not the log4j2 api.
– Tacitus86
Nov 9 at 15:54
add a comment |
Hope this will help: stackoverflow.com/a/41993517/2987755
– dkb
Nov 9 at 15:22
Unfortunately it seems like that is a bit more geared towards command line.
– Tacitus86
Nov 9 at 15:33
okay, isn't thislog4j2 loggers based on commandline flags
means that, or you can have a look other answers as well in same post: stackoverflow.com/a/15922945/2987755
– dkb
Nov 9 at 15:34
Oh sorry no. I'm using the java command line arguments to know when to trigger an internal programmatic override. So by default I want my logs to how INFO level and above. But if the application got a 'showreplies' argument, then I want to programmatically change it to say DEBUG and above level inside the code.
– Tacitus86
Nov 9 at 15:38
That link only works for log4j, not the log4j2 api.
– Tacitus86
Nov 9 at 15:54
Hope this will help: stackoverflow.com/a/41993517/2987755
– dkb
Nov 9 at 15:22
Hope this will help: stackoverflow.com/a/41993517/2987755
– dkb
Nov 9 at 15:22
Unfortunately it seems like that is a bit more geared towards command line.
– Tacitus86
Nov 9 at 15:33
Unfortunately it seems like that is a bit more geared towards command line.
– Tacitus86
Nov 9 at 15:33
okay, isn't this
log4j2 loggers based on commandline flags
means that, or you can have a look other answers as well in same post: stackoverflow.com/a/15922945/2987755– dkb
Nov 9 at 15:34
okay, isn't this
log4j2 loggers based on commandline flags
means that, or you can have a look other answers as well in same post: stackoverflow.com/a/15922945/2987755– dkb
Nov 9 at 15:34
Oh sorry no. I'm using the java command line arguments to know when to trigger an internal programmatic override. So by default I want my logs to how INFO level and above. But if the application got a 'showreplies' argument, then I want to programmatically change it to say DEBUG and above level inside the code.
– Tacitus86
Nov 9 at 15:38
Oh sorry no. I'm using the java command line arguments to know when to trigger an internal programmatic override. So by default I want my logs to how INFO level and above. But if the application got a 'showreplies' argument, then I want to programmatically change it to say DEBUG and above level inside the code.
– Tacitus86
Nov 9 at 15:38
That link only works for log4j, not the log4j2 api.
– Tacitus86
Nov 9 at 15:54
That link only works for log4j, not the log4j2 api.
– Tacitus86
Nov 9 at 15:54
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I finally figured it out. Having the level="" in each appender-ref
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
was overriding my programmatic settings. Removing them allowed me to set the level from the code.
<Root level="info" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="file-log"/>
</Root>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I finally figured it out. Having the level="" in each appender-ref
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
was overriding my programmatic settings. Removing them allowed me to set the level from the code.
<Root level="info" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="file-log"/>
</Root>
add a comment |
up vote
0
down vote
I finally figured it out. Having the level="" in each appender-ref
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
was overriding my programmatic settings. Removing them allowed me to set the level from the code.
<Root level="info" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="file-log"/>
</Root>
add a comment |
up vote
0
down vote
up vote
0
down vote
I finally figured it out. Having the level="" in each appender-ref
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
was overriding my programmatic settings. Removing them allowed me to set the level from the code.
<Root level="info" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="file-log"/>
</Root>
I finally figured it out. Having the level="" in each appender-ref
<Root level="trace" additivity="false">
<appender-ref ref="console" level="info"/>
<appender-ref ref="file-log" level="info"/>
</Root>
was overriding my programmatic settings. Removing them allowed me to set the level from the code.
<Root level="info" additivity="false">
<appender-ref ref="console"/>
<appender-ref ref="file-log"/>
</Root>
answered Nov 9 at 18:01
Tacitus86
3751213
3751213
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228299%2flog4j2-programmatic-override%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hope this will help: stackoverflow.com/a/41993517/2987755
– dkb
Nov 9 at 15:22
Unfortunately it seems like that is a bit more geared towards command line.
– Tacitus86
Nov 9 at 15:33
okay, isn't this
log4j2 loggers based on commandline flags
means that, or you can have a look other answers as well in same post: stackoverflow.com/a/15922945/2987755– dkb
Nov 9 at 15:34
Oh sorry no. I'm using the java command line arguments to know when to trigger an internal programmatic override. So by default I want my logs to how INFO level and above. But if the application got a 'showreplies' argument, then I want to programmatically change it to say DEBUG and above level inside the code.
– Tacitus86
Nov 9 at 15:38
That link only works for log4j, not the log4j2 api.
– Tacitus86
Nov 9 at 15:54