I have a fairly simple Java application that is being distributed out to users as an executable JAR file. 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.
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. When running locally (from my IDE, IntelliJ), I would prefer to use a console appender for a couple reasons:
- It’s trivial to see what is happening.
- Should I be developing without an Internet connection, I don’t want to hang up on not being able to connect.
- I don’t want to clutter the production logs with what I am doing during development.
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.
Here’s a simpler approach to your problem…
Put two log4j.properties files under
src/main/resourcesdirectory, like this:-log4j.propertiescontains the production configuration, and by default, Log4J will automatically pick this file up because you are using the standard file name.log4j_dev.propertiescontains the development configuration. To ensure IntelliJ picks this up during development, you will overwrite the log configuration using-Dlog4j.configuration=<configuration-file>VM option. I attached a screenshot on how you might configure this using IntelliJ:-