简体   繁体   English

我应该将log4j.properties文件放入库中吗?

[英]Should I put log4j.properties file into library?

I have a library project. 我有一个图书馆项目。 This project uses log4j for logging. 该项目使用log4j进行日志记录。

Should I put log4j.properties into generated jar? 我应该将log4j.properties放入生成的jar中吗?
If it is not a good practice, could you tell me why? 如果这不是一个好习惯,你能告诉我为什么吗?

You should put your log4j.properties outside your jar file because it will be easier to modify it. 您应该将log4j.properties放在jar文件之外,因为它更容易修改它。 If the application does not force a reload of the configuration (you can do that) then a simple restart will load the config (while modifying it inside a jar file means usually a rebuild). 如果应用程序没有强制重新加载配置(你可以这样做),那么简单的重启将加载配置(在jar文件中修改它通常意味着重建)。 If the application does know how to reload it, then your changes will be almost instantly applied. 如果应用程序确实知道如何重新加载它,那么您的更改几乎会立即应用。 This is very important when you have application in production environment, and you want to change a logging setting (like the log level) in order to get more info but not to stop the application for this (or worse, rebuild it). 当您在生产环境中具有应用程序时,这非常重要,并且您希望更改日志记录设置(如日志级别)以获取更多信息但不要为此停止应用程序(或者更糟糕的是,重建它)。

No, I would not include log4j.properties in your src/main/resources if it's just a library - people will use your library, and people will have to configure how it should log. 不,我不会在你的src/main/resources包含log4j.properties ,如果它只是一个库 - 人们将使用你的库,人们将不得不配置它应该如何记录。

But there is another problem: if you put it in your jar, others will have the log4j in their classpath too. 但还有另一个问题:如果你把它放在你的jar中,其他人也会在他们的类路径中有log4j。 So, if they want to have a custom configuration for log4j (which they will do), the ClassLoader will pick the first file it can find. 因此,如果他们想要为log4j进行自定义配置(他们将这样做),ClassLoader将选择它可以找到的第一个文件。

Also, if you have a library, I would suggest to use slf4j instead, so your users can choose their favourite logging framework. 另外,如果你有一个库,我建议改用slf4j ,这样你的用户就可以选择自己喜欢的日志框架了。

For testing purpouses, it's okay to include log4j in src/test/resources . 对于测试purpouses,可以在src/test/resources包含log4j。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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