1 Reply Latest reply on Feb 10, 2008 9:49 AM by jaikiran pai

    log4j: loggin to different files + sheduler

    anny_lut Newbie

      I have pack: mypack.myclass
      in jboss(3.2.1) I configure 2 shedulers, which call myclass method but with different parameters (parameters define log4j category for this sheduler)
      scheduler-service.xml :

       <mbean code="org.jboss.varia.scheduler.Scheduler" name=":service=Scheduler1">
       ...
       <attribute name="SchedulableClass">mypack.myclass</attribute>
       <attribute name="SchedulableArguments">paramValue1</attribute>
       <attribute name="SchedulableArgumentTypes">java.lang.String</attribute>
       ....
       </mbean>
      
       <mbean code="org.jboss.varia.scheduler.Scheduler" name=":service=Scheduler2">
       ...
       <attribute name="SchedulableClass">mypack.myclass</attribute>
       <attribute name="SchedulableArguments">paramValue2</attribute>
       <attribute name="SchedulableArgumentTypes">java.lang.String</attribute>
       ....
       </mbean>
      


      In mypack.myclass constructor I initialize log4j category, depending on input parameter:
      public class myclass{
       protected static Logger log;
      
       public myclass(String log4jcategory) {
       log = Logger.getLogger(log4jCategory);
       }
       ....
      }
      



      in file all\conf\log4j.xml I describe this two categories:
      <appender name="LOG1" class="org.jboss.logging.appender.DailyRollingFileAppender">
       ....
       </appender>
       <appender name="LOG2" class="org.jboss.logging.appender.DailyRollingFileAppender">
       ....
       </appender>
      
       <category name="paramValue1">
       <appender-ref ref="LOG1"/>
       ...
       </category>
      
       <category name="paramValue2">
       <appender-ref ref="LOG2"/>
       ...
       </category>
      



      But as a result, when sheduler starts .... from the begining log4j puts logdata into 2 different files, but at the 3-d itteration of sheduler all data writes to one file. And this one file choose randomly.

      Why logdata is writtn to one log?

        • 1. Re: log4j: loggin to different files + sheduler
          jaikiran pai Master

           

          public class myclass{
           protected static Logger log;
          
           public myclass(String log4jcategory) {
           log = Logger.getLogger(log4jCategory);
           }
           ....
          }
          


          This looks wrong. You are maintaining one class level variable which can only hold one logger at a time. So if scheduler1 calls this first and then scheduler2 calls this later with a different parameter, then the 'log' member variable will hold reference to the logger for scheduler2. You will no longer have reference of the logger of scheduler1. You will have to rewrite this myclass, to fix this.