[英]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. 我正在使用log4j
将所有日志写入文件中。 i have 2 different java projects say proj1
and proj2
, where project1
is required project for proj2
. 我有2个不同的Java项目,分别是proj1
和proj2
,其中project1
是proj2
必需项目。 I have added proj1
as a dependency for proj2
. 我已经添加了proj1
作为proj2
的依赖proj2
。
proj1 has log4j
setup done and is working fine. proj1已完成log4j
设置,并且工作正常。
Now my problem is when I am running a method in proj2
, it will call proj1
as well. 现在我的问题是当我在proj2
运行一个方法时,它也会调用proj1
。
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. 如果proj2
包含proj1
作为库,则可以使它使用proj1
的log4j配置文件。 This works because you only have a single VM. 之所以可行,是因为您只有一个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. 如果proj2
作为外部进程启动proj1
,则需要将两个项目都配置为使用SocketAppender,因为只有单个Java VM可以写入单个日志文件。
Related: 有关:
I was also facing the same problem but i got the solution and have configured my log4j.xml like this: 我也面临着同样的问题,但是我找到了解决方案,并按如下方式配置了log4j.xml:
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. 如果您仍然遇到问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.