Skip to content

Commit 0941938

Browse files
authored
Merge pull request #7 from git-commit-id/new-log-interface
#6: Replace custom slf4j logger bridge
2 parents cf68e4f + 4da517d commit 0941938

29 files changed

+144
-1167
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@
231231
<version>1.1.1</version>
232232
<scope>test</scope>
233233
</dependency>
234+
<!-- slf4j is a dependency of jgit, this suppresses slf4j's warning during testing when we mock jgit stuff -->
234235
<dependency>
235236
<groupId>org.slf4j</groupId>
236237
<artifactId>slf4j-simple</artifactId>

src/main/java/pl/project13/core/GitDataProvider.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pl.project13.core.git.GitDescribeConfig;
2121
import pl.project13.core.cibuild.BuildServerDataProvider;
2222
import pl.project13.core.cibuild.UnknownBuildServerData;
23-
import pl.project13.core.log.LoggerBridge;
23+
import pl.project13.core.log.LogInterface;
2424
import pl.project13.core.util.PropertyManager;
2525

2626
import javax.annotation.Nonnull;
@@ -45,7 +45,7 @@ public abstract class GitDataProvider implements GitProvider {
4545
* Logging provider which will be used to log events.
4646
*/
4747
@Nonnull
48-
protected final LoggerBridge log;
48+
protected final LogInterface log;
4949

5050
/**
5151
* The {@code prefix} used for all generated properties.
@@ -120,7 +120,7 @@ public abstract class GitDataProvider implements GitProvider {
120120
* Constructor to encapsulates all references required to dertermine all git-data.
121121
* @param log logging provider which will be used to log events
122122
*/
123-
public GitDataProvider(@Nonnull LoggerBridge log) {
123+
public GitDataProvider(@Nonnull LogInterface log) {
124124
this.log = log;
125125
}
126126

@@ -364,7 +364,7 @@ void validateAbbrevLength(int abbrevLength) throws GitCommitIdExecutionException
364364
* @throws GitCommitIdExecutionException the branch name could not be determined
365365
*/
366366
protected String determineBranchName(@Nonnull Map<String, String> env) throws GitCommitIdExecutionException {
367-
BuildServerDataProvider buildServerDataProvider = BuildServerDataProvider.getBuildServerProvider(env,log);
367+
BuildServerDataProvider buildServerDataProvider = BuildServerDataProvider.getBuildServerProvider(env, log);
368368
if (useBranchNameFromBuildEnvironment && !(buildServerDataProvider instanceof UnknownBuildServerData)) {
369369
String branchName = buildServerDataProvider.getBuildBranch();
370370
if (branchName == null || branchName.isEmpty()) {
@@ -390,10 +390,10 @@ protected void maybePut(@Nonnull Properties properties, String key, SupplierEx<S
390390
String keyWithPrefix = prefixDot + key;
391391
if (properties.stringPropertyNames().contains(keyWithPrefix)) {
392392
String propertyValue = properties.getProperty(keyWithPrefix);
393-
log.info("Using cached {} with value {}", keyWithPrefix, propertyValue);
393+
log.info(String.format("Using cached %s with value %s", keyWithPrefix, propertyValue));
394394
} else if (PropertiesFilterer.isIncluded(keyWithPrefix, includeOnlyProperties, excludeProperties)) {
395395
String propertyValue = value.get();
396-
log.info("Collected {} with value {}", keyWithPrefix, propertyValue);
396+
log.info(String.format("Collected %s with value %s", keyWithPrefix, propertyValue));
397397
PropertyManager.putWithoutPrefix(properties, keyWithPrefix, propertyValue);
398398
}
399399
}

src/main/java/pl/project13/core/JGitProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import pl.project13.core.jgit.DescribeResult;
3030
import pl.project13.core.jgit.JGitCommon;
3131
import pl.project13.core.jgit.DescribeCommand;
32-
import pl.project13.core.log.LoggerBridge;
32+
import pl.project13.core.log.LogInterface;
3333

3434
import java.io.File;
3535
import java.io.IOException;
@@ -51,11 +51,11 @@ public class JGitProvider extends GitDataProvider {
5151
private JGitCommon jGitCommon;
5252

5353
@Nonnull
54-
public static JGitProvider on(@Nonnull File dotGitDirectory, @Nonnull LoggerBridge log) {
54+
public static JGitProvider on(@Nonnull File dotGitDirectory, @Nonnull LogInterface log) {
5555
return new JGitProvider(dotGitDirectory, log);
5656
}
5757

58-
JGitProvider(@Nonnull File dotGitDirectory, @Nonnull LoggerBridge log) {
58+
JGitProvider(@Nonnull File dotGitDirectory, @Nonnull LogInterface log) {
5959
super(log);
6060
this.dotGitDirectory = dotGitDirectory;
6161
this.jGitCommon = new JGitCommon(log);
@@ -243,7 +243,7 @@ public String getTags() throws GitCommitIdExecutionException {
243243
Collection<String> tags = jGitCommon.getTags(repo, headId);
244244
return String.join(",", tags);
245245
} catch (GitAPIException e) {
246-
log.error("Unable to extract tags from commit: {} ({})", evalCommit.getName(), e.getClass().getName());
246+
log.error(String.format("Unable to extract tags from commit: %s", evalCommit.getName()), e);
247247
return "";
248248
}
249249
}

src/main/java/pl/project13/core/NativeGitProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import static java.lang.String.format;
2121

2222
import pl.project13.core.git.GitDescribeConfig;
23-
import pl.project13.core.log.LoggerBridge;
23+
import pl.project13.core.log.LogInterface;
2424

2525
import javax.annotation.Nonnull;
2626

@@ -45,11 +45,11 @@ public class NativeGitProvider extends GitDataProvider {
4545
final File canonical;
4646

4747
@Nonnull
48-
public static NativeGitProvider on(@Nonnull File dotGitDirectory, long nativeGitTimeoutInMs, @Nonnull LoggerBridge log) {
48+
public static NativeGitProvider on(@Nonnull File dotGitDirectory, long nativeGitTimeoutInMs, @Nonnull LogInterface log) {
4949
return new NativeGitProvider(dotGitDirectory, nativeGitTimeoutInMs, log);
5050
}
5151

52-
NativeGitProvider(@Nonnull File dotGitDirectory, long nativeGitTimeoutInMs, @Nonnull LoggerBridge log) {
52+
NativeGitProvider(@Nonnull File dotGitDirectory, long nativeGitTimeoutInMs, @Nonnull LogInterface log) {
5353
super(log);
5454
this.dotGitDirectory = dotGitDirectory;
5555
this.nativeGitTimeoutInMs = nativeGitTimeoutInMs;

src/main/java/pl/project13/core/PropertiesFileGenerator.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package pl.project13.core;
1919

2020
import nu.studer.java.util.OrderedProperties;
21-
import pl.project13.core.log.LoggerBridge;
21+
import pl.project13.core.log.LogInterface;
2222
import pl.project13.core.util.BuildFileChangeListener;
2323
import pl.project13.core.util.JsonManager;
2424
import pl.project13.core.util.PropertyManager;
@@ -32,13 +32,13 @@
3232

3333
public class PropertiesFileGenerator {
3434

35-
private LoggerBridge log;
35+
private LogInterface log;
3636
private BuildFileChangeListener buildFileChangeListener;
3737
private String format;
3838
private String prefixDot;
3939
private String projectName;
4040

41-
public PropertiesFileGenerator(LoggerBridge log, BuildFileChangeListener buildFileChangeListener, String format, String prefixDot, String projectName) {
41+
public PropertiesFileGenerator(LogInterface log, BuildFileChangeListener buildFileChangeListener, String format, String prefixDot, String projectName) {
4242
this.log = log;
4343
this.buildFileChangeListener = buildFileChangeListener;
4444
this.format = format;
@@ -58,10 +58,10 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
5858

5959
try {
6060
if (isJsonFormat) {
61-
log.info("Reading existing json file [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
61+
log.info(String.format("Reading existing json file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
6262
persistedProperties = JsonManager.readJsonProperties(gitPropsFile, sourceCharset);
6363
} else {
64-
log.info("Reading existing properties file [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
64+
log.info(String.format("Reading existing properties file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
6565
persistedProperties = PropertyManager.readProperties(gitPropsFile);
6666
}
6767

@@ -75,7 +75,7 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
7575
shouldGenerate = !propertiesCopy.equals(persistedProperties);
7676
} catch (CannotReadFileException ex) {
7777
// Read has failed, regenerate file
78-
log.info("Cannot read properties file [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
78+
log.info(String.format("Cannot read properties file [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
7979
shouldGenerate = true;
8080
}
8181
}
@@ -86,10 +86,10 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
8686
OrderedProperties sortedLocalProperties = PropertiesFileGenerator.createOrderedProperties();
8787
localProperties.forEach((key, value) -> sortedLocalProperties.setProperty((String) key, (String) value));
8888
if (isJsonFormat) {
89-
log.info("Writing json file to [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
89+
log.info(String.format("Writing json file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
9090
JsonManager.dumpJson(outputStream, sortedLocalProperties, sourceCharset);
9191
} else {
92-
log.info("Writing properties file to [{}] (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
92+
log.info(String.format("Writing properties file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
9393
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
9494
PropertyManager.dumpProperties(outputStream, sortedLocalProperties);
9595
}
@@ -102,7 +102,7 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
102102
}
103103

104104
} else {
105-
log.info("Properties file [{}] is up-to-date (for module {})...", gitPropsFile.getAbsolutePath(), projectName);
105+
log.info(String.format("Properties file [%s] is up-to-date (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
106106
}
107107
} catch (IOException e) {
108108
throw new GitCommitIdExecutionException(e);

src/main/java/pl/project13/core/PropertiesFilterer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
import java.util.List;
2121
import java.util.Properties;
2222

23-
import pl.project13.core.log.LoggerBridge;
23+
import pl.project13.core.log.LogInterface;
2424

2525
import javax.annotation.Nullable;
2626

2727
public class PropertiesFilterer {
2828

29-
private LoggerBridge log;
29+
private LogInterface log;
3030

31-
public PropertiesFilterer(LoggerBridge log) {
31+
public PropertiesFilterer(LogInterface log) {
3232
this.log = log;
3333
}
3434

@@ -43,7 +43,7 @@ public void filterNot(Properties properties, @Nullable List<String> exclusions,
4343
.forEach(key -> {
4444
if (exclusions.stream()
4545
.anyMatch(key::matches)) {
46-
log.debug("shouldExclude.apply({})", key);
46+
log.debug(String.format("shouldExclude.apply(%s)", key));
4747
properties.remove(key);
4848
}
4949
});
@@ -60,7 +60,7 @@ public void filter(Properties properties, @Nullable List<String> inclusions, Str
6060
.forEach(key -> {
6161
if (inclusions.stream()
6262
.noneMatch(key::matches)) {
63-
log.debug("!shouldInclude.apply({})", key);
63+
log.debug(String.format("!shouldInclude.apply(%s)", key));
6464
properties.remove(key);
6565
}
6666
});

src/main/java/pl/project13/core/cibuild/AwsCodeBuildBuildServerData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
package pl.project13.core.cibuild;
1919

2020
import pl.project13.core.GitCommitPropertyConstant;
21-
import pl.project13.core.log.LoggerBridge;
21+
import pl.project13.core.log.LogInterface;
2222

2323
import javax.annotation.Nonnull;
2424
import java.util.Map;
2525
import java.util.Properties;
2626

2727
public class AwsCodeBuildBuildServerData extends BuildServerDataProvider {
2828

29-
AwsCodeBuildBuildServerData(LoggerBridge log, @Nonnull Map<String, String> env) {
29+
AwsCodeBuildBuildServerData(LogInterface log, @Nonnull Map<String, String> env) {
3030
super(log,env);
3131
}
3232

@@ -51,7 +51,7 @@ void loadBuildNumber(@Nonnull Properties properties) {
5151
@Override
5252
public String getBuildBranch() {
5353
String environmentBasedBranch = env.getOrDefault("CODEBUILD_SOURCE_VERSION", "");
54-
log.info("Using environment variable based branch name. CODEBUILD_SOURCE_VERSION = {}", environmentBasedBranch);
54+
log.info(String.format("Using environment variable based branch name. CODEBUILD_SOURCE_VERSION = %s", environmentBasedBranch));
5555
return environmentBasedBranch;
5656
}
5757
}

src/main/java/pl/project13/core/cibuild/AzureDevOpsBuildServerData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package pl.project13.core.cibuild;
1919

20-
import pl.project13.core.log.LoggerBridge;
20+
import pl.project13.core.log.LogInterface;
2121
import pl.project13.core.GitCommitPropertyConstant;
2222

2323
import javax.annotation.Nonnull;
@@ -26,7 +26,7 @@
2626

2727
public class AzureDevOpsBuildServerData extends BuildServerDataProvider {
2828

29-
AzureDevOpsBuildServerData(@Nonnull LoggerBridge log, @Nonnull Map<String, String> env) {
29+
AzureDevOpsBuildServerData(@Nonnull LogInterface log, @Nonnull Map<String, String> env) {
3030
super(log, env);
3131
}
3232

@@ -49,7 +49,7 @@ void loadBuildNumber(@Nonnull Properties properties) {
4949
@Override
5050
public String getBuildBranch() {
5151
String environmentBasedBuildSourceBranchName = env.get("BUILD_SOURCEBRANCHNAME");
52-
log.info("Using environment variable based branch name. BUILD_SOURCEBRANCHNAME = {}", environmentBasedBuildSourceBranchName);
52+
log.info(String.format("Using environment variable based branch name. BUILD_SOURCEBRANCHNAME = %s", environmentBasedBuildSourceBranchName));
5353
return environmentBasedBuildSourceBranchName;
5454
}
5555
}

src/main/java/pl/project13/core/cibuild/BambooBuildServerData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
package pl.project13.core.cibuild;
1919

2020
import pl.project13.core.GitCommitPropertyConstant;
21-
import pl.project13.core.log.LoggerBridge;
21+
import pl.project13.core.log.LogInterface;
2222

2323
import javax.annotation.Nonnull;
2424
import java.util.*;
2525

2626
public class BambooBuildServerData extends BuildServerDataProvider {
2727

28-
BambooBuildServerData(LoggerBridge log, @Nonnull Map<String, String> env) {
28+
BambooBuildServerData(LogInterface log, @Nonnull Map<String, String> env) {
2929
super(log, env);
3030
}
3131

@@ -64,7 +64,7 @@ public String getBuildBranch() {
6464
break;
6565
}
6666
}
67-
log.info("Using environment variable based branch name. {} = {}", environmentBasedKey, environmentBasedBranch);
67+
log.info(String.format("Using environment variable based branch name. %s = %s", environmentBasedKey, environmentBasedBranch));
6868
return environmentBasedBranch;
6969
}
7070
}

src/main/java/pl/project13/core/cibuild/BitbucketBuildServerData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package pl.project13.core.cibuild;
1919

2020
import pl.project13.core.GitCommitPropertyConstant;
21-
import pl.project13.core.log.LoggerBridge;
21+
import pl.project13.core.log.LogInterface;
2222

2323
import java.util.Map;
2424
import java.util.Optional;
@@ -27,7 +27,7 @@
2727

2828
public class BitbucketBuildServerData extends BuildServerDataProvider {
2929

30-
BitbucketBuildServerData(LoggerBridge log, @Nonnull Map<String, String> env) {
30+
BitbucketBuildServerData(LogInterface log, @Nonnull Map<String, String> env) {
3131
super(log, env);
3232
}
3333

@@ -56,7 +56,7 @@ public String getBuildBranch() {
5656
if (environmentBasedBranch != null) {
5757
environmentBasedKey = envKey;
5858
}
59-
log.info("Using environment variable based branch name. {} = {}", environmentBasedKey, environmentBasedBranch);
59+
log.info(String.format("Using environment variable based branch name. %s = %s", environmentBasedKey, environmentBasedBranch));
6060
return environmentBasedBranch;
6161
}
6262
}

src/main/java/pl/project13/core/cibuild/BuildServerDataProvider.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import pl.project13.core.GitCommitPropertyConstant;
2121
import pl.project13.core.PropertiesFilterer;
22-
import pl.project13.core.log.LoggerBridge;
22+
import pl.project13.core.log.LogInterface;
2323
import pl.project13.core.util.PropertyManager;
2424

2525
import javax.annotation.Nonnull;
@@ -31,7 +31,7 @@
3131
import java.util.function.Supplier;
3232

3333
public abstract class BuildServerDataProvider {
34-
final LoggerBridge log;
34+
final LogInterface log;
3535
final Map<String, String> env;
3636
private String dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
3737
private String dateFormatTimeZone = null;
@@ -40,7 +40,7 @@ public abstract class BuildServerDataProvider {
4040
private List<String> includeOnlyProperties = null;
4141
private Map<String, Supplier<String>> additionalProperties = new HashMap<>();
4242

43-
BuildServerDataProvider(@Nonnull LoggerBridge log, @Nonnull Map<String, String> env) {
43+
BuildServerDataProvider(@Nonnull LogInterface log, @Nonnull Map<String, String> env) {
4444
this.log = log;
4545
this.env = env;
4646
}
@@ -82,7 +82,7 @@ public BuildServerDataProvider setAdditionalProperties(Map<String, Supplier<Stri
8282
* @param log logging provider which will be used to log events
8383
* @return the corresponding {@link BuildServerDataProvider} for your environment or {@link UnknownBuildServerData}
8484
*/
85-
public static BuildServerDataProvider getBuildServerProvider(@Nonnull Map<String, String> env, @Nonnull LoggerBridge log) {
85+
public static BuildServerDataProvider getBuildServerProvider(@Nonnull Map<String, String> env, @Nonnull LogInterface log) {
8686
if (BambooBuildServerData.isActiveServer(env)) {
8787
return new BambooBuildServerData(log, env);
8888
}
@@ -161,9 +161,9 @@ private void loadBuildHostData(@Nonnull Properties properties) {
161161
// this call might be costly - try to avoid it too (similar concept as in GitDataProvider)
162162
buildHost = InetAddress.getLocalHost().getHostName();
163163
} catch (UnknownHostException e) {
164-
log.info("Unable to get build host, skipping property {}. Error message: {}",
164+
log.info(String.format("Unable to get build host, skipping property %s. Error message: %s",
165165
GitCommitPropertyConstant.BUILD_HOST,
166-
e.getMessage());
166+
e.getMessage()));
167167
}
168168
return buildHost;
169169
};
@@ -174,10 +174,10 @@ protected void maybePut(@Nonnull Properties properties, @Nonnull String key, Sup
174174
String keyWithPrefix = prefixDot + key;
175175
if (properties.stringPropertyNames().contains(keyWithPrefix)) {
176176
String propertyValue = properties.getProperty(keyWithPrefix);
177-
log.info("Using cached {} with value {}", keyWithPrefix, propertyValue);
177+
log.info(String.format("Using cached %s with value %s", keyWithPrefix, propertyValue));
178178
} else if (PropertiesFilterer.isIncluded(keyWithPrefix, includeOnlyProperties, excludeProperties)) {
179179
String propertyValue = supplier.get();
180-
log.info("Collected {} with value {}", keyWithPrefix, propertyValue);
180+
log.info(String.format("Collected %s with value %s", keyWithPrefix, propertyValue));
181181
PropertyManager.putWithoutPrefix(properties, keyWithPrefix, propertyValue);
182182
}
183183
}

0 commit comments

Comments
 (0)