Skip to content

Commit

Permalink
Merge pull request #12 from FusionAuth/degroff/logger-updates
Browse files Browse the repository at this point in the history
Break  Logger interface. Update logger to be more standard.
  • Loading branch information
robotdan authored Jul 25, 2023
2 parents e237bcb + 45cbbdc commit b8732f1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.savant
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jackson5Version = "3.0.1"
restifyVersion = "4.1.2"
testngVersion = "7.8.0"

project(group: "io.fusionauth", name: "java-http", version: "0.1.16", licenses: ["ApacheV2_0"]) {
project(group: "io.fusionauth", name: "java-http", version: "0.2.0", licenses: ["ApacheV2_0"]) {
workflow {
fetch {
// Dependency resolution order:
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/io/fusionauth/http/log/BaseLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,25 @@ public void info(String message, Object... values) {
}

@Override
public boolean isDebuggable() {
public boolean isDebugEnabled() {
return getLevelOrdinal() <= Level.Debug.ordinal();
}

@Override
public boolean isErrorEnabled() {
return getLevelOrdinal() <= Level.Error.ordinal();
}

@Override
public boolean isInfoEnabled() {
return getLevelOrdinal() <= Level.Info.ordinal();
}

@Override
public boolean isTraceEnabled() {
return getLevelOrdinal() <= Level.Trace.ordinal();
}

@Override
public void setLevel(Level level) {
this.level = level;
Expand Down
50 changes: 40 additions & 10 deletions src/main/java/io/fusionauth/http/log/Logger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, FusionAuth, All Rights Reserved
* Copyright (c) 2022-2023, FusionAuth, All Rights Reserved
*
* 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 @@ -76,9 +76,46 @@ public interface Logger {
void info(String message, Object... values);

/**
* @return If this logger has debug enabled.
* @return True if this Logger is enabled for the Debug level, false otherwise.
*/
boolean isDebuggable();
boolean isDebugEnabled();

/**
* Returns whether this Logger is enabled for a given {@link Level}.
*
* @param level the level to check
* @return true if enabled, false otherwise.
*/
default boolean isEnabledForLevel(Level level) {
return switch (level) {
case Trace -> isTraceEnabled();
case Debug -> isDebugEnabled();
case Info -> isInfoEnabled();
case Error -> isErrorEnabled();
};
}

/**
* @return True if this Logger is enabled for the Error level, false otherwise.
*/
boolean isErrorEnabled();

/**
* @return True if this Logger is enabled for the Info level, false otherwise.
*/
boolean isInfoEnabled();

/**
* @return True if this Logger is enabled for the Trace level, false otherwise.
*/
boolean isTraceEnabled();

/**
* Sets the level of this logger (optional method).
*
* @param level The level.
*/
void setLevel(Level level);

/**
* Logs a trace message with values.
Expand All @@ -94,11 +131,4 @@ public interface Logger {
* @param message The message.
*/
void trace(String message);

/**
* Sets the level of this logger (optional method).
*
* @param level The level.
*/
void setLevel(Level level);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public synchronized ByteBuffer[] currentBuffer() {
}

logger.debug("Preamble is [{}] bytes long", preambleBuffers[0].remaining());
if (logger.isDebuggable()) {
if (logger.isDebugEnabled()) {
logger.debug("Preamble is [\n{}\n]", new String(preambleBuffers[0].array(), 0, preambleBuffers[0].remaining()));
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/fusionauth/http/server/HTTPServerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void accept(SelectionKey key) throws GeneralSecurityException, IOExcepti
client.configureBlocking(false);
client.register(key.selector(), tlsProcessor.initialKeyOps(), tlsProcessor);

if (logger.isDebuggable()) {
if (logger.isDebugEnabled()) {
try {
logger.debug("Accepted connection from client [{}]", client.getRemoteAddress().toString());
} catch (IOException e) {
Expand All @@ -226,7 +226,7 @@ private void accept(SelectionKey key) throws GeneralSecurityException, IOExcepti
private void cancelAndCloseKey(SelectionKey key) {
if (key != null) {
try (var client = key.channel()) {
if (logger.isDebuggable() && client instanceof SocketChannel socketChannel) {
if (logger.isDebugEnabled() && client instanceof SocketChannel socketChannel) {
logger.debug("Closing connection to client [{}]", socketChannel.getRemoteAddress().toString());
}

Expand All @@ -251,7 +251,7 @@ private void cleanup() {
.filter(key -> key.attachment() != null)
.filter(key -> ((HTTPProcessor) key.attachment()).lastUsed() < now - clientTimeout.toMillis())
.forEach(key -> {
if (logger.isDebuggable()) {
if (logger.isDebugEnabled()) {
var client = (SocketChannel) key.channel();
try {
logger.debug("Closing client connection [{}] due to inactivity", client.getRemoteAddress().toString());
Expand Down

0 comments on commit b8732f1

Please sign in to comment.