You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Eshwaramoorthy Babu <ba...@gmail.com> on 2006/07/27 17:17:58 UTC

Duplicate messages in the log file

Hello all,

I am using below code to log the mesage and I am getting duplicate messages
in the log file.
I am passing the EAILogger class instance to different classes and invoking
the log method.

Can any one help me to resolve this issue.



/////////////////////////Source Code

import org.apache.log4j.*;

public class EAILogger
{
    private Logger logger = null;
    public EBEAILogger()
    {
 try
 {
 String fileName = "TEST.LOG";
 String datePattern = "yyyy-MM-dd.'log'";
 String logPattern = "%d %-2p [%c{3}] - %m - [%F:%M:%L] %n";

 Layout layout = new PatternLayout(logPattern);
 DailyRollingFileAppender appender = new DailyRollingFileAppender(layout,
fileName, datePattern);
 logger = Logger.getRootLogger();
 logger.addAppender(appender);
 logger.setAdditivity(false);

 }
 catch(Exception e)
 {
  e.printStackTrace();
 }
 }

    public  void log(String message)
    {
 logger.info(message);
    }
}


/////////Log file

2006-07-27 19:07:41,289 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 - [?:log:?]

2006-07-27 19:07:52,293 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 - [?:log:?]

2006-07-27 19:07:52,293 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 - [?:log:?]

Re: Duplicate messages in the log file

Posted by James Stauffer <st...@gmail.com>.
If you can set a system property then you can use that to set the
filename.  You still may want to use the config file and just have
code change the appender filename.

On 7/30/06, Eshwaramoorthy Babu <ba...@gmail.com> wrote:
> James/Javier, Thanks for your help.
> I can not use the static config file because my requirement is to set the
> log file name at runtime. I do not see any other approach other than setting
> the appender in the code.
>
> I have use your 1st approach(static block).
>
> Thanks again,
> Babu
>
>
> On 7/29/06, Javier Gonzalez <ja...@gmail.com> wrote:
> >
> > On 7/29/06, Eshwaramoorthy Babu <ba...@gmail.com> wrote:
> > > James you are right. I am doing 'new' every time. Can you please tell me
> > why
> > > this is happening. Because I am calling the log method in the current
> > > instance.
> >
> > Loggers are static objects. Each time you call new on your class, you
> > are creating a new Appender instance and attaching it to the same root
> > logger, so you get n appenders (n = number of times you have called
> > new on your wrapper) processing every message that is sent to the root
> > logger, which results in n duplicates.
> >
> > To fix this, I can see two alternatives:
> >
> > 1.- Use a static block to create and initialize the appender so that
> > only one gets created at class loading instead of a new appender for
> > every instance, or...
> > 2.- ...use a configuration file to define the appender.
> >
> > I prefer #2 - it's cleaner, you can modify your logging configuration
> > without having to edit code. The way you have it right now, even
> > changing the date format in the log messages implies that you have to
> > edit code, recompile and redeploy your app.
> >
> > Cheers,
> >
> > --
> > Javier González Nicolini
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > For additional commands, e-mail: log4j-user-help@logging.apache.org
> >
> >
>
>


-- 
James Stauffer
Are you good? Take the test at http://www.livingwaters.com/good/

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Duplicate messages in the log file

Posted by Eshwaramoorthy Babu <ba...@gmail.com>.
James/Javier, Thanks for your help.
I can not use the static config file because my requirement is to set the
log file name at runtime. I do not see any other approach other than setting
the appender in the code.

I have use your 1st approach(static block).

Thanks again,
Babu


On 7/29/06, Javier Gonzalez <ja...@gmail.com> wrote:
>
> On 7/29/06, Eshwaramoorthy Babu <ba...@gmail.com> wrote:
> > James you are right. I am doing 'new' every time. Can you please tell me
> why
> > this is happening. Because I am calling the log method in the current
> > instance.
>
> Loggers are static objects. Each time you call new on your class, you
> are creating a new Appender instance and attaching it to the same root
> logger, so you get n appenders (n = number of times you have called
> new on your wrapper) processing every message that is sent to the root
> logger, which results in n duplicates.
>
> To fix this, I can see two alternatives:
>
> 1.- Use a static block to create and initialize the appender so that
> only one gets created at class loading instead of a new appender for
> every instance, or...
> 2.- ...use a configuration file to define the appender.
>
> I prefer #2 - it's cleaner, you can modify your logging configuration
> without having to edit code. The way you have it right now, even
> changing the date format in the log messages implies that you have to
> edit code, recompile and redeploy your app.
>
> Cheers,
>
> --
> Javier González Nicolini
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: Duplicate messages in the log file

