11 Replies Latest reply on Mar 17, 2014 3:01 AM by kobe_fans

    Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?

    ymss

      I have a requirement to redirect some log message in STDOUT to seperate log file instead of server.log for better trouble shotting, could anyone tell me if this is possible and how to do this? I've searched questions in this community, but still can not find an answer.

       

      BTW, the System.out.println() log message is generated in package com.sun.security, so I can not change the source code to use log4j by using Logger.getLogger().

        • 1. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
          jamezp

          There is nothing about logging that makes it write to STDOUT. Logging is separated into 2 major parts. You've got a logger and a handler. A third could be considered a formatter, but we won't go into details on that here.

           

          A logger simple takes a message and may or may not pass it to a handler if the level is allowed to be logged.

           

          A handler takes the message from the logger and outputs it some where.

           

          If you have a specific logger (category) you want to send to a different file, then you need to configure that to happen. The first thing you need to do is create a handler, probably a file handler.

          /subsystem=logging/file-handler=fh:add(file={relative-to=jboss.server.log.dir, path=my.log},autoflush=true,append=true)
          

           

          Then you need to create a logger to add your new handler too. Use the name/category of the logger you want to attach this to. For example any logger created with something like Logger.getLogger("org.jboss.example") you could use the category "org.jboss" to all loggers that start with org.jboss.

          /subsystem=logger/logger=org.jboss:add(level=INFO,handlers=[fh])
          

           

          This will output all loggers that start with org.jboss to my.file in the standalone/log directory. If you don't want those messages to also go the console or the default server.log on the operation above add the attribute use-parent-handler=false.

           

          --

          James R. Perkins

          • 2. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
            ymss

            Thanks, James, now I understand the log printed by System.out is just log under category "stdout", like "org.jboss". They are the same in nature.

            1 of 1 people found this helpful
            • 3. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
              ymss

              But, is it possible to distinguish different package when using System.out to print log? I mean redirecting log from package com.a printed by System.out to file A, and redirecting log from package com.b printed by System.out to file B.

              • 4. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
                jamezp

                Using System.xxx to print messages is not the same as using a logger. A logger can be configured to do specific things. There is no way to configure messages coming from System.out or System.err. Those will always write to STDOUT and STDERR respectively.

                 

                --

                James R. Perkins

                • 5. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
                  ymss

                  Thanks for your explaination.

                  • 6. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
                    kobe_fans

                    Hi , James

                    Is there any way to change log output strategy, not every day to collect a log, and then write a file, What i want is to customize a log size, such as 4MB. Once the log size reached 4MB, then write a new file, and the file name can be customized .

                    I will be truly grateful for your help

                    --zhihong

                    • 7. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
                      ymss

                      I think the following configuration is what you want:

                      <size-rotating-file-handler name="FILE">

                           <formatter>

                                <pattern-formatter pattern="%d %-5r %-5p [%c] (%t:%x) %m%n"/>

                           </formatter>

                           <file relative-to="jboss.server.log.dir" path="server.log"/>

                           <rotate-size value="10M"/>

                           <max-backup-index value="5"/>

                           <append value="true"/>

                      </size-rotating-file-handler>

                      You can figure out each field meaning by your self!

                      • 8. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
                        kobe_fans

                        I just do as your said , but not wok . This is my configuration of standalone.xml :

                        <subsystem xmlns="urn:jboss:domain:logging:2.0">

                                    <console-handler name="CONSOLE">

                                        <level name="INFO"/>

                                        <formatter>

                                            <named-formatter name="COLOR-PATTERN"/>

                                        </formatter>

                                    </console-handler>

                                    <periodic-rotating-file-handler name="FILE" autoflush="true">

                                        <formatter>

                                            <named-formatter name="PATTERN"/>

                                        </formatter>

                                        <file relative-to="jboss.server.log.dir" path="server.log"/>

                                        <suffix value=".yyyy-MM-dd"/>

                                        <append value="true"/>

                                    </periodic-rotating-file-handler>

                                    <size-rotating-file-handler name="FH">

                                    <formatter>

                                         <pattern-formatter pattern="%d %-5r %-5p [%c] (%t:%x) %m%n"/>

                                   </formatter>

                                    <file relative-to="jboss.server.log.dir" path="server.log"/>

                                    <rotate-size value="100K"/>

                                    <max-backup-index value="5"/>

                                    <append value="true"/>

                                    </size-rotating-file-handler>

                                     <logger category="com.arjuna">

                                        <level name="WARN"/>

                                    </logger>

                                    <logger category="org.apache.tomcat.util.modeler">

                                        <level name="WARN"/>

                                    </logger>

                                    <logger category="org.jboss.as.config">

                                        <level name="DEBUG"/>

                                    </logger>

                                    <logger category="sun.rmi">

                                        <level name="WARN"/>

                                    </logger>

                                    <logger category="jacorb">

                                        <level name="WARN"/>

                                    </logger>

                                    <logger category="jacorb.config">

                                        <level name="ERROR"/>

                                    </logger>

                                    <root-logger>

                                        <level name="INFO"/>

                                        <handlers>

                                            <handler name="CONSOLE"/>

                                            <handler name="FILE"/>

                                        </handlers>

                                    </root-logger>

                                    <formatter name="PATTERN">

                                        <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                                    </formatter>

                                    <formatter name="COLOR-PATTERN">

                                        <pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                                    </formatter>

                                </subsystem>

                         

                        The all log util wirte in a file server.log though reach 100K

                        • 9. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
                          ymss

                          You haven't understood JBoss logging configuration, go to see the developer manual.

                          • 10. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
                            jamezp

                            That looks mostly right, but you also need to add the new handler to the root logger. Also, prefer using the web console or CLI over editing the XML. It will allow you to make changes while the server is running rather than having to stop the server, edit the XML, then start the server.

                             

                            --

                            James R. Perkins

                            • 11. Re: Is it possible to redirect System.out.println log message to seperate file instead of server.log in JBoss AS 7?
                              kobe_fans

                              Thanks , I got it .