Skip to content

Commit

Permalink
feat: Support for reading logback config from local or remote file
Browse files Browse the repository at this point in the history
  • Loading branch information
devatherock committed May 12, 2022
1 parent 440a6b9 commit de24bf3
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pr_filter: &pr_filter
executors:
jdk-executor:
docker:
- image: devatherock/graalvm:0.2.0
- image: devatherock/graalvm:ol8-java11-22.1.0-2
auth:
username: $DOCKER_USERNAME
password: $DOCKER_PASSWORD
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## [Unreleased]
### Added
- Support for reading logback config from local or remote file

### Changed
- Used integration test config from `gradle-includes`

Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM devatherock/graalvm:ol7-java11-20.3.4-1 as graalvm
FROM ghcr.io/graalvm/native-image:ol8-java11-22.1.0 as graalvm

COPY . /home/app/micronaut-graal-app
WORKDIR /home/app/micronaut-graal-app

RUN native-image --no-server -cp build/libs/*-all.jar
RUN native-image -cp build/libs/*-all.jar



Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ docker run --rm \
-p 8080:8080 \
-e ARTIFACTORY_URL=https://some/url \
-e ARTIFACTORY_API_KEY=xyz \
devatherock/artifactory-badge:0.5.0
devatherock/artifactory-badge:1.0.0
```

### Configurable properties
Expand Down
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ ext.jacoco = [
exclusions: [
'io/github/devatherock/Application.class',
'io/github/devatherock/test/ArtifactoryController.class'
],
coverageThresholds: [
'io.github.devatherock.logback.LogbackConfigInitializer': [
'BRANCH': 0.75,
'COMPLEXITY': 0.62,
'INSTRUCTION': 0.89,
'LINE': 0.88
]
]
]
apply from: 'https://raw.githubusercontent.com/devatherock/gradle-includes/master/integration.gradle'
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ services:
- ARTIFACTORY_URL=http://localhost:8081
- ARTIFACTORY_API_KEY=dummyKey
- ARTIFACTORY_BADGE_SHIELDS_IO_URL=http://localhost:8081
- LOGBACK_CONFIGURATION_FILE=https://raw.githubusercontent.com/devatherock/artifactory-badge/master/src/main/resources/logback-json.xml
ports:
- "8080:8080"
32 changes: 6 additions & 26 deletions src/main/java/io/github/devatherock/Application.java
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);
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ Args = -H:ResourceConfigurationFiles=build/generated/resources/graalvm/resource-
-H:Name=micronautgraalapp \
-H:ReflectionConfigurationFiles=build/graal/reflect-config.json,build/resources/main/graal/reflect-config.json \
--no-fallback \
--allow-incomplete-classpath \
--report-unsupported-elements-at-runtime \
--initialize-at-build-time=sun.instrument.InstrumentationImpl \
--initialize-at-run-time=io.micronaut.aop.internal.intercepted.KotlinInterceptedMethod \
-H:EnableURLProtocols=http,https \
--enable-all-security-services \
-H:Class=io.github.devatherock.Application
1 change: 1 addition & 0 deletions src/main/resources/graal/access-filter.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rules": [
{"excludeClasses": "io.github.devatherock.logback.LogbackConfigInitializerSpec"},
{"excludeClasses": "io.github.devatherock.artifactory.config.AppConfigSpec"},
{"excludeClasses": "io.github.devatherock.artifactory.config.ArtifactoryPropertiesSpec"},
{"excludeClasses": "io.github.devatherock.artifactory.config.ShieldsIOPropertiesSpec"},
Expand Down
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'
]
}
}
7 changes: 7 additions & 0 deletions src/test/resources/logback-invalid.xml
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>

0 comments on commit de24bf3

Please sign in to comment.