Skip to content

Commit

Permalink
[eclipse-hono#565] Drop ability to specify logger levels
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Apr 18, 2018
1 parent bbbdb15 commit 1da309b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface ConnectionEventProducer {
* @param authenticatedDevice The optional authenticated device associated with the connection. May be {@code null}
* if the connection is from an unauthenticated device.
*/
public void connected(String remoteId, Device authenticatedDevice);
void connected(String remoteId, Device authenticatedDevice);

/**
* Produce an event for a closed connection.
Expand All @@ -47,5 +47,5 @@ public interface ConnectionEventProducer {
* @param authenticatedDevice The optional authenticated device associated with the connection. May be {@code null}
* if the connection is from an unauthenticated device.
*/
public void disconnected(String remoteId, Device authenticatedDevice);
void disconnected(String remoteId, Device authenticatedDevice);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,27 @@
*/
package org.eclipse.hono.service.monitoring;

import static java.lang.invoke.MethodType.methodType;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

import org.eclipse.hono.service.auth.device.Device;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;

/**
* A logging only implementation.
* <p>
* This implementation only create log messages as "events". It will log everything on "info" level.
*/
public class LoggingConnectionEventProducer implements ConnectionEventProducer {

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

private final MethodHandle handle;

/**
* Create a new instance with the default log level ({@link Level#INFO}).
*/
public LoggingConnectionEventProducer() {
this(null);
}

/**
* Create a new instance with the provided log level.
*
* @param level The provided log level to use, may be {@code null} in which case {@link Level#INFO} will be used.
*/
public LoggingConnectionEventProducer(Level level) {
if (level == null) {
level = Level.INFO;
}
this.handle = loggerMethod(level.name().toLowerCase()).bindTo(logger);
}

private MethodHandle loggerMethod(final String methodName) {
try {
return MethodHandles.lookup()
.findVirtual(Logger.class,
methodName,
methodType(void.class, String.class, Object.class, Object.class));
} catch (final NoSuchMethodException | IllegalAccessException e) {
throw new RuntimeException("Failed to set up logger", e);
}
}

private void log(final String message, final Object connectionIdentifier, final Object authenticatedDevice) {
try {
logInternal(message, connectionIdentifier, authenticatedDevice);
} catch (final Throwable e) {
// we don't want a log message to fail anything
}
}

void logInternal(final String message, final Object connectionIdentifier, final Object authenticatedDevice)
throws Throwable {
this.handle.invokeExact(message, connectionIdentifier, authenticatedDevice);
}

@Override
public void connected(final String remoteId, final Device authenticatedDevice) {
log(" Connected - ID: {}, Device: {}", remoteId, authenticatedDevice);
logger.info(" Connected - ID: {}, Device: {}", remoteId, authenticatedDevice);
}

@Override
public void disconnected(final String remoteId, final Device authenticatedDevice) {
log("Disconnected - ID: {}, Device: {}", remoteId, authenticatedDevice);
logger.info("Disconnected - ID: {}, Device: {}", remoteId, authenticatedDevice);
}

}

This file was deleted.

0 comments on commit 1da309b

Please sign in to comment.