log4j - log message shown in different log files

I have a old java application using the following to create the log file called, schedule.log
=======================
public class CreateLog4J
{
   protected void LaunchLogFile() throws Exception
   {
    // Log layout handler
    schedulerLogger.setLevel(Level.INFO);
    schedulerLayout   = new SimpleLayout();
    schedulerAppender = new ConsoleAppender(schedulerLayout);
    schedulerLogger.addAppender(schedulerAppender);
    schedulerLogger.addAppender(new FileAppender(schedulerLayout, "scheduler.log"));
   }
   
  public static Logger schedulerLogger = Logger.getLogger("scheduler.log");
  Appender      schedulerAppender;
  SimpleLayout  schedulerLayout;
 
}
=========================
For further enhancement, we have created a log4j.xml file to create another log file called, payroll.log,
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
       
  <appender name="appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="payroll.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
  </appender>

  <root>
    <priority value ="debug"/>
    <appender-ref ref="appender"/>
  </root>

</log4j:configuration>

==========================
The problem is

The log message supposed to send to the schedule.log goes to the payroll.log

for example:
the below line is to send the log message to schedule.log
cl.schedulerLogger.info(sdf.format(new Date()) + " - Scheduler is currently up and running on terminal " + terminal_no);

======================
Notes: log4j.xml resides in the root folder
LeanMoreTryMoreAsked:
Who is Participating?
 
iXian_Commented:
Log4J loggers are hierarchal, with each logger being given a name separated by periods, similar to fully qualified java class names, for example:

    Logger log1 = Logger.getLogger("com.acme.foobar.Scheduler");
    Logger log2 = Logger.getLogger("com.acme.logthis");
    Logger log3 = Logger.getLogger("com.acme.payroll.Calculator");
    Logger log3 = Logger.getLogger("com.acme.payroll.Deposit");

Note that although the logger names could (and typically do) represent java class names, they can be anything, although levels in the hierarchy are represented as periods.  In the examples above, com.acme.foobar is a parent of com.acme.foobar.Scheduler, com.acme is a parent of all three, etc.
The "root" level is the top of the hierarchy, and a catchall for everything.  

Log4J appenders are attached to hierarchy levels, and log messages generated by any loggers at that level or below.  In your log4j.xml configuration file (presumably in the root of a directory or JAR file in your classpath), your code example specifies that the payroll.log file appender should log all events under root, including anything being generated by the "scheduler.log" logger.  Multiple appenders may log an event, and in your code example, the messages should be logging to both the payroll.log appender attached to the root level, and the scheduler.log appender attached at the "scheduler.log" level.  

Also, in your example, it seems that you're naming your logger "scheduler.log" when this is presumably where you want the log to go.  In general, you should use package/class names for your logger names, and map appenders to that naming hierarchy.

Here's an example of how you want to might want to create a logger:

    package com.acme.foobar;
    Class Scheduler {
        Logger log = Logger.getLogger("com.acme.foobar.Scheduler");
        public void method() {
           ...
           log.info("this is an informational message from the Scheduler class");
           ...
        }
    }

Here's an example that illustrates how they wire up.  Also here I show how you could move the configuration of the console appender out of the code and into configuration -- in general, you probably want to leave the configuration of your logging out of source
code.  

    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

      <!-- console appender for scheduler (and perhaps other stuff too) -->
      <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.SimpleLayout"/>
      </appender>

      <!-- payroll file appender -->
      <appender name="PayrollLogAppender" class="org.apache.log4j.FileAppender">
        <param name="File" value="payroll.log"/>
        <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
        </layout>
      </appender>

      <!-- everything info level or better goes to the console by default -->
      <root>
        <priority value="info"/>
        <appender-ref ref="schedulerAppender"/>
      </root>

      <!-- stuff under payroll namespace will also go to the payroll file -->
      <logger name="com.acme.payroll">
        <!-- Print only messages of level info or greater -->
        <level value="debug"/>
      </logger>

    </log4j:configuration>
0
 
