8 Replies Latest reply on Aug 6, 2012 10:28 AM by jamezp

    logging into different log files from one class / categories

    ulrich-scholz

      Dear all,

       

      I need to log into differnet log files from the same class. For example, some messages should go into foo.log only, others in bar.log only. I guess, this behaviour can be achieved with using two different loggers in the same class and with filtering according to categories. But I could not find a precise definition of category. If you know one, please point me to it.

       

      Usually, a logger for class xxx.java in package my.great.project is created with parameter xxx.class. To select statements issued with this logger, one could use one of the following configurations

       

      <logger category="my.great.project.xxx">

             <level name="WARN"/>

      </logger>

       

      or

       

      <logger category="my.great">

             <level name="WARN"/>

      </logger>

       

      But how can I filter the messages coming from the same class to different log files? I would need to create two loggers that use different categories. How should they look like?

       

      Thanks, Ulrich

        • 1. Re: logging into different log files from one class / categories
          jamezp

          The only way I can think of to do it would be to use two different loggers. Logger categories do not have to be the class name. They can be whatever you want to them to be. It's actually better practice to not use the class name as it makes it easier to configure the loggers if they use common categories.

           

          Generally speaking loggers should be static, so two different loggers with different categories would get you what you want.

           

          Example Logger Interface:

          package org.jboss.example;
          
          import org.jboss.logging.Logger;
          
          public interface Loggers {
              Logger FOO_LOGGER = Logger.getLogger("foo");
          
              Logger BAR_LOGGER = Logger.getLogger("bar");
          }
          

           

           

          Example Class:

          package org.jboss.example;
          
          public class Foo {
          
               public static void main(final String[] args) {
                    Loggers.FOO_LOGGER.info("Test from FOO_LOGGER");
                    Loggers.BAR_LOGGER.info("Test from BAR_LOGGER");
               }
          }
          

           

           

          Example config:

          <file-handler name="foo-handler">
              <formatter>
                  <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
              </formatter>
              <file relative-to="jboss.server.log.dir" path="foo.log"/>
              <append value="true"/>
          </file-handler>
          
          <file-handler name="bar-handler">
              <formatter>
                  <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
              </formatter>
              <file relative-to="jboss.server.log.dir" path="bar.log"/>
              <append value="true"/>
          </file-handler>
          
          <logger category="foo">
               <level name="INFO"/>
               <handlers>
                    <handler name="foo-handler"/>
               </handlers>
          </logger>
          
          <logger category="bar">
               <level name="INFO"/>
               <handlers>
                    <handler name="bar-handler"/>
               </handlers>
          </logger>
          
          

           

          --

          James R. Perkins

          1 of 1 people found this helpful
          • 2. Re: logging into different log files from one class / categories
            ulrich-scholz

            Thanks! One more question about categories:

             

            (I think) I have seen examples where the category filter used only part of the package name.  For example, if xxx.class is my.great.project.xxx then it could have used

             

            <logger category="my.great">
                 <level name="INFO"/>
                 <handlers>
                      <handler name="xxx-handler"/>
                 </handlers>
            </logger>

             

            Is it always true that the category filter matches if it is a prefix of the category? In other words, would

             

            <logger category="fo">
                 <level name="INFO"/>
                 <handlers>
                      <handler name="foo-handler"/>
                 </handlers>
            </logger>

             

            also log the line issued by Loggers.FOO_LOGGER.info("Test from FOO_LOGGER"); , as defined by your last post?

             

            Ulrich

            • 3. Re: logging into different log files from one class / categories
              jamezp

              (I think) I have seen examples where the category filter used only part of the package name.  For example, if xxx.class is my.great.project.xxx then it could have used

               

               

              <logger category="my.great">
                   <level name="INFO"/>
                   <handlers>
                        <handler name="xxx-handler"/>
                   </handlers>
              </logger>

               

              Correct, my.great.project would use the handlers in the category my.great (parent category). However the level would not be inherited.

               

               

               

              Is it always true that the category filter matches if it is a prefix of the category? In other words, would

               

              <logger category="fo">
                   <level name="INFO"/>
                   <handlers>
                        <handler name="foo-handler"/>
                   </handlers>
              </logger>

               

              also log the line issued by Loggers.FOO_LOGGER.info("Test from FOO_LOGGER"); , as defined by your last post?

              Think of a logger category as something like a path. In this case our path separator is a . (dot). The category gets parsed into different nodes, e.g. my.great is the parent node of my.great.project.

               

              The logger itself is configured based on the category, but will check parent loggers for handlers. That is that when you define a logging configuration for my.great only a logger created with the exact category my.great will use that configuration. The only exception to the rule is handlers. Handlers are inherited from parent loggers unless you explicitly tell the logger not to inherit handlers.

               

              I hope this makes sense.

               

              --

              James R. Perkins

              • 4. Re: logging into different log files from one class / categories
                wdfink

                As James mentioned you can avoid use of parent handler by adding:

                     <logger category="a.b.c" use-parent-handlers="false">

                 

                Otherwise the handler of this category AND all handlers of parent are receiving such log entries.

                • 5. Re: logging into different log files from one class / categories
                  ulrich-scholz

                  Do I understand correctly?

                   

                  There is no explict way to assign parent handlers. The parent handler hierarchy is implictly defined via logger

                  categories:

                  • logger LA is the parent of logger LB if the category of LA is a (path-)prefix of the category of LB
                  • handler HA is the parent of handler HB if each of the following is true
                    • LA is a logger that has HA as handler
                    • LB is a logger that has HB as handler
                    • LA is the parent logger of LB

                   

                  So in the example

                   

                  <logger category="a.b.c">

                  <handlers>
                     <handler name="HABC"/>

                  </handlers>

                   

                  <logger category="a">

                  <handlers>
                     <handler name="HA"/>

                  </handlers>

                   

                  HA is the parent handler of HABC because the category a is a path-prefix of a.b.c

                  • 6. Re: logging into different log files from one class / categories
                    jamezp

                    Ulrich Scholz wrote:

                     

                    Do I understand correctly?

                     

                    There is no explict way to assign parent handlers. The parent handler hierarchy is implictly defined via logger

                    categories:

                    • logger LA is the parent of logger LB if the category of LA is a (path-)prefix of the category of LB
                    • handler HA is the parent of handler HB if each of the following is true
                      • LA is a logger that has HA as handler
                      • LB is a logger that has HB as handler
                      • LA is the parent logger of LB

                     

                    So in the example

                     

                    <logger category="a.b.c">

                    <handlers>
                       <handler name="HABC"/>

                    </handlers>

                     

                    <logger category="a">

                    <handlers>
                       <handler name="HA"/>

                    </handlers>

                     

                    HA is the parent handler of HABC because the category a is a path-prefix of a.b.c

                     

                    I think you're understanding. One note though is that handlers are standalone and don't have parents or children. I know that sounds finicky, but they are different.

                     

                    --

                    James R. Perkins

                    • 7. Re: logging into different log files from one class / categories
                      ulrich-scholz

                      But if handlers do not have parents, why then does the <logger> tag have an attribute use-parent-handler?

                       

                      Ulrich

                      • 8. Re: logging into different log files from one class / categories
                        jamezp

                        That's because the logger has parents and there could be handlers attached to those loggers. I guess I'm being kind of picky though :-). Just think of it as "use the parent loggers handlers".

                         

                        --

                        James R. Perkins

                        1 of 1 people found this helpful