Log4j 2
  1. Log4j 2
  2. LOG4J2-128

How do I set ConsoleAppender.setFollow(true) ?

    Details

    • Type: Question Question
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-beta3
    • Fix Version/s: 2.0-beta4
    • Component/s: Appenders
    • Labels:
      None

      Description

      I need to reassign System.out and System.err after Log4j2 is configured.

      Log4j 1.2 supports ConsoleAppender.setFollow(true).
      If set, the appender honors reassignments of System.out or System.err made after configuration.

      How do I set this within Log4j2 ?

      Now, Log4j2 is still logging to Console even if I reassigned System.out and System.err before.

        Activity

        Hide
        Armin Heinzer added a comment -

        Tested svn revision 1416125 successfully.

        Show
        Armin Heinzer added a comment - Tested svn revision 1416125 successfully.
        Hide
        Armin Heinzer added a comment -

        thanks, now it works as expected.

        Show
        Armin Heinzer added a comment - thanks, now it works as expected.
        Hide
        Ralph Goers added a comment -

        Most Appenders use a Manager to avoid having multiple output streams trying to access the same file, etc. In your case you don't really need to know about that. If you run your test against the code in trunk it should be working now.

        Show
        Ralph Goers added a comment - Most Appenders use a Manager to avoid having multiple output streams trying to access the same file, etc. In your case you don't really need to know about that. If you run your test against the code in trunk it should be working now.
        Hide
        Armin Heinzer added a comment -

        Sorry, I don't understand what you mean. Which ConsoleManager do you mean (i am only using the LogManger in the source an a Console appender in log4j2.xml)? Could you give me some more hints please, a link to the corresponding documentation/api of this ConsoleAppender or a patch to my example how this should be implemented correctly?

        Show
        Armin Heinzer added a comment - Sorry, I don't understand what you mean. Which ConsoleManager do you mean (i am only using the LogManger in the source an a Console appender in log4j2.xml)? Could you give me some more hints please, a link to the corresponding documentation/api of this ConsoleAppender or a patch to my example how this should be implemented correctly?
        Hide
        Ralph Goers added a comment -

        The ConsoleManager created without follow was being used by the ConsoleAppender with follow. The follow value is now included in the Manager name. Please check out again and give it another try.

        Show
        Ralph Goers added a comment - The ConsoleManager created without follow was being used by the ConsoleAppender with follow. The follow value is now included in the Manager name. Please check out again and give it another try.
        Hide
        Armin Heinzer added a comment -

        I have provided a simple test here: https://www.dropbox.com/sh/8gyul8wuzomp4vl/cdL69Oxv2z
        I have this issue on Windows and Ubuntu Linux.

        Show
        Armin Heinzer added a comment - I have provided a simple test here: https://www.dropbox.com/sh/8gyul8wuzomp4vl/cdL69Oxv2z I have this issue on Windows and Ubuntu Linux.
        Hide
        Ralph Goers added a comment -

        I created ConsoleAppenderTest to try to verify this use case but it sounds like it isn't working. Can you provide a test that shows the issue? Also, what OS are you running on?

        Show
        Ralph Goers added a comment - I created ConsoleAppenderTest to try to verify this use case but it sounds like it isn't working. Can you provide a test that shows the issue? Also, what OS are you running on?
        Hide
        Armin Heinzer added a comment -

        Today the build was successful.

        I did a test with the current snapshot but it doesn't seem to work as I excpected.
        I set <Console name="STDOUT_SHORT" follow="true"> in log4j2.xml but after redirection of stdout and stderr to the Swing-TextPane, every System.out.println prints to the Swing-TextPane as expected, but every Logger message prints still to the Console.

        The Logger messages goes to the Swing-TextPane if instantiate the Logger AFTER the redirection of stdout and stderr (but this might have worked already before you implemented the new follow attribute)
        But I would expect that follow="true" honors a redirection of stdout and stderr BEFORE the Logger is instantiated.

        Show
        Armin Heinzer added a comment - Today the build was successful. I did a test with the current snapshot but it doesn't seem to work as I excpected. I set <Console name="STDOUT_SHORT" follow="true"> in log4j2.xml but after redirection of stdout and stderr to the Swing-TextPane, every System.out.println prints to the Swing-TextPane as expected, but every Logger message prints still to the Console. The Logger messages goes to the Swing-TextPane if instantiate the Logger AFTER the redirection of stdout and stderr (but this might have worked already before you implemented the new follow attribute) But I would expect that follow="true" honors a redirection of stdout and stderr BEFORE the Logger is instantiated.
        Hide
        Ralph Goers added a comment -

        Thanks, Can you send an email to the dev list with the output from the failed test as well as information on the system you are running on? There should be a file under core/target/surefire-reports with a file name that matches the name of the unit test class that failed and ends in ".txt".

        Show
        Ralph Goers added a comment - Thanks, Can you send an email to the dev list with the output from the failed test as well as information on the system you are running on? There should be a file under core/target/surefire-reports with a file name that matches the name of the unit test class that failed and ends in ".txt".
        Hide
        Armin Heinzer added a comment - - edited

        Thanks a lot. I will test it as soon the beta4 jar's are downloadable.
        (I tried to build it by myself from source with "mvn install", but it stopped in the core module by a failed test)

        Show
        Armin Heinzer added a comment - - edited Thanks a lot. I will test it as soon the beta4 jar's are downloadable. (I tried to build it by myself from source with "mvn install", but it stopped in the core module by a failed test)
        Hide
        Ralph Goers added a comment -

        I added support for the follow atribute in revision 1413034. Please verify and close.

        Show
        Ralph Goers added a comment - I added support for the follow atribute in revision 1413034. Please verify and close.
        Hide
        Ralph Goers added a comment - - edited

        After looking at what log4j 1.x and your code above is doing I believe I can add support for it and I don't really see any reason not to. I'm not really worried about the performance aspects of it on the Log4j side. It will only have minor impact on the ConsoleAppender. As Gary points out though, the application consuming STDERR and STDOUT needs to be able to handle the data effectively.

        I missed your last comment when I first posted this comment. I still see no reason not to add support for this. Is the Appender you implemented something you think others might find useful? If so please consider opening a new Jira and attaching it to it.

        Show
        Ralph Goers added a comment - - edited After looking at what log4j 1.x and your code above is doing I believe I can add support for it and I don't really see any reason not to. I'm not really worried about the performance aspects of it on the Log4j side. It will only have minor impact on the ConsoleAppender. As Gary points out though, the application consuming STDERR and STDOUT needs to be able to handle the data effectively. I missed your last comment when I first posted this comment. I still see no reason not to add support for this. Is the Appender you implemented something you think others might find useful? If so please consider opening a new Jira and attaching it to it.
        Hide
        Armin Heinzer added a comment -

        Now I implemented a basic JTextPaneAppender and on runtime (if my application is started with the GUI and not in batch mode), I remove the Console Appenders (configured in log4j2.xml) and add my JTextPaneAppender which appends the Log entries to the corresponding JTextPane.

        This works fine and this is an acceptable solution for me.

        Thanks a lot and kind Regards,
        Armin

        Show
        Armin Heinzer added a comment - Now I implemented a basic JTextPaneAppender and on runtime (if my application is started with the GUI and not in batch mode), I remove the Console Appenders (configured in log4j2.xml) and add my JTextPaneAppender which appends the Log entries to the corresponding JTextPane. This works fine and this is an acceptable solution for me. Thanks a lot and kind Regards, Armin
        Hide
        Gary Gregory added a comment - - edited

        This kind of thing is a recipe for poor performance for you application unless you do a couple of things very carefully:

        • Remove text from the pane as you add new text to avoid growing the text to infinity and running out of memory
        • Calling Swing from the UI thread

        This kind of non-trivial code may be an argument for Log4J2 to provide a Swing text or list widget that does just that of course.

        Log4J has stayed away from putting UI code in the core project itself.

        There is also Chainsaw...

        Gary

        Show
        Gary Gregory added a comment - - edited This kind of thing is a recipe for poor performance for you application unless you do a couple of things very carefully: Remove text from the pane as you add new text to avoid growing the text to infinity and running out of memory Calling Swing from the UI thread This kind of non-trivial code may be an argument for Log4J2 to provide a Swing text or list widget that does just that of course. Log4J has stayed away from putting UI code in the core project itself. There is also Chainsaw... Gary
        Hide
        Armin Heinzer added a comment - - edited

        Or should I implement my own JTextPaneAppender (based on the idea of http://www.suurmeijer.net/java/j2se/64-log4j-write-your-own-appender-for-a-swing-component)?

        If yes, how can I implement my own Appender? Log4j2 does not seem to have an AppenderSkeleton class.

        What is the easiest way to log into a Swing JTextPane with Log4j2 ?

        Show
        Armin Heinzer added a comment - - edited Or should I implement my own JTextPaneAppender (based on the idea of http://www.suurmeijer.net/java/j2se/64-log4j-write-your-own-appender-for-a-swing-component)? If yes, how can I implement my own Appender? Log4j2 does not seem to have an AppenderSkeleton class. What is the easiest way to log into a Swing JTextPane with Log4j2 ?
        Hide
        Armin Heinzer added a comment - - edited

        I did not use ConsoleAppender.setFollow(true) directly, i configured it in the Log4j 1.2 properties file:
        log4j.appender.consoleAppender.follow = true

        This method is documented in http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/ConsoleAppender.html#setFollow%28boolean%29
        I think that my properties entry did the same.

        But now in Log4j2, the ConsoleAppender does not have a setFollow method and I dont know how to set this in the Log4j2-XML-Config-File.

        I need this because I want to redirect all logged messages to a Swing-JTextPane. Therefore I created a new PrintStream and set it to STDOUT and STDERR. This worked find with Log4j 1.2 (the log messages were redirected to the Swing App), but with Log4j2, this is ignored (the log messages goes still to the Console).

        Here my code:

        PrintStream printStream = new PrintStream(System.out, true) {
        @Override
        public void write(byte[] b, int off, int len) {
        String string = new String(b, off, len);
        if (swingGUI != null && swingGUI.isVisible())

        { swingGUI.addToLog(string); }

        else

        { super.write(b, off, len); }

        }
        };

        System.setOut(printStream);
        System.setErr(printStream);

        Show
        Armin Heinzer added a comment - - edited I did not use ConsoleAppender.setFollow(true) directly, i configured it in the Log4j 1.2 properties file: log4j.appender.consoleAppender.follow = true This method is documented in http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/ConsoleAppender.html#setFollow%28boolean%29 I think that my properties entry did the same. But now in Log4j2, the ConsoleAppender does not have a setFollow method and I dont know how to set this in the Log4j2-XML-Config-File. I need this because I want to redirect all logged messages to a Swing-JTextPane. Therefore I created a new PrintStream and set it to STDOUT and STDERR. This worked find with Log4j 1.2 (the log messages were redirected to the Swing App), but with Log4j2, this is ignored (the log messages goes still to the Console). Here my code: PrintStream printStream = new PrintStream(System.out, true) { @Override public void write(byte[] b, int off, int len) { String string = new String(b, off, len); if (swingGUI != null && swingGUI.isVisible()) { swingGUI.addToLog(string); } else { super.write(b, off, len); } } }; System.setOut(printStream); System.setErr(printStream);
        Hide
        Ralph Goers added a comment -

        BTW - does setFollow even work? I can't find any documentation on it and I can't see how it switches from STDERR or STDOUT to the other without royally screwing things up.

        Show
        Ralph Goers added a comment - BTW - does setFollow even work? I can't find any documentation on it and I can't see how it switches from STDERR or STDOUT to the other without royally screwing things up.
        Hide
        Ralph Goers added a comment -

        Components in Log4j 2 are generally immutable to avoid as many locking issues as possible. That said, I could imagine several ways to accomplish what you are looking for based upon what your requirements really are. Can you provide more details on what you want to accomplish?

        Show
        Ralph Goers added a comment - Components in Log4j 2 are generally immutable to avoid as many locking issues as possible. That said, I could imagine several ways to accomplish what you are looking for based upon what your requirements really are. Can you provide more details on what you want to accomplish?

          People

          • Assignee:
            Ralph Goers
            Reporter:
            Armin Heinzer
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development