Posted by Javier Gonzalez <ja...@gmail.com>.
On 7/29/06, Eshwaramoorthy Babu <ba...@gmail.com> wrote:
> James you are right. I am doing 'new' every time. Can you please tell me why
> this is happening. Because I am calling the log method in the current
> instance.

Loggers are static objects. Each time you call new on your class, you
are creating a new Appender instance and attaching it to the same root
logger, so you get n appenders (n = number of times you have called
new on your wrapper) processing every message that is sent to the root
logger, which results in n duplicates.

To fix this, I can see two alternatives:

1.- Use a static block to create and initialize the appender so that
only one gets created at class loading instead of a new appender for
every instance, or...
2.- ...use a configuration file to define the appender.

I prefer #2 - it's cleaner, you can modify your logging configuration
without having to edit code. The way you have it right now, even
changing the date format in the log messages implies that you have to
edit code, recompile and redeploy your app.

Cheers,

-- 
Javier González Nicolini

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Duplicate messages in the log file

Posted by James Stauffer <st...@gmail.com>.
Each time create a new instance of that class you get the same logger
but you add a new appender to it.  What is the point of that class?
It may not even be needed.

On 7/29/06, Eshwaramoorthy Babu <ba...@gmail.com> wrote:
> James you are right. I am doing 'new' every time. Can you please tell me why
> this is happening. Because I am calling the log method in the current
> instance.
>
> Thanks,
> Babu
> On 7/27/06, James Stauffer <st...@gmail.com> wrote:
> >
> > If you "new" that class muliple times I could see how that might happen.
> >
> > Is there any reason that you can't use a config file?  That is more
> > standard and easier to configure.
> >
> > On 7/27/06, Eshwaramoorthy Babu <ba...@gmail.com> wrote:
> > > Hello all,
> > >
> > > I am using below code to log the mesage and I am getting duplicate
> > messages
> > > in the log file.
> > > I am passing the EAILogger class instance to different classes and
> > invoking
> > > the log method.
> > >
> > > Can any one help me to resolve this issue.
> > >
> > >
> > >
> > > /////////////////////////Source Code
> > >
> > > import org.apache.log4j.*;
> > >
> > > public class EAILogger
> > > {
> > >     private Logger logger = null;
> > >     public EBEAILogger()
> > >     {
> > >  try
> > >  {
> > >  String fileName = "TEST.LOG";
> > >  String datePattern = "yyyy-MM-dd.'log'";
> > >  String logPattern = "%d %-2p [%c{3}] - %m - [%F:%M:%L] %n";
> > >
> > >  Layout layout = new PatternLayout(logPattern);
> > >  DailyRollingFileAppender appender = new
> > DailyRollingFileAppender(layout,
> > > fileName, datePattern);
> > >  logger = Logger.getRootLogger();
> > >  logger.addAppender(appender);
> > >  logger.setAdditivity(false);
> > >
> > >  }
> > >  catch(Exception e)
> > >  {
> > >   e.printStackTrace();
> > >  }
> > >  }
> > >
> > >     public  void log(String message)
> > >     {
> > >  logger.info(message);
> > >     }
> > > }
> > >
> > >
> > > /////////Log file
> > >
> > > 2006-07-27 19:07:41,289 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 -
> > [?:log:?]
> > >
> > > 2006-07-27 19:07:52,293 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 -
> > [?:log:?]
> > >
> > > 2006-07-27 19:07:52,293 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 -
> > [?:log:?]
> > >
> > >
> >
> >
> > --
> > James Stauffer
> > Are you good? Take the test at http://www.livingwaters.com/good/
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> > For additional commands, e-mail: log4j-user-help@logging.apache.org
> >
> >
>
>


