[英]How to Change log4j Configuration When Packaging
I have a fairly simple Java application that is being distributed out to users as an executable JAR file. 我有一个相当简单的Java应用程序,它以可执行JAR文件的形式分发给用户。 In order to track usage and assist in diagnostics, I want to add logging capability to the application but, from one execution to the next, I can never be sure where the users are going to be running this application. 为了跟踪使用情况并协助诊断,我想向应用程序添加日志记录功能,但是,从一个执行到下一个执行,我永远无法确定用户将在哪里运行该应用程序。
In order to make this work, I have configured my application to use a log4j socket appender - no matter where the user is running this application, messages are sent to the socket and properly logged, which is perfect. 为了使此工作正常进行,我已将应用程序配置为使用log4j套接字附加程序-不管用户在何处运行此应用程序,都将消息发送到套接字并正确记录,这是完美的。
What I'm running into now, though, it that I would prefer to use different log4j configurations when I am running locally vs. when I deploy this application into production. 不过,我现在正在运行的是,与在将应用程序部署到生产环境中时相比,我宁愿在本地运行时使用不同的log4j配置。 When running locally (from my IDE, IntelliJ), I would prefer to use a console appender for a couple reasons: 在本地运行时(从我的IDE IntelliJ),出于以下几个原因,我希望使用控制台附加程序:
When I package the application and distribute it, however, I would like it to use the socket appender, rather than the console appender. 但是,当我打包应用程序并分发它时,我希望它使用套接字附加程序,而不是控制台附加程序。
Is there any easy way to accomplish what I want? 有什么简单的方法可以完成我想要的吗? I am using Maven to build my application, but I am not horribly skilled with it. 我正在使用Maven来构建我的应用程序,但是我对此并不熟练。
This turned out to be simpler than I originally expected - and Vikdor's comment led me to the right answer. 事实证明,这比我最初预期的要简单-Vikdor的评论使我找到了正确的答案。 I had started digging into having multiple profiles (one for dev and one for prod) but later realized I didn't really need to have two. 我开始研究具有多个配置文件(一个用于开发人员,一个用于产品),但后来意识到我并不需要两个。 When working locally, I wasn't really going through the Maven build process - I was just executing the Java classes. 在本地工作时,我并没有真正完成Maven的构建过程-我只是在执行Java类。 The only time I went through the Maven build process was when I wanted to package my application for deployment. 我唯一经历过Maven构建过程的时候,是我想打包应用程序以进行部署。 As such, I simply added a section to my POM file that uses the maven-antrun-plugin. 这样,我只是在使用Maven-antrun-plugin的POM文件中添加了一个部分。 My final code ended up looking like this: 我的最终代码最终如下所示:
File: log4j.properties: 文件:log4j.properties:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
File: log4j_production.properties 文件:log4j_production.properties
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A2
log4j.appender.A2=org.apache.log4j.net.SocketAppender
log4j.appender.A2.remoteHost=my.server.com
log4j.appender.A2.port=6000
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
File: POM.xml 档案:POM.xml
...
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/log4j.properties"/>
<copy file="src/main/resources/log4j_production.properties"
toFile="${project.build.outputDirectory}/log4j.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
...
That seemed to do the trick. 这似乎可以解决问题。 When I run locally, I load log4j.properties, which gives me a console appender. 在本地运行时,我会加载log4j.properties,这为我提供了一个控制台附加程序。 When I package for a production distribution, Maven will replace the default log4j.properties file with the alternate one, which uses a socket appender. 当我打包生产发行版时,Maven将使用备用套接字替换默认的log4j.properties文件。
Thanks! 谢谢!
Here's a simpler approach to your problem... 这是解决问题的一种更简单的方法...
Put two log4j.properties files under src/main/resources
directory, like this:- 将两个log4j.properties文件放在src/main/resources
目录下,如下所示:
src/main/resources
|- log4j.properties
|- log4j_dev.properties
log4j.properties
contains the production configuration, and by default, Log4J will automatically pick this file up because you are using the standard file name. log4j.properties
包含生产配置,默认情况下,由于您使用的是标准文件名,因此Log4J将自动选择此文件。
log4j_dev.properties
contains the development configuration. log4j_dev.properties
包含开发配置。 To ensure IntelliJ picks this up during development, you will overwrite the log configuration using -Dlog4j.configuration=<configuration-file>
VM option. 为了确保IntelliJ在开发过程中能够做到这一点,您将使用-Dlog4j.configuration=<configuration-file>
VM选项覆盖日志配置。 I attached a screenshot on how you might configure this using IntelliJ:- 我附上了有关如何使用IntelliJ进行配置的屏幕截图:
I will strongly recommend making it determined in run-time instead of build-time. 我强烈建议在运行时而不是在构建时确定它。
For example, when you are running your app, have config/
directory put in beginning of classpath, and put your log4j.xml
there. 例如,在运行应用程序时,将config/
目录放在类路径的开头,然后将log4j.xml
放在此处。 Of course, you may still keep a default log4.xml
in your application JAR, so that if you don't provide it in deployment, your app can still run. 当然,您仍然可以在应用程序JAR中保留默认的log4.xml
,这样,如果您在部署中未提供它,则您的应用程序仍可以运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.