Skip to content

Commit

Permalink
Log to stdout and fix fat / shadow jar
Browse files Browse the repository at this point in the history
Logging to standard out means we no longer need the JNA jar, which
doesn't work when shadowed.

Making a shadow jar vs. a fat jar means the dependencies in the jar
won't collide with the dependencies in projects that use the shadow jar
for testing.
  • Loading branch information
MrCreosote committed Mar 1, 2024
1 parent 9a5981b commit ee633dc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
22 changes: 9 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
id 'war'
id 'jacoco'
id 'org.ajoberstar.grgit' version '4.1.1'
id 'com.github.johnrengelman.shadow' version '8.1.1'
}

repositories {
Expand Down Expand Up @@ -94,28 +95,24 @@ task generateTemplateFileList {
}
}


task fatTestJar(type: Jar) {
shadowJar {
// Be careful when updating jars - you may want to set the duplicates strategy to WARN
// to see if any of the jars are shadowing the others when building the fat jar, which
// has been the case in the past
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
dependsOn generateTemplateFileList
archiveAppendix = 'test-fat'

// include source files
archiveAppendix = 'test-shadow'
from sourceSets.test.output
configurations = [project.configurations.testRuntimeClasspath]

// include all jars
from {
configurations.testimpl.collect { it.isDirectory() ? it : zipTree(it) }
}

enableRelocation true
relocationPrefix 'us.kbase.auth2.shadow'

mergeServiceFiles()
// Include text files from the "templates" directory
from('templates') { into JAR_TEMPLATE_DIR }
from("$buildDir/" + TEMPLATE_LIST_FILE_NAME) { into JAR_TEMPLATE_DIR }

with jar
}

task generateManageAuthScript {
Expand Down Expand Up @@ -217,7 +214,6 @@ dependencies {
'syslog4j-0.9.46'
)
// needed for syslog4j
implementation 'net.java.dev.jna:jna:3.4.0'
implementation 'joda-time:joda-time:2.3'

// ### Test ###
Expand Down
53 changes: 43 additions & 10 deletions src/main/java/us/kbase/auth2/kbase/KBaseAuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
Expand All @@ -15,6 +16,7 @@
import java.util.regex.Pattern;

import org.ini4j.Ini;
import org.productivity.java.syslog4j.SyslogIF;
import org.slf4j.LoggerFactory;

import us.kbase.auth2.lib.identity.IdentityProviderConfig;
Expand All @@ -25,6 +27,7 @@
import us.kbase.auth2.service.exceptions.AuthConfigurationException;
import us.kbase.common.service.JsonServerSyslog;
import us.kbase.common.service.JsonServerSyslog.RpcInfo;
import us.kbase.common.service.JsonServerSyslog.SyslogOutput;

public class KBaseAuthConfig implements AuthStartupConfig {

Expand Down Expand Up @@ -85,16 +88,8 @@ public KBaseAuthConfig() throws AuthConfigurationException {
public KBaseAuthConfig(final Path filepath, final boolean nullLogger)
throws AuthConfigurationException {
final Map<String, String> cfg = getConfig(filepath);
final String ln = getString(KEY_LOG_NAME, cfg);
if (nullLogger) {
logger = new NullLogger();
} else {
logger = new JsonServerSysLogAutoLogger(new JsonServerSyslog(
ln == null ? DEFAULT_LOG_NAME : ln,
//TODO KBASECOMMON allow null for the fake config prop arg
"thisisafakekeythatshouldntexistihope",
JsonServerSyslog.LOG_LEVEL_INFO, true));
}
final String logname = getString(KEY_LOG_NAME, cfg);
logger = nullLogger ? new NullLogger() : buildLogger(logname);
try {
isTestModeEnabled = TRUE.equals(getString(KEY_TEST_MODE_ENABLED, cfg));
templateDir = Paths.get(getString(KEY_TEMPLATE_DIR, cfg, true));
Expand Down Expand Up @@ -125,6 +120,44 @@ public KBaseAuthConfig(final Path filepath, final boolean nullLogger)
}
}

private SLF4JAutoLogger buildLogger(final String logname) {
// Warning - this code is tested manually. Ensure
// logs are making it to stdout after changing
// TODO LOGGING just remove JsonServerSyslog altogether
// and get rid of Syslog4j. Not sure how much work this'd be
JsonServerSyslog.setStaticUseSyslog(false);
final JsonServerSyslog jssl = new JsonServerSyslog(
logname == null ? DEFAULT_LOG_NAME : logname,
null,
JsonServerSyslog.LOG_LEVEL_INFO,
true
);
jssl.changeOutput(new SyslogOutput() {

@Override
public void logToSystem(
final SyslogIF log,
final int level,
final String message) {
System.out.println(String.format(
"[Auth2] Lvl: %s Message: %s", level, message));
}

@Override
public PrintWriter logToFile(
final File f,
final PrintWriter pw,
final int level,
final String message) {
System.out.println(

Check warning on line 152 in src/main/java/us/kbase/auth2/kbase/KBaseAuthConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/us/kbase/auth2/kbase/KBaseAuthConfig.java#L152

Added line #L152 was not covered by tests
"log to file called - this is not supported and not expected");
return null;

Check warning on line 154 in src/main/java/us/kbase/auth2/kbase/KBaseAuthConfig.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/us/kbase/auth2/kbase/KBaseAuthConfig.java#L154

Added line #L154 was not covered by tests
}

});
return new JsonServerSysLogAutoLogger(jssl);
}

private static final String INVALID_CHARS_REGEX = "[^A-Z-]+";
private static final Pattern INVALID_CHARS = Pattern.compile(INVALID_CHARS_REGEX);

Expand Down
8 changes: 5 additions & 3 deletions src/test/java/us/kbase/test/auth2/TestConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ private static class TestLogger implements SLF4JAutoLogger {
private final JsonServerSyslog logger;

public TestLogger() {
JsonServerSyslog.setStaticUseSyslog(false);
logger = new JsonServerSyslog(
"AuthTestLogger",
//TODO CODE update kbase-common and pass null instead
"thisisafakekeythatshouldntexistihope",
JsonServerSyslog.LOG_LEVEL_INFO, true);
null,
JsonServerSyslog.LOG_LEVEL_INFO,
true
);
logger.changeOutput(new SyslogOutput() {

@Override
Expand Down

0 comments on commit ee633dc

Please sign in to comment.