-- 
James Stauffer
Are you good? Take the test at http://www.livingwaters.com/good/

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Duplicate messages in the log file

Posted by Eshwaramoorthy Babu <ba...@gmail.com>.
James you are right. I am doing 'new' every time. Can you please tell me why
this is happening. Because I am calling the log method in the current
instance.

Thanks,
Babu
On 7/27/06, James Stauffer <st...@gmail.com> wrote:
>
> If you "new" that class muliple times I could see how that might happen.
>
> Is there any reason that you can't use a config file?  That is more
> standard and easier to configure.
>
> On 7/27/06, Eshwaramoorthy Babu <ba...@gmail.com> wrote:
> > Hello all,
> >
> > I am using below code to log the mesage and I am getting duplicate
> messages
> > in the log file.
> > I am passing the EAILogger class instance to different classes and
> invoking
> > the log method.
> >
> > Can any one help me to resolve this issue.
> >
> >
> >
> > /////////////////////////Source Code
> >
> > import org.apache.log4j.*;
> >
> > public class EAILogger
> > {
> >     private Logger logger = null;
> >     public EBEAILogger()
> >     {
> >  try
> >  {
> >  String fileName = "TEST.LOG";
> >  String datePattern = "yyyy-MM-dd.'log'";
> >  String logPattern = "%d %-2p [%c{3}] - %m - [%F:%M:%L] %n";
> >
> >  Layout layout = new PatternLayout(logPattern);
> >  DailyRollingFileAppender appender = new
> DailyRollingFileAppender(layout,
> > fileName, datePattern);
> >  logger = Logger.getRootLogger();
> >  logger.addAppender(appender);
> >  logger.setAdditivity(false);
> >
> >  }
> >  catch(Exception e)
> >  {
> >   e.printStackTrace();
> >  }
> >  }
> >
> >     public  void log(String message)
> >     {
> >  logger.info(message);
> >     }
> > }
> >
> >
> > /////////Log file
> >
> > 2006-07-27 19:07:41,289 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 -
> [?:log:?]
> >
> > 2006-07-27 19:07:52,293 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 -
> [?:log:?]
> >
> > 2006-07-27 19:07:52,293 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 -
> [?:log:?]
> >
> >
>
>
> --
> James Stauffer
> Are you good? Take the test at http://www.livingwaters.com/good/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: Duplicate messages in the log file

Posted by James Stauffer <st...@gmail.com>.
If you "new" that class muliple times I could see how that might happen.

Is there any reason that you can't use a config file?  That is more
standard and easier to configure.

On 7/27/06, Eshwaramoorthy Babu <ba...@gmail.com> wrote:
> Hello all,
>
> I am using below code to log the mesage and I am getting duplicate messages
> in the log file.
> I am passing the EAILogger class instance to different classes and invoking
> the log method.
>
> Can any one help me to resolve this issue.
>
>
>
> /////////////////////////Source Code
>
> import org.apache.log4j.*;
>
> public class EAILogger
> {
>     private Logger logger = null;
>     public EBEAILogger()
>     {
>  try
>  {
>  String fileName = "TEST.LOG";
>  String datePattern = "yyyy-MM-dd.'log'";
>  String logPattern = "%d %-2p [%c{3}] - %m - [%F:%M:%L] %n";
>
>  Layout layout = new PatternLayout(logPattern);
>  DailyRollingFileAppender appender = new DailyRollingFileAppender(layout,
> fileName, datePattern);
>  logger = Logger.getRootLogger();
>  logger.addAppender(appender);
>  logger.setAdditivity(false);
>
>  }
>  catch(Exception e)
>  {
>   e.printStackTrace();
>  }
>  }
>
>     public  void log(String message)
>     {
>  logger.info(message);
>     }
> }
>
>
> /////////Log file
>
> 2006-07-27 19:07:41,289 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 - [?:log:?]
>
> 2006-07-27 19:07:52,293 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 - [?:log:?]
>
> 2006-07-27 19:07:52,293 INFO [root] - Hello!!!!!!!!!!!!!!!!!!!!1 - [?:log:?]
>
>


-- 
James Stauffer
Are you good? Take the test at http://www.livingwaters.com/good/

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org