-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from devatherock/logback-config
feat: Support for reading logback config from local or remote file
- Loading branch information
Showing
12 changed files
with
190 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,21 @@ | ||
package io.github.devatherock; | ||
|
||
import org.slf4j.LoggerFactory; | ||
import java.io.IOException; | ||
|
||
import io.github.devatherock.logback.LogbackConfigInitializer; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.joran.JoranConfigurator; | ||
import ch.qos.logback.classic.util.ContextInitializer; | ||
import ch.qos.logback.core.joran.spi.JoranException; | ||
import ch.qos.logback.core.util.StatusPrinter; | ||
import io.micronaut.runtime.Micronaut; | ||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import io.swagger.v3.oas.annotations.info.Info; | ||
import io.swagger.v3.oas.annotations.servers.Server; | ||
|
||
@OpenAPIDefinition(info = @Info(title = "artifactory-badge", version = "1.0.0"), servers = { | ||
@OpenAPIDefinition(info = @Info(title = "artifactory-badge", version = "1.1.0"), servers = { | ||
@Server(url = "http://localhost:8080", description = "The server where the application is hosted. Defaulted to localhost") | ||
}) | ||
public class Application { | ||
private static final String ENV_LOGBACK_CONFIG = "LOGBACK_CONFIGURATION_FILE"; | ||
|
||
public static void main(String[] args) { | ||
if (System.getProperty(ContextInitializer.CONFIG_FILE_PROPERTY) == null && | ||
System.getenv(ENV_LOGBACK_CONFIG) != null) { | ||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
|
||
try { | ||
JoranConfigurator configurator = new JoranConfigurator(); | ||
configurator.setContext(context); | ||
context.reset(); | ||
configurator.doConfigure( | ||
Application.class.getClassLoader().getResourceAsStream(System.getenv( | ||
ENV_LOGBACK_CONFIG))); | ||
} catch (JoranException je) { | ||
// StatusPrinter will handle this | ||
} | ||
StatusPrinter.printInCaseOfErrorsOrWarnings(context); | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
LogbackConfigInitializer.initializeConfig(); | ||
Micronaut.run(Application.class); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/io/github/devatherock/logback/LogbackConfigInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.github.devatherock.logback; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
|
||
import org.slf4j.LoggerFactory; | ||
|
||
import io.github.devatherock.Application; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.joran.JoranConfigurator; | ||
import ch.qos.logback.classic.util.ContextInitializer; | ||
import ch.qos.logback.core.joran.spi.JoranException; | ||
import ch.qos.logback.core.util.StatusPrinter; | ||
|
||
/** | ||
* Initializes logging config using an environment variable. Needed for native | ||
* images | ||
* | ||
* @author devaprasadh | ||
* | ||
*/ | ||
public class LogbackConfigInitializer { | ||
private static final String ENV_LOGBACK_CONFIG = "LOGBACK_CONFIGURATION_FILE"; | ||
|
||
/** | ||
* Initializes logging config using an environment variable | ||
* | ||
* @throws IOException | ||
*/ | ||
public static void initializeConfig() throws IOException { | ||
if (System.getProperty(ContextInitializer.CONFIG_FILE_PROPERTY) == null && | ||
System.getenv(ENV_LOGBACK_CONFIG) != null) { | ||
initializeConfig(System.getenv(ENV_LOGBACK_CONFIG)); | ||
} | ||
} | ||
|
||
/** | ||
* Initializes logging config using the specified config file | ||
* | ||
* @throws IOException | ||
*/ | ||
private static void initializeConfig(String configFile) throws IOException { | ||
InputStream config = readConfig(configFile); | ||
|
||
if (null != config) { | ||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
|
||
try { | ||
JoranConfigurator configurator = new JoranConfigurator(); | ||
configurator.setContext(context); | ||
context.reset(); | ||
configurator.doConfigure(config); | ||
} catch (JoranException exception) { | ||
// StatusPrinter will handle this | ||
} finally { | ||
config.close(); | ||
} | ||
StatusPrinter.printInCaseOfErrorsOrWarnings(context); | ||
} | ||
} | ||
|
||
/** | ||
* Read the config file contents as an {@link InputStream} | ||
* | ||
* @param configFile | ||
* @return an inputstream | ||
*/ | ||
private static InputStream readConfig(String configFile) { | ||
InputStream stream = Application.class.getClassLoader().getResourceAsStream(configFile); // From classpath | ||
|
||
if (null == stream) { | ||
try { | ||
stream = new FileInputStream(configFile); // From local file | ||
} catch (FileNotFoundException exception) { | ||
try { | ||
stream = new URL(configFile).openStream(); // From remote URL | ||
} catch (IOException ioException) { | ||
// no op | ||
} | ||
} | ||
} | ||
|
||
return stream; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/groovy/io/github/devatherock/logback/LogbackConfigInitializerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.github.devatherock.logback | ||
|
||
import java.nio.file.Paths | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
/** | ||
* Test class for {@link LogbackConfigInitializer} | ||
*/ | ||
class LogbackConfigInitializerSpec extends Specification { | ||
|
||
void 'test initialize config - no args'() { | ||
when: | ||
LogbackConfigInitializer.initializeConfig() | ||
|
||
then: | ||
noExceptionThrown() | ||
} | ||
|
||
@Unroll | ||
void 'test read config - #configFile'() { | ||
when: | ||
InputStream stream = LogbackConfigInitializer.readConfig(configFile) | ||
|
||
then: | ||
stream | ||
|
||
cleanup: | ||
stream?.close() | ||
|
||
where: | ||
configFile << [ | ||
'logback.xml', | ||
Paths.get(System.properties['user.dir'], 'src/main/resources/logback.xml').toString(), | ||
'https://raw.githubusercontent.com/devatherock/artifactory-badge/master/src/main/resources/logback-json.xml' | ||
] | ||
} | ||
|
||
@Unroll | ||
void 'test read config - non-existent/invalid path - #configFile'() { | ||
expect: | ||
!LogbackConfigInitializer.readConfig(configFile) | ||
|
||
where: | ||
configFile << [ | ||
'classpath:logback.xml', | ||
'dummy.xml', | ||
Paths.get(System.properties['user.dir'], 'src/main/resources/logbackx.xml').toString(), | ||
'https://raw.githubusercontent.com/devatherock/artifactory-badge/master/src/main/resour/logback-json.xml' | ||
] | ||
} | ||
|
||
@Unroll | ||
void 'test initialize config - #configFile'() { | ||
when: | ||
LogbackConfigInitializer.initializeConfig(configFile) | ||
|
||
then: | ||
noExceptionThrown() | ||
|
||
where: | ||
configFile << [ | ||
'logback.xml', | ||
Paths.get(System.properties['user.dir'], 'src/main/resources/logback.xml').toString(), | ||
'https://raw.githubusercontent.com/devatherock/artifactory-badge/master/src/main/resources/logback-json.xml', | ||
'classpath:logback.xml', | ||
'logback-invalid.xml' | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender" /> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |