每个模块的 Log4j 属性

发布于 2024-09-25 20:57:56 字数 453 浏览 3 评论 0原文

我正在编写一个由多个 Maven 模块组成的应用程序。他们所有人都必须将内容记录到自己的日志文件中。我在每个模块的 main/resources 文件夹中创建了一个 log4j.properties 文件。现在,当我从其中一个模块启动应用程序时,它将所有内容写入该模块的日志文件中。我想知道这是否只是因为它可能从该启动模块中获取了 root-dir 并使用它来保存日志文件,但是当我为该启动模块创建一个附加程序时(使用 log4j 属性中的类别)并且no rootLogger 它给了我一个关于其他模块中无法找到其附加程序的类的错误,这表明每个模块中的 log4j.properties 文件未找到或读取。任何人有任何想法如何解决这个问题并使这些模块使用这些属性文件,或者我是否必须将所有附加程序放在一个大的 log4j.properties 文件中,导致所有日志出现在该启动模块的根文件夹中而不是那些其他模块的根文件夹?

我希望问题足够清楚以便理解,否则请随时询问详细信息。

I'm writing an application that consists of several maven modules. All of them have to log stuff to their own log files. I created a log4j.properties file in the main/resources folder of each module. Now when I start the application from one of the modules, it writes everything to the log file of that module. I wondered if this was just because it might have taken the root-dir from that start-module and used that to save the log files, but when I create an appender for just that start-module (using category in the log4j properties) and no rootLogger it gives me an error about classes in the other modules that can't find their appenders, which indicates the log4j.properties files in each of those modules isn't found or read. Anyone any ideas how to solve this and make those modules use those properties files, or do I have to put all the appenders in one big log4j.properties file, causing all the logs to appear in the root folder of that start-module instead of the root folders of those other modules?

I hope the question is clear enough to understand, otherwise feel free to ask for details.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最初的梦 2024-10-02 20:57:56

据我所知,默认情况下仅读取并初始化被调用模块的 log4j.properties 。

如果您想从单个启动模块调用单独的模块,则必须将所有附加程序添加到位于 start-module/src/main/resources 目录中的单个 log4j.properties 文件中。如果以及何时您想要这可能是一个架构问题。

只是一个建议:如果您现在或将来需要更高级的日志记录控制,请立即切换到基于 XML 的日志记录。

As far as I know only the log4j.properties of the invoked module is read and initialized by default.

If you want to invoke the separate modules from a single start-module, you'll have to add all your appenders to a single log4j.properties file located in the start-module/src/main/resources directory. If and when you'd want this is probably a architecture question.

Just a suggestion: Switch to XML-based logging now if you want more advanced logging controls now or in the future.

白芷 2024-10-02 20:57:56

如果您在每个单独模块的 src/main/resources 中包含单独的 log4j.properties 并将生成的 jar 文件加载到单个类加载器中,则只会加载一个资源,因为 log4j 从类加载器加载属性文件(您的资源在文件名)。

我不会在我的任何 jar 文件中包含 log4j.configuration 文件。有两个原因:

  • 不适用于您的情况。

  • 为了配置 log4j(编辑记录器的详细程度),我必须打开 jar 文件并编辑属性文件。

不同的解决方案可以是让另一个模块生成阴影( http://maven.apache. org/plugins/maven-shade-plugin/) 来自其余模块的 jar 文件,包括 src/main/resources 中的单个 log4j.properties。然后,如果您有另一个模块将以前的项目用作依赖项,则只能将阴影模块作为依赖项包含在内。

但是,这是我不会使用的解决方案。

希望有帮助。

If you include separate log4j.properties in the src/main/resources of each separate module and the load the generated jar files into a single classloader, only one resource will be loaded since log4j loads the properties files from the classloader (your resources overlap in filename).

I wouldn't include log4j.configuration files in any of my jar files. For two reasons:

  • Won't work in your case.

  • In order to configure log4j (editing verbosity of loggers), I would have to open the jar file and edit the properties file.

A different solution could be make another module producing a shaded ( http://maven.apache.org/plugins/maven-shade-plugin/) jar file from the remainder modules, including a single log4j.properties in src/main/resources. Then, if you have another module consuming your previous projects as dependencies, you could only include as a dependency the shaded one.

However, that's a solution I wouldn't use.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文