I would like to have one log4j.xml config and be able to log to console while developing my application. Once deployed to an environment I want to only log to a file appender and not the console. How can I achieve this?
This is my current config:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<appender name="LogFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${log-base-dir}/${adapter-name}.log" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="99" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="info" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="LogFileAppender" />
</root>
</log4j:configuration>
One of the solutions could be separation of log4j configuration files for development and production environments e.g.:
log4j-development.xml
- for development environmentlog4j-production.xml
- for production environmentThen your application startup command could have parrameter specifying log4j configuration file e.g. java -Dlog4jconfig=log4j-development.xml -jar Application.jar
You can configure log4j by getting value of log4jconfig
property in your code e.g. System.getProperty("log4jconfig")
.
Pros of that solution are as follows:
ConsoleAppender
and LogFileAppender
in development and only LogFileAppender
in production)error
in production and debug
in development)That pattern is used in many application servers where you have multiple environments (Development, UAT, Staging, Production etc.)
Example of log4j-development.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="debug" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4j:configuration>
Example of log4j-production.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="LogFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${log-base-dir}/${adapter-name}.log" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="99" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss} %-5p%c{1} - %m%n" />
</layout>
</appender>
<root>
<level value="error" />
<appender-ref ref="LogFileAppender" />
</root>
</log4j:configuration>