Skip to content

Commit

Permalink
Switch to slf4j-api for logging (#74)
Browse files Browse the repository at this point in the history
* chore: use slf4j for logging

* fix: bring back `protected` loggers

* fix: use correct access modifiers for loggers

* chore: don't explicitly check for error logging enabled
  • Loading branch information
JarvisCraft authored Jul 1, 2023
1 parent 4bd740b commit f58d87c
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 149 deletions.
27 changes: 4 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<licenses>
<license>
<name>Apache License Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
<distribution>repo</distribution>
</license>
</licenses>
Expand Down Expand Up @@ -41,28 +41,9 @@
<version>9.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<exclusions>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
12 changes: 6 additions & 6 deletions src/one/nio/cluster/WeightCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package one.nio.cluster;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -29,7 +29,7 @@
import java.util.concurrent.ThreadLocalRandom;

public class WeightCluster<T extends ServiceProvider> implements Cluster<T> {
protected static final Log log = LogFactory.getLog(WeightCluster.class);
protected static final Logger log = LoggerFactory.getLogger(WeightCluster.class);

protected final HashMap<T, Integer> providers = new HashMap<>();
protected Timer monitorTimer;
Expand Down Expand Up @@ -65,15 +65,15 @@ public T getProvider() throws ServiceUnavailableException {
@Override
public void enableProvider(T provider) {
if (provider.enable()) {
log.info("Enabled " + provider);
log.info("Enabled {}", provider);
rebuildProviderSelector();
}
}

@Override
public void disableProvider(T provider) {
if (provider.disable()) {
log.info("Disabled " + provider);
log.info("Disabled {}", provider);
rebuildProviderSelector();
monitorTimer.schedule(new MonitoringTask(provider), monitorTimeout, monitorTimeout);
}
Expand Down Expand Up @@ -181,7 +181,7 @@ public void run() {
cancel();
}
} catch (Throwable e) {
log.warn(provider + " is not available because of " + e);
log.warn("{} is not available", provider, e);
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/one/nio/gen/BytecodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@

package one.nio.gen;

import one.nio.mgt.Management;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;

import java.io.IOException;
import java.lang.invoke.MethodHandleInfo;
Expand All @@ -34,11 +30,15 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicInteger;

import static java.nio.file.StandardOpenOption.*;
import one.nio.mgt.Management;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BytecodeGenerator extends ClassLoader implements BytecodeGeneratorMXBean, Opcodes {
private static final Log log = LogFactory.getLog(BytecodeGenerator.class);
private static final Logger log = LoggerFactory.getLogger(BytecodeGenerator.class);

public static final BytecodeGenerator INSTANCE = new BytecodeGenerator();

Expand Down Expand Up @@ -92,7 +92,7 @@ public void dumpClass(byte[] classData, String className) {
try {
Files.write(Paths.get(dumpPath, className + ".class"), classData, WRITE, CREATE, TRUNCATE_EXISTING);
} catch (IOException e) {
log.error("Could not dump " + className, e);
log.error("Could not dump {}", className, e);
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/one/nio/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
import one.nio.pool.PoolException;
import one.nio.pool.SocketPool;
import one.nio.util.Utf8;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

import java.io.IOException;
import java.net.SocketTimeoutException;
Expand All @@ -34,7 +35,7 @@
import java.util.List;

public class HttpClient extends SocketPool {
protected static final Log log = LogFactory.getLog(HttpClient.class);
private static final Logger log = LoggerFactory.getLogger(HttpClient.class);

protected String[] permanentHeaders;
protected int bufferSize;
Expand Down Expand Up @@ -232,9 +233,7 @@ Response readResponse(int method) throws IOException, HttpException {
} else if ("close".equalsIgnoreCase(response.getHeader("Connection:"))) {
response.setBody(readBodyUntilClose());
} else {
if (log.isDebugEnabled()) {
log.debug("Content-Length unspecified: " + response.toString());
}
log.debug("Content-Length unspecified: {}", response);
throw new HttpException("Content-Length unspecified");
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/one/nio/http/HttpCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ protected HttpProvider createProvider(String provider) {
}

public Response invoke(Request request) throws ServiceUnavailableException {
if (log.isTraceEnabled()) {
log.trace(request.toString());
}
log.trace("{}", request);

final int retries = this.retries;
for (int i = 0; i < retries; i++) {
Expand All @@ -83,9 +81,9 @@ public Response invoke(Request request) throws ServiceUnavailableException {
}
if ((e instanceof SocketTimeoutException || e.getCause() instanceof SocketTimeoutException)
&& !(log.isTraceEnabled() || logTimeouts)) {
log.debug(provider + " timed out");
log.debug("{} timed out", provider);
} else {
log.warn(provider + " invocation failed " + request.getURI(), e);
log.warn("{} invocation failed {}", provider, request.getURI(), e);
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/one/nio/http/HttpSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,10 @@ protected void processRead(byte[] buffer) throws IOException {
}
fragmentLength = length;
} catch (HttpException e) {
if (log.isDebugEnabled()) {
log.debug("Bad request", e);
}
log.debug("Bad request", e);
sendError(Response.BAD_REQUEST, e.getMessage());
} catch (BufferOverflowException e) {
if (log.isDebugEnabled()) {
log.debug("Request entity too large", e);
}
log.debug("Request entity too large", e);
sendError(Response.REQUEST_ENTITY_TOO_LARGE, "");
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/one/nio/mem/OffheapMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import one.nio.util.JavaInternals;
import one.nio.util.QuickSelect;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sun.misc.Unsafe;

import java.util.Arrays;
Expand All @@ -34,7 +35,7 @@
import java.util.concurrent.atomic.AtomicLong;

public abstract class OffheapMap<K, V> implements OffheapMapMXBean {
protected static final Log log = LogFactory.getLog(OffheapMap.class);
protected static final Logger log = LoggerFactory.getLogger(OffheapMap.class);
protected static final Unsafe unsafe = JavaInternals.unsafe;
protected static final long byteArrayOffset = JavaInternals.byteArrayOffset;
protected static final long MB = 1024 * 1024;
Expand Down Expand Up @@ -376,7 +377,7 @@ public int removeExpired(long expirationAge) {
for (int i = 0; i < CONCURRENCY_LEVEL; i++) {
RWLock lock = locks[i];
if (!lock.lockWrite(lockWaitTime)) {
log.debug("Could not lock segment " + i + " for cleanup");
log.debug("Could not lock segment {} for cleanup", i);
continue;
}

Expand Down Expand Up @@ -720,12 +721,12 @@ public void run() {
long elapsed = System.currentTimeMillis() - startTime;

if (expired != 0) {
log.info(getName() + " cleaned " + expired + " entries in " + elapsed + " ms");
log.info("{} cleaned {} entries in {} ms", getName(), expired, elapsed);
}
} catch (InterruptedException e) {
break;
} catch (Throwable e) {
log.error("Exception in " + getName(), e);
log.error("Exception in {}", getName(), e);
}
}
}
Expand Down Expand Up @@ -764,8 +765,9 @@ protected int cleanup() {
int k = entriesToClean < count ? (int) ((long) samples * entriesToClean / count) : 0;
long expirationAge = System.currentTimeMillis() - QuickSelect.select(timestamps, k, 0, samples - 1);

log.info(getName() + " needs to clean " + entriesToClean + " entries. Samples collected = "
+ samples + ", age = " + expirationAge);
log.info("{} needs to clean {} entries. Samples collected = {}, age = {}", getName(), entriesToClean,
samples, expirationAge
);
if (log.isDebugEnabled()) {
log.debug(Arrays.toString(timestamps));
}
Expand Down
12 changes: 6 additions & 6 deletions src/one/nio/mgt/Management.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package one.nio.mgt;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.Attribute;
import javax.management.AttributeList;
Expand All @@ -31,15 +31,15 @@
import java.util.Set;

public class Management {
private static final Log log = LogFactory.getLog(Management.class);
private static final Logger log = LoggerFactory.getLogger(Management.class);

static {
try {
// After registering this magic instance, new HotSpot internal MBeans appear:
// HotspotRuntimeMBean, getHotspotMemoryMBean etc.
ManagementFactory.getPlatformMBeanServer().createMBean("sun.management.HotspotInternal", null);
} catch (Exception e) {
log.warn("Cannot register HotspotInternal: " + e);
log.warn("Cannot register HotspotInternal", e);
}
}

Expand All @@ -58,7 +58,7 @@ public static <T> void registerMXBean(T object, Class<T> mxbeanInterface, String
}
beanServer.registerMBean(mb, objectName);
} catch (Exception e) {
log.error("Cannot register MXBean " + name, e);
log.error("Cannot register MXBean {}", name, e);
}
}

Expand All @@ -70,7 +70,7 @@ public static void unregisterMXBean(String name) {
beanServer.unregisterMBean(objectName);
}
} catch (Exception e) {
log.error("Cannot unregister MXBean " + name, e);
log.error("Cannot unregister MXBean {}", name, e);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/one/nio/mgt/ThreadDumper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@

package one.nio.mgt;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.JMException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.concurrent.atomic.AtomicLong;

public class ThreadDumper {
private static final Log log = LogFactory.getLog(ThreadDumper.class);
private static final Logger log = LoggerFactory.getLogger(ThreadDumper.class);
private static final AtomicLong dumpTime = new AtomicLong();

public static void dump(OutputStream out) {
String threadDump;
try {
threadDump = DiagnosticCommand.execute("threadPrint");
} catch (JMException e) {
log.warn("Failed to get threads dump: " + e);
log.warn("Failed to get threads dump", e);
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/one/nio/net/JavaSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package one.nio.net;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.channels.CancelledKeyException;
Expand All @@ -30,7 +30,7 @@
import java.util.concurrent.ConcurrentLinkedQueue;

final class JavaSelector extends Selector {
private static final Log log = LogFactory.getLog(JavaSelector.class);
private static final Logger log = LoggerFactory.getLogger(JavaSelector.class);

private final java.nio.channels.Selector impl;
private final ConcurrentLinkedQueue<Session> pendingSessions;
Expand Down
7 changes: 4 additions & 3 deletions src/one/nio/net/SelectableJavaSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package one.nio.net;

import one.nio.util.JavaInternals;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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

import java.io.FileDescriptor;
import java.io.IOException;
Expand All @@ -35,7 +36,7 @@
* @author ivan.grigoryev
*/
public abstract class SelectableJavaSocket extends Socket {
private static final Log log = LogFactory.getLog(SelectableJavaSocket.class);
private static final Logger log = LoggerFactory.getLogger(SelectableJavaSocket.class);

private static final MethodHandle poll = getMethodHandle("sun.nio.ch.Net", "poll", FileDescriptor.class, int.class, long.class);
private static final MethodHandle getFD = getMethodHandle("sun.nio.ch.SelChImpl", "getFD");
Expand Down
Loading

0 comments on commit f58d87c

Please sign in to comment.