简体   繁体   中英

How to make 2 or more java project write in a single log file

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?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM