Skip to content
Snippets Groups Projects
Commit 5be2fd27 authored by brinn's avatar brinn
Browse files

refactor:

- make the ISimpleLogger.LogLevel a top level class
change:
- add LogLevel.UNDEFINED and LogLevel.TRACE

SVN: 4329
parent 7d573a53
No related branches found
No related tags found
No related merge requests found
...@@ -27,7 +27,7 @@ import java.util.concurrent.TimeoutException; ...@@ -27,7 +27,7 @@ import java.util.concurrent.TimeoutException;
import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
import ch.systemsx.cisd.common.logging.ISimpleLogger; import ch.systemsx.cisd.common.logging.ISimpleLogger;
import ch.systemsx.cisd.common.logging.ISimpleLogger.Level; import ch.systemsx.cisd.common.logging.LogLevel;
/** /**
* Concurrency related utility methods. * Concurrency related utility methods.
...@@ -91,7 +91,7 @@ public final class ConcurrencyUtilities ...@@ -91,7 +91,7 @@ public final class ConcurrencyUtilities
if (loggerOrNull != null) if (loggerOrNull != null)
{ {
loggerOrNull loggerOrNull
.log(Level.DEBUG, String.format("%s took longer than %f s, cancelled.", .log(LogLevel.DEBUG, String.format("%s took longer than %f s, cancelled.",
operationNameOrNull == null ? "UNKNOWN OPERATION" : operationNameOrNull, operationNameOrNull == null ? "UNKNOWN OPERATION" : operationNameOrNull,
timeoutMillis / 1000f)); timeoutMillis / 1000f));
} }
...@@ -101,7 +101,7 @@ public final class ConcurrencyUtilities ...@@ -101,7 +101,7 @@ public final class ConcurrencyUtilities
future.cancel(true); future.cancel(true);
if (loggerOrNull != null) if (loggerOrNull != null)
{ {
loggerOrNull.log(Level.DEBUG, String.format("%s got interrupted.", loggerOrNull.log(LogLevel.DEBUG, String.format("%s got interrupted.",
operationNameOrNull == null ? "UNKNOWN OPERATION" : operationNameOrNull)); operationNameOrNull == null ? "UNKNOWN OPERATION" : operationNameOrNull));
} }
return null; return null;
...@@ -110,7 +110,7 @@ public final class ConcurrencyUtilities ...@@ -110,7 +110,7 @@ public final class ConcurrencyUtilities
final Throwable cause = ex.getCause(); final Throwable cause = ex.getCause();
if (loggerOrNull != null) if (loggerOrNull != null)
{ {
loggerOrNull.log(Level.ERROR, String.format("%s has caused an exception: %s", loggerOrNull.log(LogLevel.ERROR, String.format("%s has caused an exception: %s",
operationNameOrNull == null ? "UNKNOWN OPERATION" : operationNameOrNull, cause.getClass() operationNameOrNull == null ? "UNKNOWN OPERATION" : operationNameOrNull, cause.getClass()
.getSimpleName(), cause.getMessage() != null ? cause.getMessage() : "<no message>")); .getSimpleName(), cause.getMessage() != null ? cause.getMessage() : "<no message>"));
} }
......
...@@ -24,7 +24,7 @@ package ch.systemsx.cisd.common.logging; ...@@ -24,7 +24,7 @@ package ch.systemsx.cisd.common.logging;
public class ConsoleLogger implements ISimpleLogger public class ConsoleLogger implements ISimpleLogger
{ {
public void log(Level level, String message) public void log(LogLevel level, String message)
{ {
System.out.println(level.toString() + ": " + message); System.out.println(level.toString() + ": " + message);
} }
......
...@@ -24,10 +24,7 @@ package ch.systemsx.cisd.common.logging; ...@@ -24,10 +24,7 @@ package ch.systemsx.cisd.common.logging;
public interface ISimpleLogger public interface ISimpleLogger
{ {
/** A simple form of log levels. */
public enum Level { ERROR, WARN, INFO, DEBUG }
/** Log <var>message</var> at log <var>level</var> out to some log file or display. */ /** Log <var>message</var> at log <var>level</var> out to some log file or display. */
public void log(Level level, String message); public void log(LogLevel level, String message);
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package ch.systemsx.cisd.common.logging; package ch.systemsx.cisd.common.logging;
import org.apache.log4j.Level;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.log4j.Priority; import org.apache.log4j.Priority;
...@@ -30,20 +31,24 @@ public class Log4jSimpleLogger implements ISimpleLogger ...@@ -30,20 +31,24 @@ public class Log4jSimpleLogger implements ISimpleLogger
private final Logger log4jLogger; private final Logger log4jLogger;
private static final Priority toLog4jPriority(Level level) static final Level toLog4jPriority(LogLevel level)
{ {
switch (level) switch (level)
{ {
case ERROR: case OFF:
return org.apache.log4j.Level.ERROR; return org.apache.log4j.Level.OFF;
case WARN: case TRACE:
return org.apache.log4j.Level.WARN; return org.apache.log4j.Level.TRACE;
case INFO:
return org.apache.log4j.Level.INFO;
case DEBUG: case DEBUG:
return org.apache.log4j.Level.DEBUG; return org.apache.log4j.Level.DEBUG;
case INFO:
return org.apache.log4j.Level.INFO;
case WARN:
return org.apache.log4j.Level.WARN;
case ERROR:
return org.apache.log4j.Level.ERROR;
default: default:
throw new IllegalArgumentException("Unknown log level " + level); throw new IllegalArgumentException("Illegal log level " + level);
} }
} }
...@@ -52,7 +57,7 @@ public class Log4jSimpleLogger implements ISimpleLogger ...@@ -52,7 +57,7 @@ public class Log4jSimpleLogger implements ISimpleLogger
* *
* @param log4jLogger The log4j logger to use. * @param log4jLogger The log4j logger to use.
* @param log4jOverridePriorityOrNull If not <code>null</code>, use this log level instead of the one provided to * @param log4jOverridePriorityOrNull If not <code>null</code>, use this log level instead of the one provided to
* the {@link ISimpleLogger#log(ch.systemsx.cisd.common.logging.ISimpleLogger.Level, String)}. * the {@link ISimpleLogger#log(ch.systemsx.cisd.common.logging.LogLevel, String)}.
*/ */
public Log4jSimpleLogger(Logger log4jLogger, Priority log4jOverridePriorityOrNull) public Log4jSimpleLogger(Logger log4jLogger, Priority log4jOverridePriorityOrNull)
{ {
...@@ -70,7 +75,7 @@ public class Log4jSimpleLogger implements ISimpleLogger ...@@ -70,7 +75,7 @@ public class Log4jSimpleLogger implements ISimpleLogger
this(log4jLogger, null); this(log4jLogger, null);
} }
public void log(Level level, String message) public void log(LogLevel level, String message)
{ {
if (log4jOverridePriorityOrNull != null) if (log4jOverridePriorityOrNull != null)
{ {
......
...@@ -22,7 +22,6 @@ import java.lang.annotation.Retention; ...@@ -22,7 +22,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
/** /**
* Log annotation for methods invoked by {@link LogInvocationHandler}. * Log annotation for methods invoked by {@link LogInvocationHandler}.
* *
...@@ -34,4 +33,5 @@ import java.lang.annotation.Target; ...@@ -34,4 +33,5 @@ import java.lang.annotation.Target;
public @interface LogAnnotation public @interface LogAnnotation
{ {
public LogCategory logCategory() default LogCategory.ACCESS; public LogCategory logCategory() default LogCategory.ACCESS;
public LogLevel logLevel() default LogLevel.UNDEFINED;
} }
package ch.systemsx.cisd.common.logging;
/**
* A simple form of log levels.
*
* @author Bernd RInn
*/
public enum LogLevel
{
UNDEFINED, OFF, TRACE, DEBUG, INFO, WARN, ERROR
}
\ No newline at end of file
...@@ -27,6 +27,7 @@ import org.apache.log4j.Logger; ...@@ -27,6 +27,7 @@ import org.apache.log4j.Logger;
import ch.systemsx.cisd.common.logging.ISimpleLogger; import ch.systemsx.cisd.common.logging.ISimpleLogger;
import ch.systemsx.cisd.common.logging.LogCategory; import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory; import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.common.logging.LogLevel;
/** /**
* A {@link TimerTask} that scans a source directory for entries that are accepted by some {@link FileFilter} and * A {@link TimerTask} that scans a source directory for entries that are accepted by some {@link FileFilter} and
...@@ -304,7 +305,7 @@ public final class DirectoryScanningTimerTask extends TimerTask ...@@ -304,7 +305,7 @@ public final class DirectoryScanningTimerTask extends TimerTask
{ {
return new ISimpleLogger() return new ISimpleLogger()
{ {
public void log(ISimpleLogger.Level dummyLevel, String message) public void log(LogLevel dummyLevel, String message)
{ {
if (category == LogCategory.NOTIFY) if (category == LogCategory.NOTIFY)
{ {
......
...@@ -46,6 +46,7 @@ import org.apache.commons.lang.StringUtils; ...@@ -46,6 +46,7 @@ import org.apache.commons.lang.StringUtils;
import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException; import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.logging.ISimpleLogger; import ch.systemsx.cisd.common.logging.ISimpleLogger;
import ch.systemsx.cisd.common.logging.LogLevel;
/** /**
* Some useful utility methods for files and directories. * Some useful utility methods for files and directories.
...@@ -398,7 +399,7 @@ public final class FileUtilities ...@@ -398,7 +399,7 @@ public final class FileUtilities
{ {
if (loggerOrNull != null) if (loggerOrNull != null)
{ {
loggerOrNull.log(ISimpleLogger.Level.INFO, String.format("Deleting file '%s'", file.getPath())); loggerOrNull.log(LogLevel.INFO, String.format("Deleting file '%s'", file.getPath()));
} }
file.delete(); file.delete();
} }
...@@ -406,7 +407,7 @@ public final class FileUtilities ...@@ -406,7 +407,7 @@ public final class FileUtilities
} }
if (loggerOrNull != null) if (loggerOrNull != null)
{ {
loggerOrNull.log(ISimpleLogger.Level.INFO, String.format("Deleting directory '%s'", path.getPath())); loggerOrNull.log(LogLevel.INFO, String.format("Deleting directory '%s'", path.getPath()));
} }
return path.delete(); return path.delete();
} }
...@@ -780,18 +781,18 @@ public final class FileUtilities ...@@ -780,18 +781,18 @@ public final class FileUtilities
{ {
if (directory.isFile()) if (directory.isFile())
{ {
logger.log(ISimpleLogger.Level.ERROR, String.format( logger.log(LogLevel.ERROR, String.format(
"Failed to get listing of directory '%s' (path is file instead of directory).", directory)); "Failed to get listing of directory '%s' (path is file instead of directory).", directory));
} else } else
{ {
logger.log(ISimpleLogger.Level.ERROR, String.format( logger.log(LogLevel.ERROR, String.format(
"Failed to get listing of directory '%s' (path not found).", directory)); "Failed to get listing of directory '%s' (path not found).", directory));
} }
} else } else
{ {
StringWriter exStackWriter = new StringWriter(); StringWriter exStackWriter = new StringWriter();
exOrNull.printStackTrace(new PrintWriter(exStackWriter)); exOrNull.printStackTrace(new PrintWriter(exStackWriter));
logger.log(ISimpleLogger.Level.ERROR, String.format( logger.log(LogLevel.ERROR, String.format(
"Failed to get listing of directory '%s'. Exception: %s", directory, exStackWriter.toString())); "Failed to get listing of directory '%s'. Exception: %s", directory, exStackWriter.toString()));
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment