Skip to content

Commit

Permalink
Migrate to SLF4J for logging
Browse files Browse the repository at this point in the history
As noted in diffplug#1116, it'd be ideal if we moved to SLF4J as our logging
interface.

We can do this pretty easily, making sure we remove our `package-list`
reference, as well as making sure the SLF4J API is only used for
compilation.

We also need to map the error logging levels that most closely mirror
what `java.util.logging` uses.

Closes diffplug#1116.
  • Loading branch information
jamietanna committed Feb 6, 2022
1 parent 47ac76e commit fc6f098
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 38 deletions.
1 change: 0 additions & 1 deletion gradle/javadoc/java8/package-list
Expand Up @@ -55,7 +55,6 @@ java.util.concurrent.atomic
java.util.concurrent.locks
java.util.function
java.util.jar
java.util.logging
java.util.prefs
java.util.regex
java.util.spi
Expand Down
2 changes: 2 additions & 0 deletions lib/build.gradle
Expand Up @@ -21,6 +21,7 @@ for (glue in NEEDS_GLUE) {
}

dependencies {
compileOnly 'org.slf4j:slf4j-api:1.7.35'
// zero runtime reqs is a hard requirements for spotless-lib
// if you need a dep, put it in lib-extra
testImplementation "org.junit.jupiter:junit-jupiter:$VER_JUNIT"
Expand All @@ -29,6 +30,7 @@ dependencies {

// used for pom sorting
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0'
sortPomCompileOnly 'org.slf4j:slf4j-api:1.7.35'

palantirJavaFormatCompileOnly 'com.palantir.javaformat:palantir-java-format:1.1.0'

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,13 @@
*/
package com.diffplug.spotless;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class FormatExceptionPolicyLegacy extends NoLambda.EqualityBasedOnSerialization implements FormatExceptionPolicy {
private static final long serialVersionUID = 1L;

private static final Logger logger = Logger.getLogger(Formatter.class.getName());
private static final Logger logger = LoggerFactory.getLogger(Formatter.class.getName());

@Override
public void handleError(Throwable e, FormatterStep step, String relativePath) {
Expand All @@ -34,10 +34,10 @@ public void handleError(Throwable e, FormatterStep step, String relativePath) {
}

static void error(Throwable e, FormatterStep step, String relativePath) {
logger.log(Level.SEVERE, "Step '" + step.getName() + "' found problem in '" + relativePath + "':\n" + e.getMessage(), e);
logger.error("Step '" + step.getName() + "' found problem in '" + relativePath + "':\n" + e.getMessage(), e);
}

static void warning(Throwable e, FormatterStep step, String relativePath) {
logger.log(Level.WARNING, "Unable to apply step '" + step.getName() + "' to '" + relativePath + "'", e);
logger.warn("Unable to apply step '" + step.getName() + "' to '" + relativePath + "'", e);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,9 @@
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.logging.Logger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
Expand Down Expand Up @@ -78,8 +80,8 @@ private static class State implements Serializable {
}

FormatterFunc createFormat() throws Exception {
Logger logger = Logger.getLogger(FreshMarkStep.class.getName());
Consumer<String> loggingStream = logger::warning;
Logger logger = LoggerFactory.getLogger(FreshMarkStep.class.getName());
Consumer<String> loggingStream = logger::warn;

ClassLoader classLoader = jarState.getClassLoader();

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,9 @@
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Logger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diffplug.spotless.FileSignature;
import com.diffplug.spotless.FormatterFunc;
Expand All @@ -36,7 +38,7 @@

abstract class NpmFormatterStepStateBase implements Serializable {

private static final Logger logger = Logger.getLogger(NpmFormatterStepStateBase.class.getName());
private static final Logger logger = LoggerFactory.getLogger(NpmFormatterStepStateBase.class.getName());

private static final long serialVersionUID = 1460749955865959948L;

Expand Down Expand Up @@ -166,13 +168,13 @@ public String getBaseUrl() {
@Override
public void close() throws Exception {
try {
logger.fine("Closing npm server in directory <" + serverPortFile.getParent() + "> and port <" + serverPort + ">");
logger.trace("Closing npm server in directory <" + serverPortFile.getParent() + "> and port <" + serverPort + ">");
if (server.isAlive()) {
boolean ended = server.waitFor(5, TimeUnit.SECONDS);
if (!ended) {
logger.info("Force-Closing npm server in directory <" + serverPortFile.getParent() + "> and port <" + serverPort + ">");
server.destroyForcibly().waitFor();
logger.fine("Force-Closing npm server in directory <" + serverPortFile.getParent() + "> and port <" + serverPort + "> -- Finished");
logger.trace("Force-Closing npm server in directory <" + serverPortFile.getParent() + "> and port <" + serverPort + "> -- Finished");
}
}
} finally {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,11 +23,12 @@
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nonnull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterFunc.Closeable;
import com.diffplug.spotless.FormatterStep;
Expand All @@ -36,7 +37,7 @@

public class PrettierFormatterStep {

private static final Logger logger = Logger.getLogger(PrettierFormatterStep.class.getName());
private static final Logger logger = LoggerFactory.getLogger(PrettierFormatterStep.class.getName());

public static final String NAME = "prettier-format";

Expand Down Expand Up @@ -95,7 +96,7 @@ private void endServer(PrettierRestService restService, ServerProcessInfo restSe
try {
restService.shutdown();
} catch (Throwable t) {
logger.log(Level.INFO, "Failed to request shutdown of rest service via api. Trying via process.", t);
logger.info("Failed to request shutdown of rest service via api. Trying via process.", t);
}
restServer.close();
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,12 +21,13 @@
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterFunc.Closeable;
import com.diffplug.spotless.FormatterStep;
Expand All @@ -35,7 +36,7 @@

public class TsFmtFormatterStep {

private static final Logger logger = Logger.getLogger(TsFmtFormatterStep.class.getName());
private static final Logger logger = LoggerFactory.getLogger(TsFmtFormatterStep.class.getName());

public static final String NAME = "tsfmt-format";

Expand Down Expand Up @@ -115,7 +116,7 @@ private void endServer(TsFmtRestService restService, ServerProcessInfo restServe
try {
restService.shutdown();
} catch (Throwable t) {
logger.log(Level.INFO, "Failed to request shutdown of rest service via api. Trying via process.", t);
logger.info("Failed to request shutdown of rest service via api. Trying via process.", t);
}
restServer.close();
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,9 +18,10 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.logging.Logger;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.pom.SortPomCfg;
Expand All @@ -30,7 +31,7 @@
import sortpom.parameter.PluginParameters;

public class SortPomFormatterFunc implements FormatterFunc {
private static final Logger logger = Logger.getLogger(SortPomFormatterFunc.class.getName());
private static final Logger logger = LoggerFactory.getLogger(SortPomFormatterFunc.class.getName());
private final SortPomCfg cfg;

public SortPomFormatterFunc(SortPomCfg cfg) {
Expand Down Expand Up @@ -61,7 +62,7 @@ public String apply(String input) throws Exception {
private static class MySortPomLogger implements SortPomLogger {
@Override
public void warn(String content) {
logger.warning(content);
logger.warn(content);
}

@Override
Expand All @@ -71,7 +72,7 @@ public void info(String content) {

@Override
public void error(String content) {
logger.severe(content);
logger.error(content);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,8 +20,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.gradle.api.GradleException;
import org.gradle.api.Project;
Expand All @@ -30,6 +28,8 @@
import org.gradle.api.artifacts.dsl.DependencyHandler;
import org.gradle.api.attributes.Bundling;
import org.gradle.api.initialization.dsl.ScriptHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.diffplug.common.base.Unhandled;
import com.diffplug.common.collect.ImmutableList;
Expand Down Expand Up @@ -127,8 +127,7 @@ private static Provisioner forConfigurationContainer(Project project, Configurat
if (!projName.isEmpty()) {
projName = projName + "/";
}
logger.log(
Level.SEVERE,
logger.error(
"You need to add a repository containing the '" + mavenCoords + "' artifact in '" + projName + "build.gradle'.\n" +
"E.g.: 'repositories { mavenCentral() }'",
e);
Expand All @@ -137,7 +136,7 @@ private static Provisioner forConfigurationContainer(Project project, Configurat
};
}

private static final Logger logger = Logger.getLogger(GradleProvisioner.class.getName());
private static final Logger logger = LoggerFactory.getLogger(GradleProvisioner.class.getName());

/** Models a request to the provisioner. */
private static class Request {
Expand Down

0 comments on commit fc6f098

Please sign in to comment.