objectsCommented:
>    protected void LaunchLogFile() throws Exception

does that method actually get called.
0
 
LeanMoreTryMoreAuthor Commented:
The LaunchLogFile gets called from the main program and i expect the log messge only send to schedule.log instead of both schedule.log and payroll.log
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
objectsCommented:
>   public static Logger schedulerLogger = Logger.getLogger("scheduler.log");

that gets called *before* LaunchLogFile gets called so will reference logger from config file
0
 
LeanMoreTryMoreAuthor Commented:
Many thanks object. it does make sense.

How do i resolve this? please advise
==================
      public static void main(String[] args) {
         userName  = args[0];
         pwd       = args[1];
         dbName    = args[2];

         try {
           int dbConnOk = Utilities.SQLJdbConnection(userName, pwd, dbName);          
         }  
         catch (Exception e) {
           e.printStackTrace();
           System.exit(1);            
         }
      }

      private UTeventScheduler() throws Exception  {
        if ((ml.checkFileSize() > 1500000) && (ml.renameFile()))  ml.deleteLogfile();      
        cl.LaunchLogFile();
.
.
.
0
 
objectsCommented:
looks like u need to change CreateLog4J class and assign schedulerLogger inside the method

0
 
LeanMoreTryMoreAuthor Commented:

I dont want the payroll.log created after the payrol process is initiated from within the job scheduler.
schedule.log is created from job scheduler

any idea. please help
0
 
objectsCommented:
u need to modify CreateLog4J, don't see any way around it.
Why don't u get rid of it alltogether?
0
 
LeanMoreTryMoreAuthor Commented:
ok. let say i get rid of the createLog4j class. so what can i do next
i only want to create the payroll.log file when the payroll process is initiated from within the scheduler
0
 
objectsCommented:
> i only want to create the payroll.log file when the payroll process is initiated from within the scheduler

not sure I follow, do you mean logging destination should change once payroll process is initiated?
0
 
LeanMoreTryMoreAuthor Commented:
if i use the following log4j.xml file. how to do i create 2 log file. and most important is i only want to create the payroll.log file where the payroll process is initiated

The following is used to create the schedule.log file
=======================================================================
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
       
  <appender name="appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="schedule.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
  </appender>

  <root>
    <priority value ="debug"/>
    <appender-ref ref="appender"/>
  </root>

</log4j:configuration>
0
 
objectsCommented:
u create another appender for payroll.log (as u have in your question)
then define a seperate logger that uses that appender
0
 
LeanMoreTryMoreAuthor Commented:
the scheduler will initiate the payroll process from within the job scheduler

scheduler will use the  Runtime.getRuntime().exec method to initiate the Payroll program

"C:\Java\jdk1.5\jre\bin\java.exe" -classpath "C:\corpdev6i\JavaAppl;C:\Java\jdk1.5\sqlj\lib\runtime12.jar;C:\Java\jdk1.5\lib\mail.jar;C:\Java\jdk1.5\lib\activation.jar;C:\Hibernate\hibernate-3.1\hibernate3.jar;C:\Hibernate\hibernate-3.1\lib\antlr-2.7.5H3.jar;C:\Hibernate\hibernate-3.1\lib\asm.jar;C:\Hibernate\hibernate-3.1\lib\asm-attrs.jar;C:\Hibernate\hibernate-3.1\lib\cglib-2.1.3.jar;C:\Hibernate\hibernate-3.1\lib\jaxen-1.1-beta-7.jar;C:\Hibernate\hibernate-3.1\lib\jdbc2_0-stdext.jar;C:\Hibernate\hibernate-3.1\lib\jta.jar;C:\Hibernate\hibernate-3.1\lib\log4j-1.2.11.jar;C:\Hibernate\hibernate-3.1\lib\commons-collections-2.1.1.jar;C:\Hibernate\hibernate-3.1\lib\commons-logging-1.0.4.jar;C:\Hibernate\hibernate-3.1\lib\xerces-2.6.2.jar;C:\Hibernate\hibernate-3.1\lib\xml-apis.jar;C:\Hibernate\hibernate-3.1\lib\ehcache-1.1.jar;C:\Hibernate\hibernate-3.1\lib\dom4j-1.6.1.jar;C:\Hibernate\hibernate-3.1\lib\jaas.jar;C:\Hibernate\hibernate-3.1\lib\classes12.jar;C:\Java\jdk1.5\jdbc\lib\ojdbc14dms.jar;C:\Java\jdk1.5\jdbc\lib\ocrs12.jar;C:\Java\jdk1.5\diagnostics\lib\ojdl.jar;C:\Java\jdk1.5\lib\dms.jar;C:\Java\jdk1.5\xdk\lib\xml.jar;C:\Java\jdk1.5\xdk\lib\xmlparserv2.jar"  dirHibernate.payroll Payroll Full null null gdev

Only the Payroll program is exected i want the payrol.log file being created
0
 
LeanMoreTryMoreAuthor Commented:
would u give me a simple example using a seperate logger that uses that appender
0
 
LeanMoreTryMoreAuthor Commented:
Can I change the log4j.xml like below
==========================================================================
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
       
  <appender name="appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="schedule.log"/>
    <param name="Append" value="true"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
  </appender>

  <root>
    <priority value ="debug"/>
    <appender-ref ref="appender"/>
  </root>

  <appender name="PayrollAppender" class="org.apache.log4j.FileAppender">
    <param name="File" value="payroll.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
  </appender>

  <root>
    <priority value ="debug"/>
    <appender-ref ref="PayrollAppender"/>
  </root>

</log4j:configuration>
0
 
LeanMoreTryMoreAuthor Commented:
I change the log4j.xml like below

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="SchedulerAppender" class="org.apache.log4j.FileAppender">
    <param name="File" value="schedule.log"/>
    <param name="Append" value="true"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
  </appender>        

  <appender name="PayrollAppender" class="org.apache.log4j.FileAppender">
    <param name="File" value="Ppar.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
  </appender>

  <root>
    <priority value ="debug"/>
    <appender-ref ref="SchedulerAppender"/>
  </root>

</log4j:configuration>
================================================================
HOw do i assign the SchedulerAppender?

public class Scheduler extends MailUtilities implements EvtConfig {
    // Protected variables to be used as IN parameters
   
    private static final Log log = LogFactory.getLog(Scheduler .class);
0
 
LeanMoreTryMoreAuthor Commented:
Now my log4j.xml file like below:
===========================================================================
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="SchedulerAppender" class="org.apache.log4j.FileAppender">
    <param name="File" value="schedule.log"/>
    <param name="Append" value="true"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
  </appender>        

  <appender name="PayrollAppender" class="org.apache.log4j.FileAppender">
    <param name="File" value="Ppar.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
  </appender>

  <root>
    <priority value ="debug"/>
    <appender-ref ref="SchedulerAppender"/>
  </root>

</log4j:configuration>
===============================================================================
My schedule program to use the logfacotry

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Scheduler extends MailUtilities implements EvtConfig {
    private static final Log log = LogFactory.getLog(UTeventScheduler.class);
.
.
    log.debug(" - Scheduler is currently up and running on terminal " + terminal_no);


========================================================================
I got the following error:

log4j:ERROR The content of element type "log4j:configuration" must match "(renderer*,appender*,(category|logger)*,root?,categoryFactory?)".

======================================================================
Why I got the above error
0
 
objectsCommented:
see the example posted above by iXian

to get logger use:

  public static final Logger schedulerLogger = Logger.getLogger(Scheduler.class);
0
 
LeanMoreTryMoreAuthor Commented:
public static final Logger schedulerLogger = Logger.getLogger(Scheduler.class);

How do i know which appender it uses
0
 
objectsCommented:
selects looger that matches name, and then logs to its appenders
0
 
LeanMoreTryMoreAuthor Commented:
Thanks for both iXian and Object
0
 
LeanMoreTryMoreAuthor Commented:
How do i split the point as the web page looks different as used to be?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.