From 894482df1a2ca02e51cfdab86489ce563bfc02df Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 7 May 2014 07:19:19 -0700 Subject: [PATCH] Initialize logback is it is present in CLI This (empirically) fixes gh-726 using a new utility (LogbackInitializer) in the loader tools. If anyone has enough Gradle fu to understand where to put it (after the classpath is established but before compilation) we could do the same thing in the Gradle plugin (and that would fix gh-724). --- .../springframework/boot/cli/SpringCli.java | 2 + .../spring-boot-loader-tools/pom.xml | 5 +++ .../boot/loader/tools/LogbackInitializer.java | 44 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LogbackInitializer.java diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java index 13ee7da70b..c86e3401f6 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java @@ -24,6 +24,7 @@ import org.springframework.boot.cli.command.core.HelpCommand; import org.springframework.boot.cli.command.core.HintCommand; import org.springframework.boot.cli.command.core.VersionCommand; import org.springframework.boot.cli.command.shell.ShellCommand; +import org.springframework.boot.loader.tools.LogbackInitializer; /** * Spring Command Line Interface. This is the main entry-point for the Spring command line @@ -37,6 +38,7 @@ public class SpringCli { public static void main(String... args) { System.setProperty("java.awt.headless", Boolean.toString(true)); + LogbackInitializer.initialize(); CommandRunner runner = new CommandRunner("spring"); runner.addCommand(new HelpCommand(runner)); diff --git a/spring-boot-tools/spring-boot-loader-tools/pom.xml b/spring-boot-tools/spring-boot-loader-tools/pom.xml index d1fb124c68..3a774fe039 100644 --- a/spring-boot-tools/spring-boot-loader-tools/pom.xml +++ b/spring-boot-tools/spring-boot-loader-tools/pom.xml @@ -29,6 +29,11 @@ spring-boot-loader provided + + ch.qos.logback + logback-classic + provided + org.zeroturnaround diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LogbackInitializer.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LogbackInitializer.java new file mode 100644 index 0000000000..b821127085 --- /dev/null +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/LogbackInitializer.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.loader.tools; + +import org.slf4j.ILoggerFactory; +import org.slf4j.Logger; +import org.slf4j.impl.StaticLoggerBinder; +import org.springframework.util.ClassUtils; + +import ch.qos.logback.classic.Level; + +/** + * @author Dave Syer + */ +public class LogbackInitializer { + + public static void initialize() { + if (ClassUtils.isPresent("org.slf4j.impl.StaticLoggerBinder", null)) { + new Initializer().setRootLogLevel(); + } + } + + private static class Initializer { + public void setRootLogLevel() { + ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory(); + Logger logger = factory.getLogger(Logger.ROOT_LOGGER_NAME); + ((ch.qos.logback.classic.Logger) logger).setLevel(Level.INFO); + } + } +}