Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load conf/log4j.properties automatically if exists #203

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion framework/src/play/PlayLoggingSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ public void init() {
String log4jPath = Play.configuration.getProperty("application.log.path", "/log4j.xml");
URL log4jConf = PlayLoggingSetup.class.getResource(log4jPath);
if (log4jConf == null) {
log4jPath = Play.configuration.getProperty("application.log.path", "/log4j.properties");
// try log4j.properties from the "conf/" directory/classpath
log4jPath = "/conf/log4j.properties";
log4jConf = PlayLoggingSetup.class.getResource(log4jPath);
}
if (log4jConf == null) {
// falling back to "/log4j.properties"
log4jPath = "/log4j.properties";
log4jConf = PlayLoggingSetup.class.getResource(log4jPath);
}
if (log4jConf == null) {
Expand Down
9 changes: 9 additions & 0 deletions framework/test/conf/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
log4j.rootLogger=DEBUG, PropertiesConsoleAppender

log4j.appender.PropertiesConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.PropertiesConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.PropertiesConsoleAppender.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c: %m%n



log4j.logger.logtest.confdir=ERROR
19 changes: 19 additions & 0 deletions framework/test/play/PlayLoggingSetupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import play.libs.Files;

import java.io.File;
import java.net.URISyntaxException;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -57,4 +59,21 @@ public void init_with_xml() {
Logger log4jLogger = Logger.getLogger("logtest.xml");
assertThat(log4jLogger.getLevel()).isEqualTo(Level.ERROR);
}

@Test
public void init_with_conf_dir() {
loggingSetup.init();
Logger log4jLogger = Logger.getLogger("logtest.confdir");
assertThat(log4jLogger.getLevel()).isEqualTo(Level.ERROR);
assertThat(Logger.getRootLogger().getLevel()).isEqualTo(Level.DEBUG);
}

@Test
public void init_with_default_config() {
// delete the conf/log4j.properties from test classpath and fallback to the framework log4j.properties
File confProp = new File(getClass().getResource("/conf/log4j.properties").getFile());
assertThat(confProp.delete()).isTrue();
loggingSetup.init();
assertThat(Logger.getRootLogger().getLevel()).isEqualTo(Level.INFO);
}
}