How to change log4j configuration when packaging

I have a pretty simple Java application that is distributed to users as an executable JAR file. To track usage and help with diagnostics, I want to add logging capabilities to the application, but from one execution to the next I will never be sure where this application will run.

To do this, I configured the application to use the log4j loader application - no matter where the user starts this application, messages are sent to the socket and correctly logged, which is ideal.

What I am currently using is that I would prefer to use different log4j configurations when I run locally and when I deploy this application to production. When running locally (from my IDE, IntelliJ), I would prefer to use the console appender for several reasons:

  • It is trivial to see what happens.
  • Should I develop without an Internet connection, I don’t want to depend on the impossibility of connecting.
  • I don't want to clutter up production logs with what I do during development.

When I pack the application and distribute it, I would like it to use a socket application, not a console appender.

Is there any easy way to accomplish what I want? I use Maven to build my application, but I'm not very skilled with it.

+4
source share
3 answers

Here's a simpler approach to your problem ...

Place two log4j.properties files in the src/main/resources directory, for example:

 src/main/resources |- log4j.properties |- log4j_dev.properties 

log4j.properties contains the production configuration, and by default Log4J will automatically select this file because you are using the standard file name.

log4j_dev.properties contains the development configuration. For IntelliJ to choose this at design time, you overwrite the log configuration using the -Dlog4j.configuration=<configuration-file> VM option. I have attached a screenshot of how you can configure this using IntelliJ: -

enter image description here

+3
source

I highly recommend defining it at runtime rather than build time.

For example, when you launch your application, enter config/ directory at the beginning of the class path and put log4j.xml there. Of course, you can still store the default log4.xml in the application JAR, so if you do not provide it for deployment, your application can still work.

+3
source

This turned out to be easier than I expected, and Vikdor's comment led me to the correct answer. I started digging through several profiles (one for dev and one for prod), but later realized that I really didn't need to have two. When working on the local computer, I really did not execute the Maven build process - I just ran the Java classes. The only time I went through the Maven build process was that I wanted to pack the application for deployment. So I just added a section to my POM file that uses maven-antrun-plugin. My last code looked like this:

File: 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

 # 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

 ... <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> ... 

It seemed like a trick. When I run locally, I load log4j.properties, which gives me a console appender. When I pack for the production distribution, Maven will replace the default log4j.properties file with an alternative one that uses the socket application.

Thanks!

+3
source

Source: https://habr.com/ru/post/1445907/


All Articles