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>









share|improve this question























  • 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














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>









share|improve this question























  • 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












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>









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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















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












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>





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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>





    share|improve this answer
























      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>





      share|improve this answer






















        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>





        share|improve this answer












        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>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 18:01









        Tacitus86

        3751213




        3751213



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20