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

change: allow to set the org.log4j.Level that method invocations are logged...

change: allow to set the org.log4j.Level that method invocations are logged with in LogInvocationHandler and set it to DEBUG for CIFEX

SVN: 3964
parent ead9cf76
No related branches found
No related tags found
No related merge requests found
...@@ -21,11 +21,11 @@ import java.lang.reflect.InvocationTargetException; ...@@ -21,11 +21,11 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException; import java.lang.reflect.UndeclaredThrowableException;
import org.apache.log4j.Level;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
/** /**
* Invocation handler used to log invocations. * Invocation handler used to log invocations.
*
* *
* @author Franz-Josef Elmer * @author Franz-Josef Elmer
*/ */
...@@ -33,19 +33,22 @@ public final class LogInvocationHandler implements InvocationHandler ...@@ -33,19 +33,22 @@ public final class LogInvocationHandler implements InvocationHandler
{ {
private final Object object; private final Object object;
private final String name; private final String name;
private final Level logLevel;
private final Class<?> classUsedToNameLogger; private final Class<?> classUsedToNameLogger;
/** /**
* Creates a new instance. * Creates a new instance.
* *
* @param object Object whos invocations should be logged. * @param object Object whose invocations should be logged.
* @param name Meaningful name of <code>object</code>. Will be used in the log message. * @param name Meaningful name of <code>object</code>. Will be used in the log message.
* @param logLevel The log level to use for normal (successful) events.
* @param classUsedToNameLogger Class used to specify the name of the logger. * @param classUsedToNameLogger Class used to specify the name of the logger.
*/ */
public LogInvocationHandler(Object object, String name, Class<?> classUsedToNameLogger) public LogInvocationHandler(Object object, String name, Level logLevel, Class<?> classUsedToNameLogger)
{ {
this.object = object; this.object = object;
this.name = name; this.name = name;
this.logLevel = logLevel;
this.classUsedToNameLogger = classUsedToNameLogger; this.classUsedToNameLogger = classUsedToNameLogger;
} }
...@@ -70,36 +73,39 @@ public final class LogInvocationHandler implements InvocationHandler ...@@ -70,36 +73,39 @@ public final class LogInvocationHandler implements InvocationHandler
throw t; throw t;
} finally } finally
{ {
StringBuilder builder = new StringBuilder(throwable == null ? "Successful" : "Failed"); final Logger logger = createLogger(method);
builder.append(" invocation of "); if (throwable != null || logger.isEnabledFor(logLevel))
builder.append(name).append('.').append(method.getName()).append('(');
if (args != null)
{ {
for (int i = 0; i < args.length; i++) final StringBuilder builder = new StringBuilder(throwable == null ? "Successful" : "Failed");
builder.append(" invocation of ");
builder.append(name).append('.').append(method.getName()).append('(');
if (args != null)
{ {
builder.append(args[i]); for (int i = 0; i < args.length; i++)
if (i < args.length - 1)
{ {
builder.append(", "); builder.append(args[i]);
if (i < args.length - 1)
{
builder.append(", ");
}
} }
} }
} builder.append(") took ").append(System.currentTimeMillis() - time).append(" msec");
builder.append(") took ").append(System.currentTimeMillis() - time).append(" msec"); if (throwable == null)
Logger logger = createLogger(method); {
if (throwable == null) logger.log(logLevel, builder.toString());
{ } else
logger.info(builder); {
} else logger.error(builder.toString(), throwable);
{ }
logger.error(builder, throwable);
} }
} }
} }
private Logger createLogger(Method method) private Logger createLogger(Method method)
{ {
LogAnnotation annotation = method.getAnnotation(LogAnnotation.class); final LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
LogCategory logCategory = annotation == null ? LogCategory.ACCESS : annotation.logCategory(); final LogCategory logCategory = (annotation == null) ? LogCategory.ACCESS : annotation.logCategory();
return LogFactory.getLogger(logCategory, classUsedToNameLogger); return LogFactory.getLogger(logCategory, classUsedToNameLogger);
} }
} }
\ No newline at end of file
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