build.gradle
排除掉對logback的依賴,添加對log4j的依賴java
dependencies { compile('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' } compile('org.slf4j:slf4j-api') compile('org.slf4j:slf4j-log4j12') compile('commons-logging:commons-logging') }
把log4j-dev.xml
文件放到src/main/resources
目錄下,而後在application.properties
文件裏設置web
logging.config=classpath:log4j-dev.xml
@SpringBootApplication public class Application implements EnvironmentAware { private Environment env; @Value('${logging.config}') private String log4jName; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void setEnvironment(Environment environment) { this.env = environment; String[] dProfiles = env.getDefaultProfiles(); String[] aProfiles = env.getActiveProfiles(); System.out.println("Spring DefaultProfiles:" + java.util.Arrays.asList(dProfiles)); System.out.println("Spring ActiveProfiles:" + java.util.Arrays.asList(aProfiles)); if (aProfiles.length == 0) { System.out.println("Please set 'spring.profiles.active'"); System.exit(-1); } //加載Log4J配置文件 DOMConfigurator.configure(org.springframework.util.ResourceUtils.getURL(log4jName)); Logger log = LoggerFactory.getLogger(this.getClass()); log.info("當前ActiveProfiles:"+env.getActiveProfiles().toString()); } }