9 Replies Latest reply on Mar 22, 2012 10:41 AM by Arthur Gerson

    Application logging using log4j

    Arthur Gerson Newbie

      We are using jboss-6.1.0.Final and trying to get application logging to work. Our testing is using a very simple servlet test program called Test.java. It has the normal servlet stuff and the following.

       

      private static final Log log=LogFactory.getLog(Test.class);

      .....

      log.info("Test entered.");

       

      This works just fine execpt that the output is to the server.log. It works but not a very good idea. The log entry should go to an application log like app.log. Reading a lot on the web and this forum there does not seem to be clear direction on what to do. We tried using log4j.xml and log4j.jar in the application war file and this did not work. Now I see a thread that says all you have to do is put jboss-logging.xml in your META-INF directory. Well this did now work either, because the log entries continued to be written in server.log and not app.log. The following is our jboss-logging.xml file. We did not remove the jboss-logging.xml file from the server/standard/deploy directory. I guess I am missing something but cannot figure it out. We are using Eclipse to test this little application. Thanks for any help.

       

      <?xml version="1.0" encoding="UTF-8"?>

      <!-- ===================================================================== -->
      <!--                                                                       -->
      <!--  Logging System Configuration                                         -->
      <!--                                                                       -->
      <!-- ===================================================================== -->

      <logging xmlns="urn:jboss:logging:6.0" xmlns:b="urn:jboss:bean-deployer:2.0">

         <!-- ================================= -->
         <!-- Preserve messages in a local file -->
         <!-- ================================= -->

         <!-- A time/date based rolling handler -->

         <periodic-rotating-file-handler
               file-name="${jboss.server.log.dir}/tan.log"
               name="TAN"
               autoflush="true"
               append="true"
               suffix=".yyyy-MM-dd">

            <error-manager>
               <only-once/>
            </error-manager>

            <formatter>
               <pattern-formatter pattern="%d %-5p [%c] (%t) %s%E%n"/>
            </formatter>
         </periodic-rotating-file-handler>

         <logger category="com.tanager">
            <level name="INFO"/>
         </logger>

         <root-logger>
            <level name="${jboss.server.log.threshold:INFO}"/>
            <handlers>
               <handler-ref name="TAN"/>
            </handlers>
         </root-logger>

      </logging>

        • 1. Re: Application logging using log4j
          Wolf-Dieter Fink Master

          One simple way is to add your appender to the  jboss-logging.xml file from the server/standard/deploy directory (remove the loggin from META-INF or do the changes here).

          Add also your com.tanager logger to it but add the handler-ref here and do not change the root-logger, then your log should go to the tan.log file.

          • 2. Re: Application logging using log4j
            Arthur Gerson Newbie

            Ok, this is what I have done so far. I added the following to the standard/deploy/jboss-logging.xml file.

               <log4j-appender name="TanAppender" class="org.apache.log4j.DailyRollingFileAppender">

                <error-manager>

                  <only-once/>

                </error-manager>

                <level name="INFO"/>

                <properties>

                  <!-- <property name="directory">${jboss.server.log.dir}/</property> -->

                  <property name="file">${jboss.server.log.dir}/tan.log</property>

                  <property name="append">true</property>

                  <property name="datePattern">'.'yyyy-MM-dd</property>

                </properties>

                <formatter>

                  <pattern-formatter pattern="%d %-5p [%c] %m%n"/>

                </formatter>

              </log4j-appender>

              

               <logger category="com.tanager">

                 <level name="INFO"/>

                 <handlers>

                   <handler-ref name="TanAppender"/>

                 </handlers>

               </logger>

             

            Got the following message when I test with my test program.

            ERROR [STDERR] log4j:ERROR No output stream or file set for the appender named [null].

             

            There seemed to be issues with the log4j.jar mentioned in Google so I replaced the log4j.jar in common/lib with the latest from the Apache group. Still get the messge. Am I missing something?

            Thanks, Art

            • 3. Re: Application logging using log4j
              Wolf-Dieter Fink Master

              Looks different to the standard appender.

              See the original jboss-logging.xml.

              Add the periodic-rotating-file-handler as you show it in your initial post.

              • 4. Re: Application logging using log4j
                Arthur Gerson Newbie

                Sorry for the confusion. I was trying something. Anyway, I now have the following in jboss-logging.xml.

                 

                   <periodic-rotating-file-handler

                         file-name="${jboss.server.log.dir}/tan.log"

                         name="TAN"

                         autoflush="true"

                         append="true"

                         suffix=".yyyy-MM-dd">

                        <error-manager>

                         <only-once/>

                      </error-manager>

                        <formatter>

                         <pattern-formatter pattern="%d %-5p [%c] (%t) %s%E%n"/>

                      </formatter>

                   </periodic-rotating-file-handler>

                     <logger category="com.tanager">

                      <level name="INFO"/>

                      <handlers>

                        <handler-ref name="TAN"/>

                      </handlers>

                   </logger>

                 

                This works and writes out the tan.log. I still get the same line writtent to server.log. How is this stopped?

                Thanks,Art

                • 5. Re: Application logging using log4j
                  Wolf-Dieter Fink Master

                  Does

                  <logger category="com.tanager" additivity="false>

                  helps?

                  • 6. Re: Application logging using log4j
                    Arthur Gerson Newbie

                    Now we have the following in jboss-logging.xml and the output still goes to both the server and tan log.

                     

                       <periodic-rotating-file-handler

                             file-name="${jboss.server.log.dir}/tan.log"

                             name="TAN"

                             autoflush="true"

                             append="true"

                             suffix=".yyyy-MM-dd">

                          <error-manager>

                             <only-once/>

                          </error-manager>

                          <formatter>

                             <pattern-formatter pattern="%d %-5p [%c] (%t) %s%E%n"/>

                          </formatter>

                       </periodic-rotating-file-handler>

                       <logger category="com.tanager" additivity="false">

                          <level name="INFO"/>

                          <handlers>

                            <handler-ref name="TAN"/>

                          </handlers>

                       </logger>

                    • 7. Re: Application logging using log4j
                      Wolf-Dieter Fink Master

                      Do you remove the handler TAN from the root-logger?

                      • 8. Re: Application logging using log4j
                        Arthur Gerson Newbie

                        I never added anything to the rool-logger.

                         

                          <root-logger>

                              <!-- Set the root logger priority via a system property, with a default value. -->

                              <level name="${jboss.server.log.threshold:INFO}"/>

                              <handlers>

                                 <handler-ref name="CONSOLE"/>

                                 <handler-ref name="FILE"/>

                              </handlers>

                           </root-logger>

                        • 9. Re: Application logging using log4j
                          Arthur Gerson Newbie

                          Thanks for the help. I am still unable to to stop the application messages from the server.log. But that is life.

                          1 of 1 people found this helpful