0

I am using log4j to write all the logs in a file. i have 2 different java projects say proj1 and proj2, where project1 is required project for proj2. I have added proj1 as a dependency for proj2.

proj1 has log4j setup done and is working fine.
Now my problem is when I am running a method in proj2, it will call proj1 as well.
So I want to have a single logfile for both the projects.

Any input please?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Pratik
  • 952
  • 2
  • 16
  • 31

2 Answers2

1

There are several ways to write to a single log file but which way is best depends on a couple of details which you omit.

If proj2 includes proj1 as a library, you can make it use proj1's log4j configuration file. This works because you only have a single VM. The most simple solution here is to either copy the first project's config into the other or not give the second project any log config; it will then read the config from its dependencies.

If proj2 starts proj1 as an external process, you need to configure both projects to use a SocketAppender since only a single Java VM can ever write to a single log file.

Related:

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thanks Aaron. I have made it working. Here is what i have done.I just copied proj1's log4j.properties file to proj2 and then i had single log for any execution between proj1 and proj2. – Pratik Aug 29 '13 at 12:00
  • you may use root logger configuration for external appender.there will be no need of raplacing or copying log4j.xml. – Avyaan Aug 29 '13 at 13:45
0

I was also facing the same problem but i got the solution and have configured my log4j.xml like this:

used a appender:

<appender name="FILE1" class="org.apache.log4j.RollingFileAppender">
        <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler" />
        <param name="File" value="E:/OESController.log" />
        <param name="Append" value="true" />
        <param name="MaxFileSize" value="20KB" />
        <param name="MaxBackupIndex" value="2" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p: %d{dd MMM yyyy HH:mm:ss.SSS} %-5l - %m%n%n" />
        </layout>
    </appender>


<!-- Root Logger -->
    <root>
        <priority value="error" />
            <appender-ref ref="FILE" />     
    </root>

Note:*Root logger logs for entire application.*

let me know if you still face the problem.

Avyaan
  • 1,285
  • 6
  • 20
  • 47