[Logo]
Forums Register Login
RollingFileAppender not working with multiple appenders to same file
(1 like)
I am using log4j where I need to create logging for multiple appenders into a single file.

Even I have used RollingFileAppender, the file is not rolling up and logging is written into single file.
Now if I remove the dbLogger appender, then the Rolling of file get succeed. Means, if multiple appenders are writing to same file, the RollingFileAppender stops behaving correctly.

Is there any workaround to this ?
Where does RollingFileAppender come from?
 

Campbell Ritchie wrote:Where does RollingFileAppender come from?


It's under the log4j framework.
 

Puspender Tanwar wrote:Even I have used RollingFileAppender, the file is not rolling up and logging is written into single file.
Now if I remove the dbLogger appender, then the Rolling of file get succeed. Means, if multiple appenders are writing to same file, the RollingFileAppender stops behaving correctly.

Is there any workaround to this ?



Your description is perfectly clear (we don't often get clear problem descriptions here). And if you read it over, you'll see it contains a description of the solution.
 

Paul Clapham wrote:Your description is perfectly clear (we don't often get clear problem descriptions here). And if you read it over, you'll see it contains a description of the solution.


Hi Paul, Where is the solution? it contains the description of the problem, but not the workaround/solution to this.
 

You wrote:Now if I remove the dbLogger appender, then the Rolling of file get succeed.

 
But that doesn't solve my requirement. I want dbLogger and logAppender to log under the same file.
Well, log4j is open source so you could start working on a fix for that problem. But my workaround would be to use only one appender rather than two, since you know there isn't a problem there.
If you're using Windows and you have two separate appenders trying to write to the same file, then probably one appender can't close and rename the file because the other appender still has a lock on it. Actually I'm surprised that you are even getting logs from both appenders reliably, I wouldn't be surprised if you have write conflicts and lost buffers between the two appenders.
An appender indicates a single logging destination. It doesn't make sense to define a destination twice. If you don't want Hibernate and your app to log to the same destination, then define a different destination (file pattern) for one of them.

Otherwise, define a single destination and target both your org.hibernate and regular logging sources to it.

Having multiple writers to a single file should only be done if you understand how file and record locking work on the OS you're using and program accordingly. Otherwise, one of the writers may be denied access, or worse, the two writers can smash into each other and in many cases you'll end up with data that's out of order or worse still, data from one record wedged in the middle of another record, resulting in garbage output.
 

Puspender Tanwar wrote:I am using log4j where I need to create logging for multiple appenders into a single file.



Perhaps it would help if you mentioned why you need more than one appender for the file?
 

Paul Clapham wrote:If you're using Windows and you have two separate appenders trying to write to the same file, then probably one appender can't close and rename the file because the other appender still has a lock on it

what is the case with UNIX or LINUX?

Tim Holloway wrote:define a single destination and target both your org.hibernate and regular logging sources to it. 

Even if the OS allows two writers to write to the same file, in that case the rolling feature doesn't work.

Paul Clapham wrote:Perhaps it would help if you mentioned why you need more than one appender for the file?


I am having four projects(as executable jar). The package they are under are:

So for maintaining a single logging file for all the projects(which is my requirement), I made an appender on com.bcone.oracle.ebs which is the parent package of all the projects. This fulfills my requirement up to some extent, but the Rolling feature stopped behaving. Because writing is done from four different locations(projects).
The other requirement was to append the Hibernate logs into the same logging file. So I made an appender for org.hibernate pointing to the same log file. And as above(four projects writing to the same file), two appenders disturbed the Rolling feature.
You did this:



where dbLogger and logAppender are identical duplicate appenders. You noted that this has problems which don't occur when you have only one appender. So why not have only one appender and allow both loggers to use it?

 

Paul Clapham wrote: So why not have only one appender and allow both loggers to use it?

I tried this one as well. When I used only one appender, the second value of appender overrides the first one. So org.hibernate get overridden by com.bcone.oracle.ebs  and only com.bcone.oracle.ebs logging gets logged in the file.
That could mean that org.hibernate isn't producing any error logs, you know.
And if you think that overriding is taking place, then try this:


 

Paul Clapham wrote:And if you think that overriding is taking place, then try this:



This time, only hibernate logs are there in log file. Hibernate overrides the com.bcone.oracle.ebs
Well, I'm surprised by that. When I was building log4j configuration files I would regularly map several packages to one appender. And I did use RollingFileAppender too. Never had any problems.

But if you only need one appender, why not just use the root logger like in almost every example of log4j configuration file which you can find on the web?
 

Paul Clapham wrote:But if you only need one appender, why not just use the root logger like in almost every example of log4j configuration file which you can find on the web?

root logger also stopped Rolling when different projects started logging. If single project logs into the file, Rolling works.
Sanity check: these two things, EBS and Hibernate, they are running in the same class loader, right? If they were running in different class loaders then they would each run their own copy of log4j, essentially making them different applications trying to do simultaneous access to the log file.
 

Paul Clapham wrote:Sanity check: these two things, EBS and Hibernate, they are running in the same class loader, right? If they were running in different class loaders then they would each run their own copy of log4j, essentially making them different applications trying to do simultaneous access to the log file.


Yes, EBS and Hibernate are running on same classloader.

This thread has been viewed 505 times.

All times above are in ranch (not your local) time.
The current ranch time is
Sep 22, 2018 12:46:14.